Esempio n. 1
0
 public function mount(CollectionInterface $collection)
 {
     if ($collection instanceof ApiCollection) {
         $collectionName = $collection->getName();
         if (!is_null($collectionName)) {
             $this->collectionsByName[$collectionName] = $collection;
         }
         $this->collectionsByIdentifier[$collection->getIdentifier()] = $collection;
         /** @var ApiEndpoint $endpoint */
         foreach ($collection->getEndpoints() as $endpoint) {
             $fullIdentifier = $collection->getIdentifier() . ' ' . $endpoint->getIdentifier();
             $this->endpointsByIdentifier[$fullIdentifier] = $endpoint;
         }
     }
     return parent::mount($collection);
 }
Esempio n. 2
0
 /**
  * Mounts a collection of handlers
  *
  * @param CollectionInterface $collection
  *
  * @return $this|\Phalcon\Mvc\Micro
  * @throws Exception
  */
 public function mount(CollectionInterface $collection)
 {
     $mainHandler = null;
     $handlers = null;
     $lazyHandler = null;
     $prefix = null;
     $methods = null;
     $pattern = null;
     $subHandler = null;
     $realHandler = null;
     $prefixedPattern = null;
     $route = null;
     $handler = null;
     $name = null;
     /**
      * Get the main handler
      */
     $mainHandler = $collection->getHandler();
     if (empty($mainHandler)) {
         throw new Exception("Collection requires a main handler");
     }
     $handlers = $collection->getHandlers();
     if (!count($handlers)) {
         throw new Exception("There are no handlers to mount");
     }
     if (is_array($handlers)) {
         /**
          * Check if handler is lazy
          */
         if ($collection->isLazy()) {
             $lazyHandler = new LazyLoader($mainHandler);
         } else {
             $lazyHandler = $mainHandler;
         }
         /**
          * Get the main prefix for the collection
          */
         $prefix = $collection->getPrefix();
         foreach ($handlers as $handler) {
             if (!is_array($handler)) {
                 throw new Exception("One of the registered handlers is invalid");
             }
             $methods = $handler[0];
             $pattern = $handler[1];
             $subHandler = $handler[2];
             $name = $handler[3];
             /**
              * Create a real handler
              */
             $realHandler = array($lazyHandler, $subHandler);
             if (!empty($prefix)) {
                 if ($pattern == "/") {
                     $prefixedPattern = $prefix;
                 } else {
                     $prefixedPattern = $prefix . $pattern;
                 }
             } else {
                 $prefixedPattern = $pattern;
             }
             /**
              * Map the route manually
              */
             $route = $this->map($prefixedPattern, $realHandler);
             if (is_string($methods) && $methods != "" || is_array($methods)) {
                 $route->via($methods);
             }
             if (is_string($name)) {
                 $route->setName($name);
             }
         }
     }
     return $this;
 }