Exemple #1
0
 /**
  * Get blueprint.
  *
  * @param  string  $type  Blueprint type.
  * @return Blueprint
  * @throws \RuntimeException
  */
 public function get($type)
 {
     if (!isset($this->instances[$type])) {
         if (is_string($this->search)) {
             $filename = $this->search . $type . YAML_EXT;
         } else {
             $filename = isset($this->search[$type]) ? $this->search[$type] : '';
         }
         if ($filename && is_file($filename)) {
             $file = CompiledYamlFile::instance($filename);
             $blueprints = $file->content();
         } else {
             // throw new \RuntimeException("Blueprints for '{$type}' cannot be found! {$this->search}{$type}");
             $blueprints = [];
         }
         $blueprint = new Blueprint($type, $blueprints, $this);
         if (isset($blueprints['@extends'])) {
             // Extend blueprint by other blueprints.
             $extends = (array) $blueprints['@extends'];
             foreach ($extends as $extendType) {
                 $blueprint->extend($this->get($extendType));
             }
         }
         $this->instances[$type] = $blueprint;
     }
     return $this->instances[$type];
 }
 /**
  * Get blueprint.
  *
  * @param  string  $type  Blueprint type.
  * @return Blueprint
  * @throws \RuntimeException
  */
 public function get($type)
 {
     if (!isset($this->instances[$type])) {
         if (is_string($this->search)) {
             $filename = $this->search . $type . YAML_EXT;
         } else {
             $filename = isset($this->search[$type]) ? $this->search[$type] : '';
         }
         if ($filename && is_file($filename)) {
             $file = CompiledYamlFile::instance($filename);
             $blueprints = $file->content();
         } else {
             $blueprints = [];
         }
         $blueprint = new Blueprint($type, $blueprints, $this);
         if (isset($blueprints['@extends'])) {
             // Extend blueprint by other blueprints.
             $extends = (array) $blueprints['@extends'];
             if (is_string(key($extends))) {
                 $extends = [$extends];
             }
             foreach ($extends as $extendConfig) {
                 $extendType = !is_string($extendConfig) ? empty($extendConfig['type']) ? false : $extendConfig['type'] : $extendConfig;
                 if (!$extendType) {
                     continue;
                 }
                 $context = is_string($extendConfig) || empty($extendConfig['context']) ? $this : new self(self::getGrav()['locator']->findResource($extendConfig['context']));
                 $blueprint->extend($context->get($extendType));
             }
         }
         $this->instances[$type] = $blueprint;
     }
     return $this->instances[$type];
 }
Exemple #3
0
 /**
  * Get blueprint.
  *
  * @param  string  $type  Blueprint type.
  * @return Blueprint
  * @throws \RuntimeException
  */
 public function get($type)
 {
     if (!isset($this->instances[$type])) {
         if (is_file($this->search . $type . YAML_EXT)) {
             $blueprints = (array) Yaml::parse($this->search . $type . YAML_EXT);
         } else {
             // throw new \RuntimeException("Blueprints for '{$type}' cannot be found! {$this->search}{$type}");
             $blueprints = array();
         }
         $blueprint = new Blueprint($type, $blueprints, $this);
         if (isset($blueprints['@extends'])) {
             // Extend blueprint by other blueprints.
             $extends = (array) $blueprints['@extends'];
             foreach ($extends as $extendType) {
                 $blueprint->extend($this->get($extendType));
             }
         }
         $this->instances[$type] = $blueprint;
     }
     return $this->instances[$type];
 }
Exemple #4
0
 /**
  * Get blueprint.
  *
  * @param  string  $type  Blueprint type.
  * @return Blueprint
  * @throws \RuntimeException
  */
 public function get($type)
 {
     if (!isset($this->instances[$type])) {
         $parents = [];
         if (is_string($this->search)) {
             $filename = $this->search . $type . YAML_EXT;
             // Check if search is a stream and resolve the path.
             if (strpos($filename, '://')) {
                 $grav = static::getGrav();
                 /** @var UniformResourceLocator $locator */
                 $locator = $grav['locator'];
                 $parents = $locator->findResources($filename);
                 $filename = array_shift($parents);
             }
         } else {
             $filename = isset($this->search[$type]) ? $this->search[$type] : '';
         }
         if ($filename && is_file($filename)) {
             $file = CompiledYamlFile::instance($filename);
             $blueprints = $file->content();
         } else {
             $blueprints = [];
         }
         $blueprint = new Blueprint($type, $blueprints, $this);
         if (isset($blueprints['@extends'])) {
             // Extend blueprint by other blueprints.
             $extends = (array) $blueprints['@extends'];
             if (is_string(key($extends))) {
                 $extends = [$extends];
             }
             foreach ($extends as $extendConfig) {
                 $extendType = !is_string($extendConfig) ? empty($extendConfig['type']) ? false : $extendConfig['type'] : $extendConfig;
                 if (!$extendType) {
                     continue;
                 } elseif ($extendType === '@parent') {
                     $parentFile = array_shift($parents);
                     if (!$parentFile || !is_file($parentFile)) {
                         continue;
                     }
                     $blueprints = CompiledYamlFile::instance($parentFile)->content();
                     $parent = new Blueprint($type . '-parent', $blueprints, $this);
                     $blueprint->extend($parent);
                     continue;
                 }
                 if (is_string($extendConfig) || empty($extendConfig['context'])) {
                     $context = $this;
                 } else {
                     // Load blueprints from external context.
                     $array = explode('://', $extendConfig['context'], 2);
                     $scheme = array_shift($array);
                     $path = array_shift($array);
                     if ($path) {
                         $scheme .= '://';
                         $extendType = $path ? "{$path}/{$extendType}" : $extendType;
                     }
                     $context = new self($scheme);
                 }
                 $blueprint->extend($context->get($extendType));
             }
         }
         $this->instances[$type] = $blueprint;
     }
     return $this->instances[$type];
 }