TreeSlugHandler.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. <?php
  2. namespace Gedmo\Sluggable\Handler;
  3. use Doctrine\Common\Persistence\ObjectManager;
  4. use Gedmo\Sluggable\SluggableListener;
  5. use Gedmo\Sluggable\Mapping\Event\SluggableAdapter;
  6. use Gedmo\Tool\Wrapper\AbstractWrapper;
  7. use Gedmo\Exception\InvalidMappingException;
  8. /**
  9. * Sluggable handler which slugs all parent nodes
  10. * recursively and synchronizes on updates. For instance
  11. * category tree slug could look like "food/fruits/apples"
  12. *
  13. * @author Gediminas Morkevicius <gediminas.morkevicius@gmail.com>
  14. * @package Gedmo.Sluggable.Handler
  15. * @subpackage TreeSlugHandler
  16. * @link http://www.gediminasm.org
  17. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  18. */
  19. class TreeSlugHandler implements SlugHandlerInterface
  20. {
  21. const SEPARATOR = '/';
  22. /**
  23. * @var Doctrine\Common\Persistence\ObjectManager
  24. */
  25. protected $om;
  26. /**
  27. * @var Gedmo\Sluggable\SluggableListener
  28. */
  29. protected $sluggable;
  30. /**
  31. * Callable of original transliterator
  32. * which is used by sluggable
  33. *
  34. * @var callable
  35. */
  36. private $originalTransliterator;
  37. /**
  38. * True if node is being inserted
  39. *
  40. * @var boolean
  41. */
  42. private $isInsert = false;
  43. /**
  44. * Transliterated parent slug
  45. *
  46. * @var string
  47. */
  48. private $parentSlug;
  49. /**
  50. * Used path separator
  51. *
  52. * @var string
  53. */
  54. private $usedPathSeparator;
  55. /**
  56. * {@inheritDoc}
  57. */
  58. public function __construct(SluggableListener $sluggable)
  59. {
  60. $this->sluggable = $sluggable;
  61. }
  62. /**
  63. * {@inheritDoc}
  64. */
  65. public function onChangeDecision(SluggableAdapter $ea, $config, $object, &$slug, &$needToChangeSlug)
  66. {
  67. $this->om = $ea->getObjectManager();
  68. $this->isInsert = $this->om->getUnitOfWork()->isScheduledForInsert($object);
  69. $options = $config['handlers'][get_called_class()];
  70. $this->usedPathSeparator = isset($options['separator']) ? $options['separator'] : self::SEPARATOR;
  71. if (!$this->isInsert && !$needToChangeSlug) {
  72. $changeSet = $ea->getObjectChangeSet($this->om->getUnitOfWork(), $object);
  73. if (isset($changeSet[$options['parentRelationField']])) {
  74. $needToChangeSlug = true;
  75. }
  76. }
  77. }
  78. /**
  79. * {@inheritDoc}
  80. */
  81. public function postSlugBuild(SluggableAdapter $ea, array &$config, $object, &$slug)
  82. {
  83. $options = $config['handlers'][get_called_class()];
  84. $this->originalTransliterator = $this->sluggable->getTransliterator();
  85. $this->sluggable->setTransliterator(array($this, 'transliterate'));
  86. $this->parentSlug = '';
  87. $wrapped = AbstractWrapper::wrapp($object, $this->om);
  88. if ($parent = $wrapped->getPropertyValue($options['parentRelationField'])) {
  89. $parent = AbstractWrapper::wrapp($parent, $this->om);
  90. $this->parentSlug = $parent->getPropertyValue($config['slug']);
  91. }
  92. }
  93. /**
  94. * {@inheritDoc}
  95. */
  96. public static function validate(array $options, $meta)
  97. {
  98. if (!$meta->isSingleValuedAssociation($options['parentRelationField'])) {
  99. throw new InvalidMappingException("Unable to find tree parent slug relation through field - [{$options['parentRelationField']}] in class - {$meta->name}");
  100. }
  101. }
  102. /**
  103. * {@inheritDoc}
  104. */
  105. public function onSlugCompletion(SluggableAdapter $ea, array &$config, $object, &$slug)
  106. {
  107. if (!$this->isInsert) {
  108. $options = $config['handlers'][get_called_class()];
  109. $wrapped = AbstractWrapper::wrapp($object, $this->om);
  110. $meta = $wrapped->getMetadata();
  111. $target = $wrapped->getPropertyValue($config['slug']);
  112. $config['pathSeparator'] = $this->usedPathSeparator;
  113. $ea->replaceRelative($object, $config, $target.$config['pathSeparator'], $slug);
  114. $uow = $this->om->getUnitOfWork();
  115. // update in memory objects
  116. foreach ($uow->getIdentityMap() as $className => $objects) {
  117. // for inheritance mapped classes, only root is always in the identity map
  118. if ($className !== $wrapped->getRootObjectName()) {
  119. continue;
  120. }
  121. foreach ($objects as $object) {
  122. if (property_exists($object, '__isInitialized__') && !$object->__isInitialized__) {
  123. continue;
  124. }
  125. $oid = spl_object_hash($object);
  126. $objectSlug = $meta->getReflectionProperty($config['slug'])->getValue($object);
  127. if (preg_match("@^{$target}{$config['pathSeparator']}@smi", $objectSlug)) {
  128. $objectSlug = str_replace($target, $slug, $objectSlug);
  129. $meta->getReflectionProperty($config['slug'])->setValue($object, $objectSlug);
  130. $ea->setOriginalObjectProperty($uow, $oid, $config['slug'], $objectSlug);
  131. }
  132. }
  133. }
  134. }
  135. }
  136. /**
  137. * Transliterates the slug and prefixes the slug
  138. * by collection of parent slugs
  139. *
  140. * @param string $text
  141. * @param string $separator
  142. * @param object $object
  143. * @return string
  144. */
  145. public function transliterate($text, $separator, $object)
  146. {
  147. $slug = call_user_func_array(
  148. $this->originalTransliterator,
  149. array($text, $separator, $object)
  150. );
  151. if (strlen($this->parentSlug)) {
  152. $slug = $this->parentSlug . $this->usedPathSeparator . $slug;
  153. }
  154. $this->sluggable->setTransliterator($this->originalTransliterator);
  155. return $slug;
  156. }
  157. }