Esempio n. 1
0
 /**
  * Generate a list of route details from the route details file. The route
  * builder implements inheritance among route detail specifications. Since
  * all route details from the file share the same namespace the are all
  * added to the route map. This allows alias routes to be found by the
  * framework. 
  *
  * @param   string  $key
  * @return  MvcRouteHandlerInterface
  */
 public static function buildRouteDetails($key)
 {
     $reader = self::$reader;
     if (!$reader) {
         $finder = new FileFinder(AF_LIB_PATH, false);
         $reader = new FileReader($finder);
         self::$reader = $reader;
     }
     $namespace = self::getNamespace($key);
     /*
      * 404 is used as the error code because the fault handler will
      * catch this and use a 404 http reponse for any thing that is not
      * commandline and exit with 404 for commandline
      */
     if (false === $namespace) {
         $err = "could not resolve namespace for route key -({$key})";
         throw new RunTimeException($err, 404);
     }
     /*
      * grap the route detail file form disk and use the route
      * build to create a list of routes from its specifications
      */
     $path = NamespaceParser::parseNs($namespace);
     $file = self::getRouteDetailFilename();
     $path = "{$path}/{$file}";
     $data = $reader->import($path, true);
     $routes = RouteBuilder::buildRoutes($data);
     if (empty($routes)) {
         return false;
     }
     /*
      * Make alias routes visible to the framework
      */
     $routeKeys = array_keys($routes);
     foreach ($routeKeys as $routeKey) {
         if (!self::isRoute($routeKey)) {
             self::addRoute($routeKey, $namespace);
         }
     }
     return $routes;
 }
 /**
  * @param   string  $vendorFile
  * @return  array
  */
 public function discoverPackageTree($path)
 {
     $isBasePath = true;
     if ('/' === $path[0]) {
         $isBasePath = false;
     }
     $finder = new FileFinder('resource', $isBasePath);
     $fileReader = new FileReader($finder);
     $packages = array();
     $topDir = new RecursiveDirectoryIterator($finder->getPath($path));
     $fileReader->setFileFinder(new FileFinder(null, false));
     foreach (new RecursiveIteratorIterator($topDir) as $file) {
         if ('json' !== $file->getExtension()) {
             continue;
         }
         $data = $fileReader->decodeJsonAt($file->getPathName(), true);
         if (null === $data) {
             $info = $file->getPathInfo();
             $dir = $info->getFileName();
             $err = "error parsing the manfiest json file for -({$dir}): ";
             $err .= "{$fileReader->getLastJsonError()}";
             throw new RunTimeException($err);
         }
         if (!isset($data['name'])) {
             $pathName = $file->getPathName();
             $err = 'every resource pkg must have a -(name) property ';
             $err .= "file: -({$pathName})";
             throw new DomainException($err);
         }
         $name = $data['name'];
         if (!is_string($name) || empty($name)) {
             $err = "property -(name) must be a none empty string ";
             $err .= "-({$file->getPathInfo()->getFileName()})";
             throw new RunTimeException($err);
         }
         if (!isset($data['type'])) {
             $err = 'every resource pkg must have a -(type) property ';
             $err .= "defined: none found for -({$name})";
             throw new DomainException($err);
         }
         $type = $data['type'];
         if (!is_string($type) || empty($type)) {
             $err = "appfuel resource -({$name}) json must have a type ";
             $err .= "property as which is a non empty string ";
             throw new DomainException($err);
         }
         if (isset($packages[$type][$name])) {
             $info = $file->getPathInfo();
             $err = "can not build tree -({$type}, {$name}) is already ";
             $err .= "defined -({$info->getPathName()})";
             throw new RunTimeException($err);
         }
         $packages[$type][$name] = $data;
     }
     return $packages;
 }
 /**
  * @return	null
  */
 public static function loadTree()
 {
     $file = self::getTreeFile();
     $finder = self::loadFileFinder();
     $finder->setRootPath($file);
     $reader = new FileReader($finder);
     $data = $reader->decodeJsonAt();
     if (empty($data)) {
         $err = "could not load resource tree or tree is empty ";
         $err .= "-({$finder->getPath()})";
         throw new RunTimeException($err);
     }
     ResourceTree::setTree($data);
     /*
      * we want to reuse the finder so set the root back to the base path
      */
     $finder->setRootPath('');
 }