Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Added ability to get configured metadata classes.

  • Loading branch information...
commit 9c74000dd342cba709724377c1d16d2f64a9451d 1 parent a965c62
Jordan Stout authored December 15, 2012
36  src/Metadata/AdvancedMetadataFactoryInterface.php
... ...
@@ -0,0 +1,36 @@
  1
+<?php
  2
+
  3
+/*
  4
+ * Copyright 2011 Johannes M. Schmitt <schmittjoh@gmail.com>
  5
+ *
  6
+ * Licensed under the Apache License, Version 2.0 (the "License");
  7
+ * you may not use this file except in compliance with the License.
  8
+ * You may obtain a copy of the License at
  9
+ *
  10
+ * http://www.apache.org/licenses/LICENSE-2.0
  11
+ *
  12
+ * Unless required by applicable law or agreed to in writing, software
  13
+ * distributed under the License is distributed on an "AS IS" BASIS,
  14
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15
+ * See the License for the specific language governing permissions and
  16
+ * limitations under the License.
  17
+ */
  18
+
  19
+namespace Metadata;
  20
+
  21
+/**
  22
+ * Interface for advanced Metadata Factory implementations.
  23
+ *
  24
+ * @author Johannes M. Schmitt <schmittjoh@gmail.com>
  25
+ * @author Jordan Stout <j@jrdn.org>
  26
+ */
  27
+interface AdvancedMetadataFactoryInterface extends MetadataFactoryInterface
  28
+{
  29
+    /**
  30
+     * Gets all the possible classes.
  31
+     *
  32
+     * @throws \RuntimeException if driver does not an advanced driver.
  33
+     * @return array
  34
+     */
  35
+    public function getAllClassNames();
  36
+}
17  src/Metadata/Driver/AbstractFileDriver.php
@@ -7,8 +7,11 @@
7 7
  *
8 8
  * @author Johannes M. Schmitt <schmittjoh@gmail.com>
9 9
  */
10  
-abstract class AbstractFileDriver implements DriverInterface
  10
+abstract class AbstractFileDriver implements AdvancedDriverInterface
11 11
 {
  12
+    /**
  13
+     * @var FileLocatorInterface|FileLocator
  14
+     */
12 15
     private $locator;
13 16
 
14 17
     public function __construct(FileLocatorInterface $locator)
@@ -26,6 +29,18 @@ public function loadMetadataForClass(\ReflectionClass $class)
26 29
     }
27 30
 
28 31
     /**
  32
+     * {@inheritDoc}
  33
+     */
  34
+    public function getAllClassNames()
  35
+    {
  36
+        if (!$this->locator instanceof AdvancedFileLocatorInterface) {
  37
+            throw new \RuntimeException('Locator "%s" must be an instance of "AdvancedFileLocatorInterface".');
  38
+        }
  39
+
  40
+        return $this->locator->findAllClasses($this->getExtension());
  41
+    }
  42
+
  43
+    /**
29 44
      * Parses the content of the file, and converts it to the desired metadata.
30 45
      *
31 46
      * @param  string             $file
34  src/Metadata/Driver/AdvancedDriverInterface.php
... ...
@@ -0,0 +1,34 @@
  1
+<?php
  2
+
  3
+/*
  4
+ * Copyright 2011 Johannes M. Schmitt <schmittjoh@gmail.com>
  5
+ *
  6
+ * Licensed under the Apache License, Version 2.0 (the "License");
  7
+ * you may not use this file except in compliance with the License.
  8
+ * You may obtain a copy of the License at
  9
+ *
  10
+ * http://www.apache.org/licenses/LICENSE-2.0
  11
+ *
  12
+ * Unless required by applicable law or agreed to in writing, software
  13
+ * distributed under the License is distributed on an "AS IS" BASIS,
  14
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15
+ * See the License for the specific language governing permissions and
  16
+ * limitations under the License.
  17
+ */
  18
+
  19
+namespace Metadata\Driver;
  20
+
  21
+/**
  22
+ * Forces advanced logic to drivers.
  23
+ *
  24
+ * @author Jordan Stout <j@jrdn.org>
  25
+ */
  26
+interface AdvancedDriverInterface extends DriverInterface
  27
+{
  28
+    /**
  29
+     * Gets all the metadata class names known to this driver.
  30
+     *
  31
+     * @return array
  32
+     */
  33
+    public function getAllClassNames();
  34
+}
36  src/Metadata/Driver/AdvancedFileLocatorInterface.php
... ...
@@ -0,0 +1,36 @@
  1
+<?php
  2
+
  3
+/*
  4
+ * Copyright 2011 Johannes M. Schmitt <schmittjoh@gmail.com>
  5
+ *
  6
+ * Licensed under the Apache License, Version 2.0 (the "License");
  7
+ * you may not use this file except in compliance with the License.
  8
+ * You may obtain a copy of the License at
  9
+ *
  10
+ * http://www.apache.org/licenses/LICENSE-2.0
  11
+ *
  12
+ * Unless required by applicable law or agreed to in writing, software
  13
+ * distributed under the License is distributed on an "AS IS" BASIS,
  14
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15
+ * See the License for the specific language governing permissions and
  16
+ * limitations under the License.
  17
+ */
  18
+
  19
+namespace Metadata\Driver;
  20
+
  21
+/**
  22
+ * Forces advanced logic on a file locator.
  23
+ *
  24
+ * @author Jordan Stout <j@jrdn.org>
  25
+ */
  26
+interface AdvancedFileLocatorInterface extends FileLocatorInterface
  27
+{
  28
+    /**
  29
+     * Finds all possible metadata files.
  30
+     *
  31
+     * @param string $extension
  32
+     *
  33
+     * @return array
  34
+     */
  35
+    public function findAllClasses($extension);
  36
+}
27  src/Metadata/Driver/DriverChain.php
@@ -18,7 +18,7 @@
18 18
 
19 19
 namespace Metadata\Driver;
20 20
 
21  
-final class DriverChain implements DriverInterface
  21
+final class DriverChain implements AdvancedDriverInterface
22 22
 {
23 23
     private $drivers;
24 24
 
@@ -37,4 +37,29 @@ public function loadMetadataForClass(\ReflectionClass $class)
37 37
 
38 38
         return null;
39 39
     }
  40
+
  41
+    /**
  42
+     * {@inheritDoc}
  43
+     */
  44
+    public function getAllClassNames()
  45
+    {
  46
+        $classes = array();
  47
+        foreach ($this->drivers as $driver) {
  48
+            if (!$driver instanceof AdvancedDriverInterface) {
  49
+                throw new \RuntimeException(
  50
+                    sprintf(
  51
+                        'Driver "%s" must be an instance of "AdvancedDriverInterface" to use '.
  52
+                        '"DriverChain::getAllClassNames()".',
  53
+                        get_class($driver)
  54
+                    )
  55
+                );
  56
+            }
  57
+            $driverClasses = $driver->getAllClassNames();
  58
+            if (!empty($driverClasses)) {
  59
+                $classes = array_merge($classes, $driverClasses);
  60
+            }
  61
+        }
  62
+
  63
+        return $classes;
  64
+    }
40 65
 }
26  src/Metadata/Driver/FileLocator.php
@@ -2,7 +2,7 @@
2 2
 
3 3
 namespace Metadata\Driver;
4 4
 
5  
-class FileLocator implements FileLocatorInterface
  5
+class FileLocator implements AdvancedFileLocatorInterface
6 6
 {
7 7
     private $dirs;
8 8
 
@@ -35,4 +35,28 @@ public function findFileForClass(\ReflectionClass $class, $extension)
35 35
 
36 36
         return null;
37 37
     }
  38
+
  39
+    /**
  40
+     * {@inheritDoc}
  41
+     */
  42
+    public function findAllClasses($extension)
  43
+    {
  44
+        $classes = array();
  45
+        foreach ($this->dirs as $prefix => $dir) {
  46
+            /** @var $iterator \RecursiveIteratorIterator|\SplFileInfo[] */
  47
+            $iterator = new \RecursiveIteratorIterator(
  48
+                new \RecursiveDirectoryIterator($dir),
  49
+                \RecursiveIteratorIterator::LEAVES_ONLY
  50
+            );
  51
+            foreach ($iterator as $file) {
  52
+                if (($fileName = $file->getBasename('.'.$extension)) == $file->getBasename()) {
  53
+                    continue;
  54
+                }
  55
+
  56
+                $classes[] = ($prefix !== '' ? $prefix.'\\' : '').str_replace('.', '\\', $fileName);
  57
+            }
  58
+        }
  59
+
  60
+        return $classes;
  61
+    }
38 62
 }
17  src/Metadata/MetadataFactory.php
@@ -18,10 +18,11 @@
18 18
 
19 19
 namespace Metadata;
20 20
 
  21
+use Metadata\Driver\AdvancedDriverInterface;
21 22
 use Metadata\Driver\DriverInterface;
22 23
 use Metadata\Cache\CacheInterface;
23 24
 
24  
-final class MetadataFactory implements MetadataFactoryInterface
  25
+final class MetadataFactory implements AdvancedMetadataFactoryInterface
25 26
 {
26 27
     private $driver;
27 28
     private $cache;
@@ -99,6 +100,20 @@ public function getMetadataForClass($className)
99 100
     }
100 101
 
101 102
     /**
  103
+     * {@inheritDoc}
  104
+     */
  105
+    public function getAllClassNames()
  106
+    {
  107
+        if (!$this->driver instanceof AdvancedDriverInterface) {
  108
+            throw new \RuntimeException(
  109
+                sprintf('Driver "%s" must be an instance of "AdvancedDriverInterface".', get_class($this->driver))
  110
+            );
  111
+        }
  112
+
  113
+        return $this->driver->getAllClassNames();
  114
+    }
  115
+
  116
+    /**
102 117
      * @param ClassMetadata $toAdd
103 118
      */
104 119
     private function addClassMetadata(&$metadata, $toAdd)
85  tests/Metadata/Tests/Driver/AbstractFileDriverTest.php
... ...
@@ -0,0 +1,85 @@
  1
+<?php
  2
+
  3
+namespace Metadata\Tests\Driver;
  4
+
  5
+use Metadata\ClassMetadata;
  6
+
  7
+/**
  8
+ * @author Jordan Stout <j@jrdn.org>
  9
+ */
  10
+class AbstractFileDriverTest extends \PHPUnit_Framework_TestCase
  11
+{
  12
+    private static $extension = 'jms_metadata.yml';
  13
+
  14
+    /** @var \PHPUnit_Framework_MockObject_MockObject */
  15
+    private $locator;
  16
+
  17
+    /** @var \PHPUnit_Framework_MockObject_MockObject */
  18
+    private $driver;
  19
+
  20
+    public function setUp()
  21
+    {
  22
+        $this->locator = $this->getMock('Metadata\Driver\FileLocator', array(), array(), '', false);
  23
+        $this->driver = $this->getMockBuilder('Metadata\Driver\AbstractFileDriver')
  24
+            ->setConstructorArgs(array($this->locator))
  25
+            ->getMockForAbstractClass();
  26
+
  27
+        $this->driver->expects($this->any())->method('getExtension')->will($this->returnValue(self::$extension));
  28
+    }
  29
+
  30
+    public function testLoadMetadataForClass()
  31
+    {
  32
+        $class = new \ReflectionClass('\stdClass');
  33
+        $this->locator
  34
+            ->expects($this->once())
  35
+            ->method('findFileForClass')
  36
+            ->with($class, self::$extension)
  37
+            ->will($this->returnValue('Some\Path'));
  38
+
  39
+        $this->driver
  40
+            ->expects($this->once())
  41
+            ->method('loadMetadataFromFile')
  42
+            ->with($class, 'Some\Path')
  43
+            ->will($this->returnValue($metadata = new ClassMetadata('\stdClass')));
  44
+
  45
+        $this->assertSame($metadata, $this->driver->loadMetadataForClass($class));
  46
+    }
  47
+
  48
+    public function testLoadMetadataForClassWillReturnNull()
  49
+    {
  50
+        $class = new \ReflectionClass('\stdClass');
  51
+        $this->locator
  52
+            ->expects($this->once())
  53
+            ->method('findFileForClass')
  54
+            ->with($class, self::$extension)
  55
+            ->will($this->returnValue(null));
  56
+
  57
+        $this->assertSame(null, $this->driver->loadMetadataForClass($class));
  58
+    }
  59
+
  60
+    public function testGetAllClassNames()
  61
+    {
  62
+        $class = new \ReflectionClass('\stdClass');
  63
+        $this->locator
  64
+            ->expects($this->once())
  65
+            ->method('findAllClasses')
  66
+            ->with(self::$extension)
  67
+            ->will($this->returnValue(array('\stdClass')));
  68
+
  69
+        $this->assertSame(array('\stdClass'), $this->driver->getAllClassNames($class));
  70
+    }
  71
+
  72
+    public function testGetAllClassNamesThrowsRuntimeException()
  73
+    {
  74
+        $this->setExpectedException('RuntimeException');
  75
+
  76
+        $locator = $this->getMock('Metadata\Driver\FileLocatorInterface', array(), array(), '', false);
  77
+        $driver = $this->getMockBuilder('Metadata\Driver\AbstractFileDriver')
  78
+            ->setConstructorArgs(array($locator))
  79
+            ->getMockForAbstractClass();
  80
+        $class = new \ReflectionClass('\stdClass');
  81
+        $locator->expects($this->never())->method('findAllClasses');
  82
+
  83
+        $driver->getAllClassNames($class);
  84
+    }
  85
+}
28  tests/Metadata/Tests/Driver/DriverChainTest.php
@@ -20,6 +20,25 @@ public function testLoadMetadataForClass()
20 20
         $this->assertSame($metadata, $chain->loadMetadataForClass(new \ReflectionClass('\stdClass')));
21 21
     }
22 22
 
  23
+    public function testGetAllClassNames()
  24
+    {
  25
+        $driver1 = $this->getMock('Metadata\\Driver\\AdvancedDriverInterface');
  26
+        $driver1
  27
+            ->expects($this->once())
  28
+            ->method('getAllClassNames')
  29
+            ->will($this->returnValue(array('Foo')));
  30
+
  31
+        $driver2 = $this->getMock('Metadata\\Driver\\AdvancedDriverInterface');
  32
+        $driver2
  33
+            ->expects($this->once())
  34
+            ->method('getAllClassNames')
  35
+            ->will($this->returnValue(array('Bar')));
  36
+
  37
+        $chain = new DriverChain(array($driver1, $driver2));
  38
+
  39
+        $this->assertSame(array('Foo', 'Bar'), $chain->getAllClassNames());
  40
+    }
  41
+
23 42
     public function testLoadMetadataForClassReturnsNullWhenNoMetadataIsFound()
24 43
     {
25 44
         $driver = new DriverChain(array());
@@ -34,4 +53,13 @@ public function testLoadMetadataForClassReturnsNullWhenNoMetadataIsFound()
34 53
         $driverChain = new DriverChain(array($driver));
35 54
         $this->assertNull($driver->loadMetadataForClass(new \ReflectionClass('\stdClass')));
36 55
     }
  56
+
  57
+    public function testGetAllClassNamesThrowsException()
  58
+    {
  59
+        $this->setExpectedException('RuntimeException');
  60
+        $driver = $this->getMock('Metadata\\Driver\\DriverInterface');
  61
+        $driver->expects($this->never())->method('getAllClassNames');
  62
+        $chain = new DriverChain(array($driver));
  63
+        $chain->getAllClassNames();
  64
+    }
37 65
 }
18  tests/Metadata/Tests/Driver/FileLocatorTest.php
@@ -48,4 +48,22 @@ public function testFindFileForGlobalNamespacedClass()
48 48
         $ref = new \ReflectionClass('D');
49 49
         $this->assertEquals(realpath(__DIR__.'/Fixture/D/D.yml'), realpath($locator->findFileForClass($ref, 'yml')));
50 50
     }
  51
+
  52
+    public function testFindAllFiles()
  53
+    {
  54
+        $locator = new FileLocator(array(
  55
+            'Metadata\Tests\Driver\Fixture\A' => __DIR__.'/Fixture/A',
  56
+            'Metadata\Tests\Driver\Fixture\B' => __DIR__.'/Fixture/B',
  57
+            'Metadata\Tests\Driver\Fixture\C' => __DIR__.'/Fixture/C',
  58
+            'Metadata\Tests\Driver\Fixture\D' => __DIR__.'/Fixture/D'
  59
+        ));
  60
+
  61
+        $this->assertCount(1, $xmlFiles = $locator->findAllClasses('xml'));
  62
+        $this->assertSame('Metadata\Tests\Driver\Fixture\A\A', $xmlFiles[0]);
  63
+
  64
+        $this->assertCount(3, $ymlFiles = $locator->findAllClasses('yml'));
  65
+        $this->assertSame('Metadata\Tests\Driver\Fixture\B\B', $ymlFiles[0]);
  66
+        $this->assertSame('Metadata\Tests\Driver\Fixture\C\SubDir\C', $ymlFiles[1]);
  67
+        $this->assertSame('Metadata\Tests\Driver\Fixture\D\D', $ymlFiles[2]);
  68
+    }
51 69
 }
21  tests/Metadata/Tests/MetadataFactoryTest.php
@@ -3,9 +3,7 @@
3 3
 namespace Metadata\Tests;
4 4
 
5 5
 use Metadata\PropertyMetadata;
6  
-
7 6
 use Metadata\MergeableClassMetadata;
8  
-
9 7
 use Metadata\ClassMetadata;
10 8
 use Metadata\MetadataFactory;
11 9
 
@@ -181,4 +179,23 @@ public function testGetMetadataWithInterfaces()
181 179
 
182 180
         $factory->getMetadataForClass('Metadata\Tests\Fixtures\ComplexHierarchy\SubClassA');
183 181
     }
  182
+
  183
+    public function testGetAllClassNames()
  184
+    {
  185
+        $driver = $this->getMock('Metadata\Driver\AdvancedDriverInterface');
  186
+        $driver
  187
+            ->expects($this->once())
  188
+            ->method('getAllClassNames')
  189
+            ->will($this->returnValue(array()));
  190
+
  191
+        $factory = new MetadataFactory($driver);
  192
+        $this->assertSame(array(), $factory->getAllClassNames());
  193
+    }
  194
+
  195
+    public function testGetAllClassNamesThrowsException()
  196
+    {
  197
+        $this->setExpectedException('RuntimeException');
  198
+        $factory = new MetadataFactory($this->getMock('Metadata\Driver\DriverInterface'));
  199
+        $factory->getAllClassNames();
  200
+    }
184 201
 }

0 notes on commit 9c74000

Please sign in to comment.
Something went wrong with that request. Please try again.