Example #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];
 }
Example #3
0
 /**
  * Load blueprint file.
  *
  * @param  string  $name  Name of the blueprint.
  * @return Blueprint
  */
 protected function loadFile($name)
 {
     $blueprint = new Blueprint($name);
     if (is_array($this->search) || is_object($this->search)) {
         // Page types.
         $blueprint->setOverrides($this->search);
         $blueprint->setContext('blueprints://pages');
     } else {
         $blueprint->setContext($this->search);
     }
     return $blueprint->load()->init();
 }
Example #4
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];
 }
Example #5
0
 /**
  * Extend blueprint with another blueprint.
  *
  * @param Blueprint $extends
  * @param bool $append
  */
 public function extend(Blueprint $extends, $append = false)
 {
     $blueprints = $append ? $this->items : $extends->toArray();
     $appended = $append ? $extends->toArray() : $this->items;
     $bref_stack = array(&$blueprints);
     $head_stack = array($appended);
     do {
         end($bref_stack);
         $bref =& $bref_stack[key($bref_stack)];
         $head = array_pop($head_stack);
         unset($bref_stack[key($bref_stack)]);
         foreach (array_keys($head) as $key) {
             if (isset($key, $bref[$key]) && is_array($bref[$key]) && is_array($head[$key])) {
                 $bref_stack[] =& $bref[$key];
                 $head_stack[] = $head[$key];
             } else {
                 $bref = array_merge($bref, array($key => $head[$key]));
             }
         }
     } while (count($head_stack));
     $this->items = $blueprints;
 }
Example #6
0
 /**
  * Load single configuration file and append it to the correct position.
  *
  * @param  string  $name  Name of the position.
  * @param  array   $files  Files to be loaded.
  */
 protected function loadFile($name, $files)
 {
     // Load blueprint file.
     $blueprint = new Blueprint($files);
     $this->object->embed($name, $blueprint->load()->toArray(), '/', true);
 }
Example #7
0
 /**
  * @return array
  */
 public function toArray()
 {
     return $this->blueprints->toArray();
 }
Example #8
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];
 }