Quellcode durchsuchen

[sluggable] handlers implemented support for ODM

Gediminas Morkevicius vor 14 Jahren
Ursprung
Commit
63f4e28ddd

+ 1 - 0
lib/Gedmo/Sluggable/Handler/TreeSlugHandler.php

@@ -151,6 +151,7 @@ class TreeSlugHandler implements SlugHandlerInterface
             $extConfig = $this->sluggable->getConfiguration($this->om, $meta->name);
             $config['useObjectClass'] = $extConfig['useObjectClass'];
             $target = $wrapped->getPropertyValue($config['slug']);
+            $config['pathSeparator'] = $this->usedSeparator;
             $ea->replaceRelative($object, $config, $target.$this->usedSeparator, $slug);
             $uow = $this->om->getUnitOfWork();
             // update in memory objects

+ 56 - 2
lib/Gedmo/Sluggable/Mapping/Event/Adapter/ODM.php

@@ -45,18 +45,72 @@ final class ODM extends BaseAdapterODM implements SluggableAdapter
     }
 
     /**
+     * This query can couse some data integrity failures since it does not
+     * execute atomicaly
+     *
      * {@inheritDoc}
      */
     public function replaceRelative($object, array $config, $target, $replacement)
     {
-        throw new \Exception('not implemented yet');
+        $dm = $this->getObjectManager();
+        $meta = $dm->getClassMetadata($config['useObjectClass']);
+
+        $q = $dm
+            ->createQueryBuilder($config['useObjectClass'])
+            ->where("function() {
+                return this.{$config['slug']}.indexOf('{$target}') === 0;
+            }")
+            ->getQuery()
+        ;
+        $q->setHydrate(false);
+        $result = $q->execute();
+        if ($result instanceof Cursor) {
+            $result = $result->toArray();
+            foreach ($result as $targetObject) {
+                $slug = preg_replace("@^{$target}@smi", $replacement.$config['pathSeparator'], $targetObject[$config['slug']]);
+                $dm
+                    ->createQueryBuilder()
+                    ->update($config['useObjectClass'])
+                    ->field($config['slug'])->set($slug)
+                    ->field($meta->identifier)->equals($targetObject['_id'])
+                    ->getQuery()
+                    ->execute()
+                ;
+            }
+        }
     }
 
     /**
+     * This query can couse some data integrity failures since it does not
+     * execute atomicaly
+     *
      * {@inheritDoc}
      */
     public function replaceInverseRelative($object, array $config, $target, $replacement)
     {
-        throw new \Exception('not implemented yet');
+        $dm = $this->getObjectManager();
+        $wrapped = AbstractWrapper::wrapp($object, $dm);
+        $meta = $dm->getClassMetadata($config['useObjectClass']);
+        $q = $dm
+            ->createQueryBuilder($config['useObjectClass'])
+            ->field($config['mappedBy'].'.'.$meta->identifier)->equals($wrapped->getIdentifier())
+            ->getQuery()
+        ;
+        $q->setHydrate(false);
+        $result = $q->execute();
+        if ($result instanceof Cursor) {
+            $result = $result->toArray();
+            foreach ($result as $targetObject) {
+                $slug = preg_replace("@^{$replacement}@smi", $target, $targetObject[$config['slug']]);
+                $dm
+                    ->createQueryBuilder()
+                    ->update($config['useObjectClass'])
+                    ->field($config['slug'])->set($slug)
+                    ->field($meta->identifier)->equals($targetObject['_id'])
+                    ->getQuery()
+                    ->execute()
+                ;
+            }
+        }
     }
 }

+ 70 - 0
tests/Gedmo/Sluggable/Fixture/Document/TreeSlug.php

@@ -0,0 +1,70 @@
+<?php
+
+namespace Sluggable\Fixture\Document;
+
+use Doctrine\ODM\MongoDB\Mapping\Annotations as ODM;
+use Gedmo\Mapping\Annotation as Gedmo;
+
+/**
+ * @ODM\Document
+ */
+class TreeSlug
+{
+    /**
+     * @ODM\Id
+     */
+    private $id;
+
+    /**
+     * @Gedmo\Sluggable(slugField="alias")
+     * @ODM\String
+     */
+    private $title;
+
+    /**
+     * @Gedmo\Slug(handlers={
+     *      @Gedmo\SlugHandler(class="Gedmo\Sluggable\Handler\TreeSlugHandler", options={
+     *          @Gedmo\SlugHandlerOption(name="parentRelation", value="parent"),
+     *          @Gedmo\SlugHandlerOption(name="targetField", value="title"),
+     *          @Gedmo\SlugHandlerOption(name="separator", value="/")
+     *      })
+     * }, separator="-", updatable=true)
+     * @ODM\String
+     */
+    private $alias;
+
+    /**
+     * @ODM\ReferenceOne(targetDocument="TreeSlug")
+     */
+    private $parent;
+
+    public function setParent(TreeSlug $parent = null)
+    {
+        $this->parent = $parent;
+    }
+
+    public function getParent()
+    {
+        return $this->parent;
+    }
+
+    public function getId()
+    {
+        return $this->id;
+    }
+
+    public function setTitle($title)
+    {
+        $this->title = $title;
+    }
+
+    public function getTitle()
+    {
+        return $this->title;
+    }
+
+    public function getSlug()
+    {
+        return $this->alias;
+    }
+}

+ 2 - 2
tests/Gedmo/Sluggable/RelativeSlugHandlerDocumentTest.php

@@ -52,7 +52,7 @@ class RelativeSlugHandlerDocumentTest extends BaseTestCaseMongoODM
         $this->populate();
         $repo = $this->dm->getRepository(self::SLUG);
 
-        /*$thomas = $repo->findOneByTitle('Thomas');
+        $thomas = $repo->findOneByTitle('Thomas');
         $thomas->setTitle('Ninja');
         $this->dm->persist($thomas);
         $this->dm->flush();
@@ -68,7 +68,7 @@ class RelativeSlugHandlerDocumentTest extends BaseTestCaseMongoODM
         $this->assertEquals('martial-arts-test/ninja', $thomas->getSlug());
 
         $jen = $repo->findOneByTitle('Jen');
-        $this->assertEquals('martial-arts-test/jen', $jen->getSlug());*/
+        $this->assertEquals('martial-arts-test/jen', $jen->getSlug());
     }
 
     private function populate()

+ 116 - 0
tests/Gedmo/Sluggable/TreeSlugHandlerDocumentTest.php

@@ -0,0 +1,116 @@
+<?php
+
+namespace Gedmo\Sluggable;
+
+use Tool\BaseTestCaseMongoODM;
+use Doctrine\Common\EventManager;
+use Sluggable\Fixture\Document\TreeSlug;
+
+/**
+ * These are tests for sluggable behavior
+ *
+ * @author Gediminas Morkevicius <gediminas.morkevicius@gmail.com>
+ * @package Gedmo.Sluggable
+ * @link http://www.gediminasm.org
+ * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
+ */
+class TreeSlugHandlerDocumentTest extends BaseTestCaseMongoODM
+{
+    const SLUG = 'Sluggable\\Fixture\\Document\\TreeSlug';
+
+    protected function setUp()
+    {
+        parent::setUp();
+        $evm = new EventManager();
+        $evm->addEventSubscriber(new SluggableListener);
+
+        $this->getMockDocumentManager($evm);
+    }
+
+    public function testSlugGeneration()
+    {
+        $this->populate();
+        $repo = $this->dm->getRepository(self::SLUG);
+
+        $food = $repo->findOneByTitle('Food');
+        $this->assertEquals('food', $food->getSlug());
+
+        $fruits = $repo->findOneByTitle('Fruits');
+        $this->assertEquals('food/fruits', $fruits->getSlug());
+
+        $oranges = $repo->findOneByTitle('Oranges');
+        $this->assertEquals('food/fruits/oranges', $oranges->getSlug());
+
+        $citrons = $repo->findOneByTitle('Citrons');
+        $this->assertEquals('food/fruits/citrons', $citrons->getSlug());
+    }
+
+    public function testSlugUpdates()
+    {
+        $this->populate();
+        $repo = $this->dm->getRepository(self::SLUG);
+
+        $fruits = $repo->findOneByTitle('Fruits');
+        $fruits->setTitle('Fructis');
+
+        $this->dm->persist($fruits);
+        $this->dm->flush();
+
+        $this->assertEquals('food/fructis', $fruits->getSlug());
+
+        $oranges = $repo->findOneByTitle('Oranges');
+        $this->assertEquals('food/fructis/oranges', $oranges->getSlug());
+
+        $citrons = $repo->findOneByTitle('Citrons');
+        $this->assertEquals('food/fructis/citrons', $citrons->getSlug());
+
+        $food = $repo->findOneByTitle('Food');
+        $food->setTitle('Foodissimo');
+
+        $this->dm->persist($food);
+        $this->dm->flush();
+
+        $this->assertEquals('foodissimo', $food->getSlug());
+        $this->assertEquals('foodissimo/fructis/oranges', $oranges->getSlug());
+        $this->assertEquals('foodissimo/fructis/citrons', $citrons->getSlug());
+    }
+
+    private function populate()
+    {
+        $food = new TreeSlug;
+        $food->setTitle('Food');
+        $this->dm->persist($food);
+
+        $fruits = new TreeSlug;
+        $fruits->setTitle('Fruits');
+        $fruits->setParent($food);
+        $this->dm->persist($fruits);
+
+        $vegitables = new TreeSlug;
+        $vegitables->setTitle('Vegitables');
+        $vegitables->setParent($food);
+        $this->dm->persist($vegitables);
+
+        $milk = new TreeSlug;
+        $milk->setTitle('Milk');
+        $milk->setParent($food);
+        $this->dm->persist($milk);
+
+        $meat = new TreeSlug;
+        $meat->setTitle('Meat');
+        $meat->setParent($food);
+        $this->dm->persist($meat);
+
+        $oranges = new TreeSlug;
+        $oranges->setTitle('Oranges');
+        $oranges->setParent($fruits);
+        $this->dm->persist($oranges);
+
+        $citrons = new TreeSlug;
+        $citrons->setTitle('Citrons');
+        $citrons->setParent($fruits);
+        $this->dm->persist($citrons);
+
+        $this->dm->flush();
+    }
+}