TreeSlugHandler.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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. * List of node slugs to transliterate
  46. *
  47. * @var array
  48. */
  49. private $parts = array();
  50. /**
  51. * True if node is being inserted
  52. *
  53. * @var boolean
  54. */
  55. private $isInsert = false;
  56. /**
  57. * Used separator for slugs
  58. *
  59. * @var string
  60. */
  61. private $usedSeparator;
  62. /**
  63. * {@inheritDoc}
  64. */
  65. public function __construct(ObjectManager $om, SluggableListener $sluggable)
  66. {
  67. $this->om = $om;
  68. $this->sluggable = $sluggable;
  69. }
  70. /**
  71. * $options = array(
  72. * 'separator' => '/',
  73. * 'parentRelation' => 'parent',
  74. * 'targetField' => 'title'
  75. * )
  76. * {@inheritDoc}
  77. */
  78. public function getOptions($object)
  79. {
  80. $meta = $this->om->getClassMetadata(get_class($object));
  81. if (!isset($this->options[$meta->name])) {
  82. $config = $this->sluggable->getConfiguration($this->om, $meta->name);
  83. $options = $config['handlers'][get_called_class()];
  84. $default = array(
  85. 'separator' => '/'
  86. );
  87. $this->options[$meta->name] = array_merge($default, $options);
  88. }
  89. return $this->options[$meta->name];
  90. }
  91. /**
  92. * {@inheritDoc}
  93. */
  94. public function postSlugBuild(SluggableAdapter $ea, array &$config, $object, &$slug)
  95. {
  96. $options = $this->getOptions($object);
  97. $this->originalTransliterator = $this->sluggable->getTransliterator();
  98. $this->sluggable->setTransliterator(array($this, 'transliterate'));
  99. $this->parts = array();
  100. $this->isInsert = $this->om->getUnitOfWork()->isScheduledForInsert($object);
  101. $wrapped = AbstractWrapper::wrapp($object, $this->om);
  102. if ($this->isInsert) {
  103. do {
  104. $relation = $wrapped->getPropertyValue($options['parentRelation']);
  105. if ($relation) {
  106. $wrappedRelation = AbstractWrapper::wrapp($relation, $this->om);
  107. array_unshift($this->parts, $wrappedRelation->getPropertyValue($options['targetField']));
  108. $wrapped = $wrappedRelation;
  109. }
  110. } while ($relation);
  111. } else {
  112. $this->parts = explode($options['separator'], $wrapped->getPropertyValue($config['slug']));
  113. }
  114. $this->usedSeparator = $options['separator'];
  115. }
  116. /**
  117. * {@inheritDoc}
  118. */
  119. public static function validate(array $options, ClassMetadata $meta)
  120. {
  121. if (!$meta->isSingleValuedAssociation($options['parentRelation'])) {
  122. throw new InvalidMappingException("Unable to find tree parent slug relation through field - [{$options['parentRelation']}] in class - {$meta->name}");
  123. }
  124. /*if (!$meta->isSingleValuedAssociation($options['relation'])) {
  125. throw new InvalidMappingException("Unable to find slug relation through field - [{$options['relation']}] in class - {$meta->name}");
  126. }*/
  127. }
  128. /**
  129. * {@inheritDoc}
  130. */
  131. public function onSlugCompletion(SluggableAdapter $ea, array &$config, $object, &$slug)
  132. {
  133. if (!$this->isInsert) {
  134. $wrapped = AbstractWrapper::wrapp($object, $this->om);
  135. $meta = $wrapped->getMetadata();
  136. $extConfig = $this->sluggable->getConfiguration($this->om, $meta->name);
  137. $config['useObjectClass'] = $extConfig['useObjectClass'];
  138. $target = $wrapped->getPropertyValue($config['slug']);
  139. $ea->replaceRelative($object, $config, $target.$this->usedSeparator, $slug);
  140. $uow = $this->om->getUnitOfWork();
  141. // update in memory objects
  142. foreach ($uow->getIdentityMap() as $className => $objects) {
  143. // for inheritance mapped classes, only root is always in the identity map
  144. if ($className !== $wrapped->getRootObjectName()) {
  145. continue;
  146. }
  147. foreach ($objects as $object) {
  148. if (property_exists($object, '__isInitialized__') && !$object->__isInitialized__) {
  149. continue;
  150. }
  151. $oid = spl_object_hash($object);
  152. $objectSlug = $meta->getReflectionProperty($config['slug'])->getValue($object);
  153. if (preg_match("@^{$target}{$this->usedSeparator}@smi", $objectSlug)) {
  154. $objectSlug = str_replace($target, $slug, $objectSlug);
  155. $meta->getReflectionProperty($config['slug'])->setValue($object, $objectSlug);
  156. $ea->setOriginalObjectProperty($uow, $oid, $config['slug'], $objectSlug);
  157. }
  158. }
  159. }
  160. }
  161. }
  162. /**
  163. * Transliterates the slug and prefixes the slug
  164. * by collection of parent slugs
  165. *
  166. * @param string $text
  167. * @param string $separator
  168. * @param object $object
  169. * @return string
  170. */
  171. public function transliterate($text, $separator, $object)
  172. {
  173. if ($this->isInsert) {
  174. foreach ($this->parts as &$part) {
  175. $part = call_user_func_array(
  176. $this->originalTransliterator,
  177. array($part, $separator, $object)
  178. );
  179. }
  180. } else {
  181. array_pop($this->parts);
  182. }
  183. $this->parts[] = call_user_func_array(
  184. $this->originalTransliterator,
  185. array($text, $separator, $object)
  186. );
  187. $this->sluggable->setTransliterator($this->originalTransliterator);
  188. return implode($this->usedSeparator, $this->parts);
  189. }
  190. }