InversedRelativeSlugHandler.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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. use Doctrine\Common\Persistence\Mapping\ClassMetadata;
  9. /**
  10. * Sluggable handler which should be used for inversed relation mapping
  11. * used together with RelativeSlugHandler. Updates back related slug on
  12. * relation changes
  13. *
  14. * @author Gediminas Morkevicius <gediminas.morkevicius@gmail.com>
  15. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  16. */
  17. class InversedRelativeSlugHandler implements SlugHandlerInterface
  18. {
  19. /**
  20. * @var ObjectManager
  21. */
  22. protected $om;
  23. /**
  24. * @var SluggableListener
  25. */
  26. protected $sluggable;
  27. /**
  28. * $options = array(
  29. * 'relationClass' => 'objectclass',
  30. * 'inverseSlugField' => 'slug',
  31. * 'mappedBy' => 'relationField'
  32. * )
  33. * {@inheritDoc}
  34. */
  35. public function __construct(SluggableListener $sluggable)
  36. {
  37. $this->sluggable = $sluggable;
  38. }
  39. /**
  40. * {@inheritDoc}
  41. */
  42. public function onChangeDecision(SluggableAdapter $ea, $slugFieldConfig, $object, &$slug, &$needToChangeSlug)
  43. {}
  44. /**
  45. * {@inheritDoc}
  46. */
  47. public function postSlugBuild(SluggableAdapter $ea, array &$config, $object, &$slug)
  48. {}
  49. /**
  50. * {@inheritDoc}
  51. */
  52. public static function validate(array $options, ClassMetadata $meta)
  53. {
  54. if (!isset($options['relationClass']) || !strlen($options['relationClass'])) {
  55. throw new InvalidMappingException("'relationClass' option must be specified for object slug mapping - {$meta->name}");
  56. }
  57. if (!isset($options['mappedBy']) || !strlen($options['mappedBy'])) {
  58. throw new InvalidMappingException("'mappedBy' option must be specified for object slug mapping - {$meta->name}");
  59. }
  60. if (!isset($options['inverseSlugField']) || !strlen($options['inverseSlugField'])) {
  61. throw new InvalidMappingException("'inverseSlugField' option must be specified for object slug mapping - {$meta->name}");
  62. }
  63. }
  64. /**
  65. * {@inheritDoc}
  66. */
  67. public function onSlugCompletion(SluggableAdapter $ea, array &$config, $object, &$slug)
  68. {
  69. $this->om = $ea->getObjectManager();
  70. $isInsert = $this->om->getUnitOfWork()->isScheduledForInsert($object);
  71. if (!$isInsert) {
  72. $options = $config['handlers'][get_called_class()];
  73. $wrapped = AbstractWrapper::wrap($object, $this->om);
  74. $oldSlug = $wrapped->getPropertyValue($config['slug']);
  75. $mappedByConfig = $this->sluggable->getConfiguration(
  76. $this->om,
  77. $options['relationClass']
  78. );
  79. if ($mappedByConfig) {
  80. $meta = $this->om->getClassMetadata($options['relationClass']);
  81. if (!$meta->isSingleValuedAssociation($options['mappedBy'])) {
  82. throw new InvalidMappingException("Unable to find ".$wrapped->getMetadata()->name." relation - [{$options['mappedBy']}] in class - {$meta->name}");
  83. }
  84. if (!isset($mappedByConfig['slugs'][$options['inverseSlugField']])) {
  85. throw new InvalidMappingException("Unable to find slug field - [{$options['inverseSlugField']}] in class - {$meta->name}");
  86. }
  87. $mappedByConfig['slug'] = $mappedByConfig['slugs'][$options['inverseSlugField']]['slug'];
  88. $mappedByConfig['mappedBy'] = $options['mappedBy'];
  89. $ea->replaceInverseRelative($object, $mappedByConfig, $slug, $oldSlug);
  90. $uow = $this->om->getUnitOfWork();
  91. // update in memory objects
  92. foreach ($uow->getIdentityMap() as $className => $objects) {
  93. // for inheritance mapped classes, only root is always in the identity map
  94. if ($className !== $mappedByConfig['useObjectClass']) {
  95. continue;
  96. }
  97. foreach ($objects as $object) {
  98. if (property_exists($object, '__isInitialized__') && !$object->__isInitialized__) {
  99. continue;
  100. }
  101. $oid = spl_object_hash($object);
  102. $objectSlug = $meta->getReflectionProperty($mappedByConfig['slug'])->getValue($object);
  103. if (preg_match("@^{$oldSlug}@smi", $objectSlug)) {
  104. $objectSlug = str_replace($oldSlug, $slug, $objectSlug);
  105. $meta->getReflectionProperty($mappedByConfig['slug'])->setValue($object, $objectSlug);
  106. $ea->setOriginalObjectProperty($uow, $oid, $mappedByConfig['slug'], $objectSlug);
  107. }
  108. }
  109. }
  110. }
  111. }
  112. }
  113. /**
  114. * {@inheritDoc}
  115. */
  116. public function handlesUrlization()
  117. {
  118. return false;
  119. }
  120. }