示例#1
0
 public function register(Application $app)
 {
     $app['model'] = function (Application $app) {
         $manager = new Manager();
         $manager->setFinder($app['model.finder']);
         $storageHandlerServices = $app['model.storage.handlers'];
         if (!is_array($storageHandlerServices)) {
             throw new \InvalidArgumentException("Service 'model.storage.handlers' must be an array of storage handlers");
         }
         if (!count($storageHandlerServices)) {
             throw new \InvalidArgumentException("Service 'model.storage.handlers' contains no storage handler service(s). Please register atleast one.");
         }
         foreach ($storageHandlerServices as $storageHandlerName => $storageHandlerService) {
             if (is_string($storageHandlerService)) {
                 $storageHandlerService = $app[$storageHandlerService];
             }
             $manager->setModelStorageHandler($storageHandlerService, $storageHandlerName);
         }
         $storageMap = isset($app['model.storage.handler.map']) ? $app['model.storage.handler.map'] : array();
         $manager->setModelToStorageHandlerMap($storageMap);
         return $manager;
     };
     $app['model.finder'] = function (Application $app) {
         $finder = new Finder\FileFinder();
         $finder->setNamespace($app['model.finder.namespace']);
         $finder->setBasePath($app['model.finder.path']);
         return $finder;
     };
 }
示例#2
0
 /**
  * test getModelClassList method
  */
 public function testGetModelClassList()
 {
     $mockPath = '/mock-existent-path';
     $mockClassList = array('model.class.a' => 'Mock\\Namespace\\ModelClassA', 'model.class.b' => 'Mock\\Namespace\\ModelClassB', 'model.class.c' => 'Mock\\Namespace\\ModelClassC');
     $mockFileList = array(new \SplFileInfo($mockPath . '/ModelClassA'), new \SplFileInfo($mockPath . '/ModelClassB'), new \SplFileInfo($mockPath . '/ModelClassC'));
     $mockIterator = new \ArrayIterator($mockFileList);
     $finder = new FileFinder();
     $finder->setNamespace('Mock\\Namespace');
     $finder->setBasePath($mockPath);
     $symfonyFinder = $this->getMock('Symfony\\Component\\Finder\\Finder', array('files', 'in', 'name', 'getIterator'), array(), '', FALSE);
     $symfonyFinder->expects($this->once())->method('files')->will($this->returnValue($symfonyFinder));
     $symfonyFinder->expects($this->once())->method('in')->will($this->returnCallback(function ($basePath) use($symfonyFinder) {
         $this->assertEquals('/mock-existent-path', $basePath);
         return $symfonyFinder;
     }));
     $symfonyFinder->expects($this->once())->method('name')->will($this->returnCallback(function ($filter) use($symfonyFinder) {
         $this->assertEquals('*.php', $filter);
         return $symfonyFinder;
     }));
     $symfonyFinder->expects($this->once())->method('getIterator')->will($this->returnValue($mockIterator));
     $finder->setFileFinder($symfonyFinder);
     $classList = $finder->getModelClassList();
     $this->assertEquals($mockIterator->count(), count($classList));
     foreach ($mockClassList as $key => $classPath) {
         $this->assertArrayHasKey($key, $classList);
         $this->assertEquals($classPath, $classList[$key]);
     }
 }