/** * {@inheritDoc} */ public function onChangeDecision(SluggableAdapter $ea, array &$config, $object, &$slug, &$needToChangeSlug) { $om = $ea->getObjectManager(); $isInsert = $om->getUnitOfWork()->isScheduledForInsert($object); $this->options = array_merge(array('separator' => '-', 'dateField' => 'date', 'format' => 'Y', 'check_included' => true), $config['handlers'][get_called_class()]); if (!$isInsert && !$needToChangeSlug) { $changeSet = $ea->getObjectChangeSet($om->getUnitOfWork(), $object); if (isset($changeSet[$this->options['dateField']])) { $needToChangeSlug = true; } } }
/** * {@inheritDoc} */ public function onChangeDecision(SluggableAdapter $ea, array &$config, $object, &$slug, &$needToChangeSlug) { $this->om = $ea->getObjectManager(); $isInsert = $this->om->getUnitOfWork()->isScheduledForInsert($object); $this->usedOptions = $config['handlers'][get_called_class()]; if (!isset($this->usedOptions['separator'])) { $this->usedOptions['separator'] = self::SEPARATOR; } if (!$isInsert && !$needToChangeSlug) { $changeSet = $ea->getObjectChangeSet($this->om->getUnitOfWork(), $object); if (isset($changeSet[$this->usedOptions['relationField']])) { $needToChangeSlug = true; } } }
/** * {@inheritDoc} */ public function onSlugCompletion(SluggableAdapter $ea, array &$config, $object, &$slug) { if (!$slug) { return; } $newSlug = $slug; $repository = $ea->getObjectManager()->getRepository(get_class($object)); $usedOptions = $config['handlers'][get_called_class()]; $results = $repository->createQueryBuilder('c')->where("c." . $usedOptions['field'] . " LIKE :term")->andWhere("c.id != :id")->orderBy('c.' . $usedOptions['field'], 'ASC')->setParameter('term', $slug . '%')->setParameter('id', $object->getId())->getQuery()->getResult(); $i = 1; foreach ($results as $content) { if ($content->getSlug() == $newSlug) { $newSlug = $slug . $usedOptions['separator'] . $i; $i++; } } $slug = $newSlug; }
/** * {@inheritDoc} */ public function onSlugCompletion(SluggableAdapter $ea, array &$config, $object, &$slug) { $this->om = $ea->getObjectManager(); $isInsert = $this->om->getUnitOfWork()->isScheduledForInsert($object); if (!$isInsert) { $options = $config['handlers'][get_called_class()]; $wrapped = AbstractWrapper::wrapp($object, $this->om); $oldSlug = $wrapped->getPropertyValue($config['slug']); $mappedByConfig = $this->sluggable->getConfiguration($this->om, $options['relationClass']); if ($mappedByConfig) { $meta = $this->om->getClassMetadata($options['relationClass']); if (!$meta->isSingleValuedAssociation($options['mappedBy'])) { throw new InvalidMappingException("Unable to find " . $wrapped->getMetadata()->name . " relation - [{$options['mappedBy']}] in class - {$meta->name}"); } if (!isset($mappedByConfig['slugs'][$options['inverseSlugField']])) { throw new InvalidMappingException("Unable to find slug field - [{$options['inverseSlugField']}] in class - {$meta->name}"); } $mappedByConfig['slug'] = $mappedByConfig['slugs'][$options['inverseSlugField']]['slug']; $mappedByConfig['mappedBy'] = $options['mappedBy']; $ea->replaceInverseRelative($object, $mappedByConfig, $slug, $oldSlug); $uow = $this->om->getUnitOfWork(); // update in memory objects foreach ($uow->getIdentityMap() as $className => $objects) { // for inheritance mapped classes, only root is always in the identity map if ($className !== $mappedByConfig['useObjectClass']) { continue; } foreach ($objects as $object) { if (property_exists($object, '__isInitialized__') && !$object->__isInitialized__) { continue; } $oid = spl_object_hash($object); $objectSlug = $meta->getReflectionProperty($mappedByConfig['slug'])->getValue($object); if (preg_match("@^{$oldSlug}@smi", $objectSlug)) { $objectSlug = str_replace($oldSlug, $slug, $objectSlug); $meta->getReflectionProperty($mappedByConfig['slug'])->setValue($object, $objectSlug); $ea->setOriginalObjectProperty($uow, $oid, $mappedByConfig['slug'], $objectSlug); } } } } } }
/** * {@inheritDoc} */ public function onChangeDecision(SluggableAdapter $ea, $slugFieldConfig, $object, &$slug, &$needToChangeSlug) { $this->om = $ea->getObjectManager(); $isInsert = $this->om->getUnitOfWork()->isScheduledForInsert($object); if (!$isInsert && !$needToChangeSlug) { $changeSet = $ea->getObjectChangeSet($this->om->getUnitOfWork(), $object); $options = $this->getOptions($object); if (isset($changeSet[$options['relationField']])) { $needToChangeSlug = true; } } }
/** * Generates the unique slug * * @param SluggableAdapter $ea * @param object $object * @param string $preferedSlug * @param array $config[$slugField] * @return string - unique slug */ private function makeUniqueSlug(SluggableAdapter $ea, $object, $preferedSlug, $recursing = false, $config = array()) { $om = $ea->getObjectManager(); $meta = $om->getClassMetadata(get_class($object)); if (count($config) == 0) { $config = $this->getConfiguration($om, $meta->name); } // search for similar slug $result = $ea->getSimilarSlugs($object, $meta, $config, $preferedSlug); // add similar persisted slugs into account $result += $this->getSimilarPersistedSlugs($config['useObjectClass'], $preferedSlug, $config['slug']); // leave only right slugs if (!$recursing) { $this->filterSimilarSlugs($result, $config, $preferedSlug); } if ($result) { $generatedSlug = $preferedSlug; $sameSlugs = array(); foreach ((array) $result as $list) { $sameSlugs[] = $list[$config['slug']]; } $i = pow(10, $this->exponent); do { $generatedSlug = $preferedSlug . $config['separator'] . $i++; } while (in_array($generatedSlug, $sameSlugs)); $mapping = $meta->getFieldMapping($config['slug']); if (isset($mapping['length']) && strlen($generatedSlug) > $mapping['length']) { $generatedSlug = substr($generatedSlug, 0, $mapping['length'] - (strlen($i) + strlen($config['separator']))); $this->exponent = strlen($i) - 1; $generatedSlug = $this->makeUniqueSlug($ea, $object, $generatedSlug, true, $config); } $preferedSlug = $generatedSlug; } return $preferedSlug; }
/** * Generates the unique slug * * @param SluggableAdapter $ea * @param object $object * @param string $preferedSlug * @param boolean $recursing * @param array $config[$slugField] * @return string - unique slug */ private function makeUniqueSlug(SluggableAdapter $ea, $object, $preferedSlug, $recursing = false, $config = array()) { $om = $ea->getObjectManager(); $meta = $om->getClassMetadata(get_class($object)); $similarPersisted = array(); // extract unique base $base = false; if ($config['unique'] && isset($config['unique_base'])) { $base = $meta->getReflectionProperty($config['unique_base'])->getValue($object); } // collect similar persisted slugs during this flush if (isset($this->persisted[$class = $ea->getRootObjectClass($meta)])) { foreach ($this->persisted[$class] as $obj) { if ($base !== false && $meta->getReflectionProperty($config['unique_base'])->getValue($obj) !== $base) { continue; // if unique_base field is not the same, do not take slug as similar } $slug = $meta->getReflectionProperty($config['slug'])->getValue($obj); if (preg_match("@^{$preferedSlug}.*@smi", $slug)) { $similarPersisted[] = array($config['slug'] => $slug); } } } // load similar slugs $result = array_merge((array) $ea->getSimilarSlugs($object, $meta, $config, $preferedSlug), $similarPersisted); // leave only right slugs if (!$recursing) { // filter similar slugs foreach ($result as $key => $similar) { if (!preg_match("@{$preferedSlug}(\$|{$config['separator']}[\\d]+\$)@smi", $similar[$config['slug']])) { unset($result[$key]); } } } if ($result) { $generatedSlug = $preferedSlug; $sameSlugs = array(); foreach ((array) $result as $list) { $sameSlugs[] = $list[$config['slug']]; } $i = pow(10, $this->exponent); do { $generatedSlug = $preferedSlug . $config['separator'] . $i++; } while (in_array($generatedSlug, $sameSlugs)); $mapping = $meta->getFieldMapping($config['slug']); if (isset($mapping['length']) && strlen($generatedSlug) > $mapping['length']) { $generatedSlug = substr($generatedSlug, 0, $mapping['length'] - (strlen($i) + strlen($config['separator']))); $this->exponent = strlen($i) - 1; if (substr($generatedSlug, -strlen($config['separator'])) == $config['separator']) { $generatedSlug = substr($generatedSlug, 0, strlen($generatedSlug) - strlen($config['separator'])); } $generatedSlug = $this->makeUniqueSlug($ea, $object, $generatedSlug, true, $config); } $preferedSlug = $generatedSlug; } return $preferedSlug; }
/** * {@inheritDoc} */ public function onSlugCompletion(SluggableAdapter $ea, array &$config, $object, &$slug) { if (!$this->isInsert) { $options = $this->getOptions($object); $wrapped = AbstractWrapper::wrapp($object, $this->om); $meta = $wrapped->getMetadata(); $extConfig = $this->sluggable->getConfiguration($this->om, $meta->name); $config['useObjectClass'] = $extConfig['useObjectClass']; $target = $wrapped->getPropertyValue($config['slug']); $config['pathSeparator'] = $options['separator']; $ea->replaceRelative($object, $config, $target.$options['separator'], $slug); $uow = $this->om->getUnitOfWork(); // update in memory objects foreach ($uow->getIdentityMap() as $className => $objects) { // for inheritance mapped classes, only root is always in the identity map if ($className !== $wrapped->getRootObjectName()) { continue; } foreach ($objects as $object) { if (property_exists($object, '__isInitialized__') && !$object->__isInitialized__) { continue; } $oid = spl_object_hash($object); $objectSlug = $meta->getReflectionProperty($config['slug'])->getValue($object); if (preg_match("@^{$target}{$options['separator']}@smi", $objectSlug)) { $objectSlug = str_replace($target, $slug, $objectSlug); $meta->getReflectionProperty($config['slug'])->setValue($object, $objectSlug); $ea->setOriginalObjectProperty($uow, $oid, $config['slug'], $objectSlug); } } } } }