forked from doctrine-extensions/DoctrineExtensions
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathTreeSlugHandler.php
More file actions
179 lines (152 loc) · 6.04 KB
/
TreeSlugHandler.php
File metadata and controls
179 lines (152 loc) · 6.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
<?php
/*
* This file is part of the Doctrine Behavioral Extensions package.
* (c) Gediminas Morkevicius <gediminas.morkevicius@gmail.com> http://www.gediminasm.org
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Gedmo\Sluggable\Handler;
use Doctrine\Persistence\Mapping\ClassMetadata;
use Doctrine\Persistence\ObjectManager;
use Doctrine\Persistence\Proxy;
use Gedmo\Exception\InvalidMappingException;
use Gedmo\Sluggable\Mapping\Event\SluggableAdapter;
use Gedmo\Sluggable\SluggableListener;
use Gedmo\Tool\Wrapper\AbstractWrapper;
use function Symfony\Component\String\u;
/**
* Sluggable handler which slugs all parent nodes
* recursively and synchronizes on updates. For instance
* category tree slug could look like "food/fruits/apples"
*
* @author Gediminas Morkevicius <gediminas.morkevicius@gmail.com>
*
* @final since gedmo/doctrine-extensions 3.11
*/
class TreeSlugHandler implements SlugHandlerWithUniqueCallbackInterface
{
public const SEPARATOR = '/';
/**
* @var ObjectManager
*/
protected $om;
/**
* @var SluggableListener
*/
protected $sluggable;
private string $prefix = '';
private string $suffix = '';
/**
* True if node is being inserted
*/
private bool $isInsert = false;
/**
* Transliterated parent slug
*/
private string $parentSlug = '';
/**
* Used path separator
*/
private string $usedPathSeparator = self::SEPARATOR;
public function __construct(SluggableListener $sluggable)
{
$this->sluggable = $sluggable;
}
public function onChangeDecision(SluggableAdapter $ea, array &$config, $object, &$slug, &$needToChangeSlug)
{
$this->om = $ea->getObjectManager();
$this->isInsert = $this->om->getUnitOfWork()->isScheduledForInsert($object);
$options = $config['handlers'][static::class];
$this->usedPathSeparator = $options['separator'] ?? self::SEPARATOR;
$this->prefix = $options['prefix'] ?? '';
$this->suffix = $options['suffix'] ?? '';
if (!$this->isInsert && !$needToChangeSlug) {
$changeSet = $ea->getObjectChangeSet($this->om->getUnitOfWork(), $object);
if (isset($changeSet[$options['parentRelationField']])) {
$needToChangeSlug = true;
}
}
}
public function postSlugBuild(SluggableAdapter $ea, array &$config, $object, &$slug)
{
$options = $config['handlers'][static::class];
$this->parentSlug = '';
$wrapped = AbstractWrapper::wrap($object, $this->om);
if ($parent = $wrapped->getPropertyValue($options['parentRelationField'])) {
$parent = AbstractWrapper::wrap($parent, $this->om);
$this->parentSlug = $parent->getPropertyValue($config['slug']);
// if needed, remove suffix from parentSlug, so we can use it to prepend it to our slug
if (isset($options['suffix'])) {
$this->parentSlug = u($this->parentSlug)->trimSuffix($options['suffix'])->toString();
}
}
}
/**
* @param ClassMetadata<object> $meta
*/
public static function validate(array $options, ClassMetadata $meta)
{
if (!$meta->isSingleValuedAssociation($options['parentRelationField'])) {
throw new InvalidMappingException("Unable to find tree parent slug relation through field - [{$options['parentRelationField']}] in class - {$meta->getName()}");
}
}
public function beforeMakingUnique(SluggableAdapter $ea, array &$config, $object, &$slug)
{
$slug = $this->transliterate($slug, $config['separator'], $object);
}
public function onSlugCompletion(SluggableAdapter $ea, array &$config, $object, &$slug)
{
if (!$this->isInsert) {
$wrapped = AbstractWrapper::wrap($object, $this->om);
$meta = $wrapped->getMetadata();
$target = $wrapped->getPropertyValue($config['slug']);
$config['pathSeparator'] = $this->usedPathSeparator;
$ea->replaceRelative($object, $config, $target.$config['pathSeparator'], $slug);
$uow = $this->om->getUnitOfWork();
// update in memory objects
foreach ($uow->getIdentityMap() as $className => $objects) {
// for inheritance mapped classes, only root is always in the identity map
if ($className !== $wrapped->getRootObjectName()) {
continue;
}
foreach ($objects as $object) {
// @todo: Remove the check against `method_exists()` in the next major release.
if (($object instanceof Proxy || method_exists($object, '__isInitialized')) && !$object->__isInitialized()) {
continue;
}
$objectSlug = (string) $meta->getReflectionProperty($config['slug'])->getValue($object);
if (preg_match("@^{$target}{$config['pathSeparator']}@smi", $objectSlug)) {
$objectSlug = str_replace($target, $slug, $objectSlug);
$meta->getReflectionProperty($config['slug'])->setValue($object, $objectSlug);
$ea->setOriginalObjectProperty($uow, $object, $config['slug'], $objectSlug);
}
}
}
}
}
/**
* Transliterates the slug and prefixes the slug
* by collection of parent slugs
*
* @param string $text
* @param string $separator
* @param object $object
*
* @return string
*/
public function transliterate($text, $separator, $object)
{
$slug = $text.$this->suffix;
if (strlen($this->parentSlug)) {
$slug = $this->parentSlug.$this->usedPathSeparator.$slug;
} else {
// if no parentSlug, apply our prefix
$slug = $this->prefix.$slug;
}
return $slug;
}
public function handlesUrlization()
{
return false;
}
}