Beispiel #1
0
 /**
  * Actually serve the routes. Called by route() above.
  *
  * @param array $route
  * @param array $args
  * @return mixed
  * @throws FallbackLoop
  * @throws \Error
  */
 protected function serve(array $route, array $args = [])
 {
     static $calledOnce = null;
     if (count($route) === 1) {
         $route[] = 'index';
     }
     try {
         $class_name = Gears::getName('Landing__' . $route[0]);
     } catch (GearNotFound $ex) {
         $class_name = '\\Airship\\Cabin\\' . self::$active_cabin . '\\Landing\\' . $route[0];
     }
     $method = $route[1];
     if (!\class_exists($class_name)) {
         $state = State::instance();
         $state->logger->error('Landing Error: Class not found when invoked from router', ['route' => ['class' => $class_name, 'method' => $method]]);
         $calledOnce = true;
         return $this->serveFallback();
     }
     // Load our cabin-specific landing
     $landing = new $class_name();
     if (!$landing instanceof Landing) {
         throw new \Error(\__("%s is not a Landing", "default", $class_name));
     }
     // Dependency injection with a twist
     $landing->airshipEjectFromCockpit($this->lens, $this->databases, self::$patternPrefix);
     // Tighten the Bolts!
     \Airship\tightenBolts($landing);
     if (!\method_exists($landing, $method)) {
         if ($calledOnce) {
             throw new FallbackLoop(\trk('errors.router.fallback_loop'));
         }
         $calledOnce = true;
         return $this->serveFallback();
     }
     return $landing->{$method}(...$args);
 }
Beispiel #2
0
 /**
  * Grab a blueprint
  *
  * @param string $name
  * @param mixed[] ...$cArgs Constructor arguments
  * @return Blueprint
  * @throws InvalidType
  */
 protected function blueprint(string $name, ...$cArgs) : Blueprint
 {
     if (!empty($cArgs)) {
         $cache = Util::hash($name . ':' . \json_encode($cArgs));
     } else {
         $cArgs = [];
         $cache = Util::hash($name . '[]');
     }
     if (!isset($this->_cache['blueprints'][$cache])) {
         // CACHE MISS. We need to build it, then!
         $db = $this->airshipChooseDB();
         if ($db instanceof DBInterface) {
             \array_unshift($cArgs, $db);
         }
         try {
             $class = Gears::getName('Blueprint__' . $name);
         } catch (GearNotFound $ex) {
             if ($name[0] === '\\') {
                 // If you pass a \Absolute\Namespace, we will just use it.
                 $class = $name;
             } else {
                 // We default to \Current\Application\Namespace\Blueprint\NameHere.
                 $x = \explode('\\', $this->getNamespace());
                 \array_pop($x);
                 $class = \implode('\\', $x) . '\\Blueprint\\' . $name;
             }
         }
         $this->_cache['blueprints'][$cache] = new $class(...$cArgs);
         if (!$this->_cache['blueprints'][$cache] instanceof Blueprint) {
             throw new InvalidType(\trk('errors.type.wrong_class', 'Blueprint'));
         }
         \Airship\tightenBolts($this->_cache['blueprints'][$cache]);
     }
     return $this->_cache['blueprints'][$cache];
 }