TreeSlugHandler.php 5.6 KB

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