|
@@ -22,6 +22,8 @@ use Gedmo\Exception\InvalidMappingException;
|
|
|
*/
|
|
|
class TreeSlugHandler implements SlugHandlerInterface
|
|
|
{
|
|
|
+ const SEPARATOR = '/';
|
|
|
+
|
|
|
/**
|
|
|
* @var Doctrine\Common\Persistence\ObjectManager
|
|
|
*/
|
|
@@ -32,14 +34,6 @@ class TreeSlugHandler implements SlugHandlerInterface
|
|
|
*/
|
|
|
protected $sluggable;
|
|
|
|
|
|
- /**
|
|
|
- * Options for relative slug handler object
|
|
|
- * classes
|
|
|
- *
|
|
|
- * @var array
|
|
|
- */
|
|
|
- private $options;
|
|
|
-
|
|
|
/**
|
|
|
* Callable of original transliterator
|
|
|
* which is used by sluggable
|
|
@@ -63,44 +57,31 @@ class TreeSlugHandler implements SlugHandlerInterface
|
|
|
private $parentSlug;
|
|
|
|
|
|
/**
|
|
|
- * {@inheritDoc}
|
|
|
+ * Used path separator
|
|
|
+ *
|
|
|
+ * @var string
|
|
|
*/
|
|
|
- public function __construct(SluggableListener $sluggable)
|
|
|
- {
|
|
|
- $this->sluggable = $sluggable;
|
|
|
- }
|
|
|
+ private $usedPathSeparator;
|
|
|
|
|
|
/**
|
|
|
- * $options = array(
|
|
|
- * 'separator' => '/',
|
|
|
- * 'parentRelationField' => 'parent'
|
|
|
- * )
|
|
|
* {@inheritDoc}
|
|
|
*/
|
|
|
- public function getOptions($object)
|
|
|
+ public function __construct(SluggableListener $sluggable)
|
|
|
{
|
|
|
- $meta = $this->om->getClassMetadata(get_class($object));
|
|
|
- if (!isset($this->options[$meta->name])) {
|
|
|
- $config = $this->sluggable->getConfiguration($this->om, $meta->name);
|
|
|
- $options = $config['handlers'][get_called_class()];
|
|
|
- $default = array(
|
|
|
- 'separator' => '/'
|
|
|
- );
|
|
|
- $this->options[$meta->name] = array_merge($default, $options);
|
|
|
- }
|
|
|
- return $this->options[$meta->name];
|
|
|
+ $this->sluggable = $sluggable;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* {@inheritDoc}
|
|
|
*/
|
|
|
- public function onChangeDecision(SluggableAdapter $ea, $slugFieldConfig, $object, &$slug, &$needToChangeSlug)
|
|
|
+ public function onChangeDecision(SluggableAdapter $ea, $config, $object, &$slug, &$needToChangeSlug)
|
|
|
{
|
|
|
$this->om = $ea->getObjectManager();
|
|
|
$this->isInsert = $this->om->getUnitOfWork()->isScheduledForInsert($object);
|
|
|
+ $options = $config['handlers'][get_called_class()];
|
|
|
+ $this->usedPathSeparator = isset($options['separator']) ? $options['separator'] : self::SEPARATOR;
|
|
|
if (!$this->isInsert && !$needToChangeSlug) {
|
|
|
$changeSet = $ea->getObjectChangeSet($this->om->getUnitOfWork(), $object);
|
|
|
- $options = $this->getOptions($object);
|
|
|
if (isset($changeSet[$options['parentRelationField']])) {
|
|
|
$needToChangeSlug = true;
|
|
|
}
|
|
@@ -112,7 +93,7 @@ class TreeSlugHandler implements SlugHandlerInterface
|
|
|
*/
|
|
|
public function postSlugBuild(SluggableAdapter $ea, array &$config, $object, &$slug)
|
|
|
{
|
|
|
- $options = $this->getOptions($object);
|
|
|
+ $options = $config['handlers'][get_called_class()];
|
|
|
$this->originalTransliterator = $this->sluggable->getTransliterator();
|
|
|
$this->sluggable->setTransliterator(array($this, 'transliterate'));
|
|
|
$this->parentSlug = '';
|
|
@@ -140,14 +121,12 @@ class TreeSlugHandler implements SlugHandlerInterface
|
|
|
public function onSlugCompletion(SluggableAdapter $ea, array &$config, $object, &$slug)
|
|
|
{
|
|
|
if (!$this->isInsert) {
|
|
|
- $options = $this->getOptions($object);
|
|
|
+ $options = $config['handlers'][get_called_class()];
|
|
|
$wrapped = AbstractWrapper::wrapp($object, $this->om);
|
|
|
$meta = $wrapped->getMetadata();
|
|
|
- $extConfig = $this->sluggable->getConfiguration($this->om, $meta->name);
|
|
|
- $config['useObjectClass'] = $extConfig['useObjectClass'];
|
|
|
$target = $wrapped->getPropertyValue($config['slug']);
|
|
|
- $config['pathSeparator'] = $options['separator'];
|
|
|
- $ea->replaceRelative($object, $config, $target.$options['separator'], $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) {
|
|
@@ -161,7 +140,7 @@ class TreeSlugHandler implements SlugHandlerInterface
|
|
|
}
|
|
|
$oid = spl_object_hash($object);
|
|
|
$objectSlug = $meta->getReflectionProperty($config['slug'])->getValue($object);
|
|
|
- if (preg_match("@^{$target}{$options['separator']}@smi", $objectSlug)) {
|
|
|
+ if (preg_match("@^{$target}{$config['pathSeparator']}@smi", $objectSlug)) {
|
|
|
$objectSlug = str_replace($target, $slug, $objectSlug);
|
|
|
$meta->getReflectionProperty($config['slug'])->setValue($object, $objectSlug);
|
|
|
$ea->setOriginalObjectProperty($uow, $oid, $config['slug'], $objectSlug);
|
|
@@ -187,8 +166,7 @@ class TreeSlugHandler implements SlugHandlerInterface
|
|
|
array($text, $separator, $object)
|
|
|
);
|
|
|
if (strlen($this->parentSlug)) {
|
|
|
- $options = $this->getOptions($object);
|
|
|
- $slug = $this->parentSlug . $options['separator'] . $slug;
|
|
|
+ $slug = $this->parentSlug . $this->usedPathSeparator . $slug;
|
|
|
}
|
|
|
$this->sluggable->setTransliterator($this->originalTransliterator);
|
|
|
return $slug;
|