TreeSlugHandler.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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. * @package Gedmo.Sluggable.Handler
  16. * @subpackage TreeSlugHandler
  17. * @link http://www.gediminasm.org
  18. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  19. */
  20. class TreeSlugHandler implements SlugHandlerInterface
  21. {
  22. /**
  23. * @var Doctrine\Common\Persistence\ObjectManager
  24. */
  25. protected $om;
  26. /**
  27. * @var Gedmo\Sluggable\SluggableListener
  28. */
  29. protected $sluggable;
  30. /**
  31. * Options for relative slug handler object
  32. * classes
  33. *
  34. * @var array
  35. */
  36. private $options;
  37. /**
  38. * Callable of original transliterator
  39. * which is used by sluggable
  40. *
  41. * @var callable
  42. */
  43. private $originalTransliterator;
  44. /**
  45. * True if node is being inserted
  46. *
  47. * @var boolean
  48. */
  49. private $isInsert = false;
  50. /**
  51. * Transliterated parent slug
  52. *
  53. * @var string
  54. */
  55. private $parentSlug;
  56. /**
  57. * {@inheritDoc}
  58. */
  59. public function __construct(SluggableListener $sluggable)
  60. {
  61. $this->sluggable = $sluggable;
  62. }
  63. /**
  64. * $options = array(
  65. * 'separator' => '/',
  66. * 'parentRelationField' => 'parent'
  67. * )
  68. * {@inheritDoc}
  69. */
  70. public function getOptions($object)
  71. {
  72. $meta = $this->om->getClassMetadata(get_class($object));
  73. if (!isset($this->options[$meta->name])) {
  74. $config = $this->sluggable->getConfiguration($this->om, $meta->name);
  75. $options = $config['handlers'][get_called_class()];
  76. $default = array(
  77. 'separator' => '/'
  78. );
  79. $this->options[$meta->name] = array_merge($default, $options);
  80. }
  81. return $this->options[$meta->name];
  82. }
  83. /**
  84. * {@inheritDoc}
  85. */
  86. public function onChangeDecision(SluggableAdapter $ea, $slugFieldConfig, $object, &$slug, &$needToChangeSlug)
  87. {
  88. $this->om = $ea->getObjectManager();
  89. $this->isInsert = $this->om->getUnitOfWork()->isScheduledForInsert($object);
  90. if (!$this->isInsert && !$needToChangeSlug) {
  91. $changeSet = $ea->getObjectChangeSet($this->om->getUnitOfWork(), $object);
  92. $options = $this->getOptions($object);
  93. if (isset($changeSet[$options['parentRelationField']])) {
  94. $needToChangeSlug = true;
  95. }
  96. }
  97. }
  98. /**
  99. * {@inheritDoc}
  100. */
  101. public function postSlugBuild(SluggableAdapter $ea, array &$config, $object, &$slug)
  102. {
  103. $options = $this->getOptions($object);
  104. $this->originalTransliterator = $this->sluggable->getTransliterator();
  105. $this->sluggable->setTransliterator(array($this, 'transliterate'));
  106. $this->parentSlug = '';
  107. $wrapped = AbstractWrapper::wrapp($object, $this->om);
  108. if ($parent = $wrapped->getPropertyValue($options['parentRelationField'])) {
  109. $parent = AbstractWrapper::wrapp($parent, $this->om);
  110. $this->parentSlug = $parent->getPropertyValue($config['slug']);
  111. }
  112. }
  113. /**
  114. * {@inheritDoc}
  115. */
  116. public static function validate(array $options, ClassMetadata $meta)
  117. {
  118. if (!$meta->isSingleValuedAssociation($options['parentRelationField'])) {
  119. throw new InvalidMappingException("Unable to find tree parent slug relation through field - [{$options['parentRelationField']}] in class - {$meta->name}");
  120. }
  121. }
  122. /**
  123. * {@inheritDoc}
  124. */
  125. public function onSlugCompletion(SluggableAdapter $ea, array &$config, $object, &$slug)
  126. {
  127. if (!$this->isInsert) {
  128. $options = $this->getOptions($object);
  129. $wrapped = AbstractWrapper::wrapp($object, $this->om);
  130. $meta = $wrapped->getMetadata();
  131. $extConfig = $this->sluggable->getConfiguration($this->om, $meta->name);
  132. $config['useObjectClass'] = $extConfig['useObjectClass'];
  133. $target = $wrapped->getPropertyValue($config['slug']);
  134. $config['pathSeparator'] = $options['separator'];
  135. $ea->replaceRelative($object, $config, $target.$options['separator'], $slug);
  136. $uow = $this->om->getUnitOfWork();
  137. // update in memory objects
  138. foreach ($uow->getIdentityMap() as $className => $objects) {
  139. // for inheritance mapped classes, only root is always in the identity map
  140. if ($className !== $wrapped->getRootObjectName()) {
  141. continue;
  142. }
  143. foreach ($objects as $object) {
  144. if (property_exists($object, '__isInitialized__') && !$object->__isInitialized__) {
  145. continue;
  146. }
  147. $oid = spl_object_hash($object);
  148. $objectSlug = $meta->getReflectionProperty($config['slug'])->getValue($object);
  149. if (preg_match("@^{$target}{$options['separator']}@smi", $objectSlug)) {
  150. $objectSlug = str_replace($target, $slug, $objectSlug);
  151. $meta->getReflectionProperty($config['slug'])->setValue($object, $objectSlug);
  152. $ea->setOriginalObjectProperty($uow, $oid, $config['slug'], $objectSlug);
  153. }
  154. }
  155. }
  156. }
  157. }
  158. /**
  159. * Transliterates the slug and prefixes the slug
  160. * by collection of parent slugs
  161. *
  162. * @param string $text
  163. * @param string $separator
  164. * @param object $object
  165. * @return string
  166. */
  167. public function transliterate($text, $separator, $object)
  168. {
  169. $slug = call_user_func_array(
  170. $this->originalTransliterator,
  171. array($text, $separator, $object)
  172. );
  173. if (strlen($this->parentSlug)) {
  174. $options = $this->getOptions($object);
  175. $slug = $this->parentSlug . $options['separator'] . $slug;
  176. }
  177. $this->sluggable->setTransliterator($this->originalTransliterator);
  178. return $slug;
  179. }
  180. }