Exemplo n.º 1
0
 /**
  * @param string $value
  *
  * @return mixed
  * @internal
  */
 protected function evaluate($value)
 {
     // Parse command.
     if (is_string($value)) {
         // Format: @command.param
         $cmd = $value;
         $params = array();
     } elseif (is_array($value) && count($value) == 1 && !is_int(key($value))) {
         // Format: @command.param: { attr1: value1, attr2: value2 }
         $cmd = (string) key($value);
         $params = (array) current($value);
     } else {
         $result = [];
         foreach ($value as $key => $val) {
             if (is_int($key)) {
                 $result = $result + $this->evaluate($val)->toArray();
             } else {
                 $result = $result + $this->evaluate([$key => $val])->toArray();
             }
         }
         return new Collection($result);
     }
     // We only evaluate commands which start with @
     if (empty($cmd) || $cmd[0] != '@') {
         return $value;
     }
     /** @var Pages $pages */
     $pages = self::getGrav()['pages'];
     $parts = explode('.', $cmd);
     $current = array_shift($parts);
     $results = new Collection();
     switch ($current) {
         case '@self':
             if (!empty($parts)) {
                 switch ($parts[0]) {
                     case 'modular':
                         // @self.modular: false (alternative to @self.children)
                         if (!empty($params) && $params[0] === false) {
                             $results = $this->children()->nonModular();
                             break;
                         }
                         $results = $this->children()->modular();
                         break;
                     case 'children':
                         $results = $this->children()->nonModular();
                         break;
                     case 'parent':
                         $collection = new Collection();
                         $results = $collection->addPage($this->parent());
                         break;
                     case 'siblings':
                         $results = $this->parent()->children()->remove($this->path());
                         break;
                     case 'descendants':
                         $results = $pages->all($this)->remove($this->path())->nonModular();
                         break;
                 }
             }
             $results = $results->published();
             break;
         case '@page':
             $page = null;
             if (!empty($params)) {
                 $page = $this->find($params[0]);
             }
             // safety check in case page is not found
             if (!isset($page)) {
                 return $results;
             }
             // Handle a @page.descendants
             if (!empty($parts)) {
                 switch ($parts[0]) {
                     case 'self':
                         $results = new Collection();
                         $results = $results->addPage($page);
                         break;
                     case 'descendants':
                         $results = $pages->all($page)->remove($page->path());
                         break;
                     case 'children':
                         $results = $page->children();
                         break;
                 }
             } else {
                 $results = $page->children();
             }
             $results = $results->nonModular()->published();
             break;
         case '@root':
             if (!empty($parts) && $parts[0] == 'descendants') {
                 $results = $pages->all($pages->root())->nonModular()->published();
             } else {
                 $results = $pages->root()->children()->nonModular()->published();
             }
             break;
         case '@taxonomy':
             // Gets a collection of pages by using one of the following formats:
             // @taxonomy.category: blog
             // @taxonomy.category: [ blog, featured ]
             // @taxonomy: { category: [ blog, featured ], level: 1 }
             /** @var Taxonomy $taxonomy_map */
             $taxonomy_map = self::getGrav()['taxonomy'];
             if (!empty($parts)) {
                 $params = [implode('.', $parts) => $params];
             }
             $results = $taxonomy_map->findTaxonomy($params)->published();
             break;
     }
     return $results;
 }