/**
  * Parse a block of YAML into PHP
  *
  * @param string  $yaml  YAML-formatted string to parse
  * @param string  $mode  Parsing mode to use
  * @return array
  */
 public static function parse($yaml, $mode = null)
 {
     // start measuring
     $hash = Debug::markStart('parsing', 'yaml');
     $mode = $mode ? $mode : self::getMode();
     switch ($mode) {
         case 'loose':
             $result = Spyc::YAMLLoad($yaml);
             break;
         case 'strict':
             $result = sYAML::parse($yaml);
             break;
         case 'transitional':
             try {
                 $result = sYaml::parse($yaml);
             } catch (Exception $e) {
                 Log::error($e->getMessage() . ' Falling back to loose mode.', 'core', 'yaml');
                 $result = Spyc::YAMLLoad($yaml);
             }
             break;
         default:
             // check for new lines, is this a file?
             $has_newline = strpos($yaml, "\n") !== false;
             if (!$has_newline && File::exists($yaml)) {
                 // seems like it is
                 $yaml = File::get($yaml);
             }
             $result = Statamic\Dipper\Dipper::parse($yaml);
     }
     // end measuring
     Debug::markEnd($hash);
     Debug::increment('parses', 'yaml');
     return $result;
 }
示例#2
0
 /**
  * Parse a block of YAML into PHP
  *
  * @param string  $yaml  YAML-formatted string to parse
  * @param string  $mode  Parsing mode to use
  * @return array
  */
 public static function parse($yaml, $mode = null)
 {
     // start measuring
     $hash = Debug::markStart('parsing', 'yaml');
     $mode = $mode ? $mode : self::getMode();
     switch ($mode) {
         case 'loose':
             $result = Spyc::YAMLLoad($yaml);
             break;
         case 'strict':
             $result = sYAML::parse($yaml);
             break;
         case 'transitional':
             try {
                 $result = sYaml::parse($yaml);
             } catch (Exception $e) {
                 Log::error($e->getMessage() . ' Falling back to loose mode.', 'core', 'yaml');
                 $result = Spyc::YAMLLoad($yaml);
             }
             break;
         default:
             $result = Spyc::YAMLLoad($yaml);
     }
     // end measuring
     Debug::markEnd($hash);
     Debug::increment('parses', 'yaml');
     return $result;
 }
示例#3
0
 public function partial()
 {
     $start = time();
     $src = $this->fetchParam('src', null, null, false, false);
     $extensions = array(".html", ".md", ".markdown", ".textile");
     $html = null;
     // measurement
     $hash = Debug::markStart('partials', $src, $start);
     Debug::increment('partials', $src);
     if ($src) {
         foreach ($extensions as $extension) {
             $full_src = Path::assemble(BASE_PATH, $this->theme_root, 'partials', ltrim($src . $extension, '/'));
             if (File::exists($full_src)) {
                 Statamic_View::$_dataStore = $this->attributes + Statamic_View::$_dataStore;
                 if ($this->fetchParam('use_context', false, false, true, false)) {
                     // to use context, we only want to pass the attributes as
                     // the current data, as those will override into the context
                     // set of data; if we were to include all of ::$_dataStore,
                     // we run into the issue where all of the global-level variables
                     // are overriding variables in context, when the variables in
                     // context are more accurate scope-wise at this point in the parse
                     $html = Parse::contextualTemplate(file_get_contents($full_src), $this->attributes, $this->context, array('statamic_view', 'callback'), true);
                 } else {
                     $html = Parse::template(file_get_contents($full_src), Statamic_View::$_dataStore);
                 }
                 // parse contents if needed
                 if ($extension == ".md" || $extension == ".markdown") {
                     $html = Parse::markdown($html);
                 } elseif ($extension == ".textile") {
                     $html = Parse::textile($html);
                 }
             }
         }
         if (Config::get('enable_smartypants', TRUE)) {
             $html = Parse::smartypants($html);
         }
     }
     Debug::markEnd($hash);
     return $html;
 }
示例#4
0
 /**
  * Builds a file with YAML front-matter
  *
  * @param array  $data  Front-matter data
  * @param string  $content  Content
  * @return string
  */
 public static function buildContent(array $data, $content)
 {
     Debug::increment('content', 'files_built');
     $file_content = "---\n";
     $file_content .= preg_replace('/\\A^---\\s/ism', "", YAML::dump($data));
     $file_content .= "---\n";
     $file_content .= $content;
     return $file_content;
 }
 /**
  * Empty the specified directory of all files and folders.
  *
  * @param  string|array  $directory folder(s) to empty
  * @return void
  */
 public static function wipe($directory)
 {
     Debug::increment('folders', 'emptied');
     self::delete($directory, TRUE);
 }
示例#6
0
 /**
  * Parses a conditions string
  *
  * @param string  $conditions  Conditions to parse
  * @return array
  */
 public static function conditions($conditions)
 {
     // start measuring
     $hash = Debug::markStart('parsing', 'conditions');
     Debug::increment('parses', 'condition_statements');
     $replacement = '__TEMP_COMMA_' . substr(md5(time()), 0, 12) . '__';
     $conditions = explode(",", str_replace('\\,', $replacement, $conditions));
     $output = array();
     foreach ($conditions as $condition) {
         Debug::increment('parses', 'conditions');
         $result = Parse::condition(str_replace($replacement, ',', $condition));
         $output[$result['key']] = $result['value'];
     }
     // end measuring
     Debug::markEnd($hash);
     return $output;
 }
 /**
  * Takes a scope-notated key and finds the value for it in the given
  * array or object.
  *
  * @param  string       $key     Dot-notated key to find
  * @param  array|object $data    Array or object to search
  * @param  mixed        $default Default value to use if not found
  * @return mixed
  */
 protected function getVariable($key, $data, $default = null)
 {
     // <statamic>
     // detect modifiers
     $modifiers = null;
     if (strpos($key, "|") !== false) {
         $parts = explode("|", $key);
         $key = $parts[0];
         $modifiers = array_splice($parts, 1);
     }
     // </statamic>
     if (strpos($key, $this->scopeGlue) === false) {
         $parts = explode('.', $key);
     } else {
         $parts = explode($this->scopeGlue, $key);
     }
     foreach ($parts as $key_part) {
         if (is_array($data)) {
             if (!array_key_exists($key_part, $data)) {
                 return $default;
             }
             $data = $data[$key_part];
         } elseif (is_object($data)) {
             if (!isset($data->{$key_part})) {
                 return $default;
             }
             $data = $data->{$key_part};
         } else {
             return $default;
         }
     }
     // <statamic>
     // execute modifier chain
     if ($modifiers) {
         foreach ($modifiers as $mod) {
             $now = time();
             if (strpos($mod, ":") === false) {
                 $modifier = $mod;
                 $modifier_params = array();
             } else {
                 $parts = explode(":", $mod);
                 $modifier = $parts[0];
                 $modifier_params = array_splice($parts, 1);
             }
             $hash = \Debug::markStart('modifiers', $modifier, $now);
             try {
                 // load modifier
                 $modifier_obj = \Resource::loadModifier(\Parse::modifierAlias($modifier));
                 // ensure method exists
                 if (!method_exists($modifier_obj, "index")) {
                     throw new \Exception("Improperly formatted modifier object.");
                 }
                 // call method
                 $data = $modifier_obj->index($data, $modifier_params);
                 \Debug::increment('modifiers', $modifier);
             } catch (\Exception $e) {
                 // do nothing
             }
             \Debug::markEnd($hash);
         }
     }
     // </statamic>
     return $data;
 }
示例#8
0
 /**
  * callback
  * Attempts to load a plugin?
  *
  * @param string $name
  * @param array $attributes
  * @param string $content
  * @param array $context
  * @return string
  * @throws Exception
  */
 public static function callback($name, $attributes, $content, $context = array())
 {
     $now = time();
     $output = false;
     $pos = strpos($name, ':');
     # single function plugins
     if ($pos === false) {
         $plugin = $name;
         $call = "index";
     } else {
         $plugin = substr($name, 0, $pos);
         $call = substr($name, $pos + 1);
     }
     // mark start of debug timing
     $hash = Debug::markStart('plugins', $plugin . ':' . $call, $now);
     // if nothing to call, abort
     if (!$call) {
         Debug::markEnd($hash);
         return null;
     }
     try {
         // will throw an exception if resource isn't available
         $plugin_obj = Resource::loadPlugin($plugin);
         if (!is_callable(array($plugin_obj, $call))) {
             throw new Exception('Method not callable.');
         }
         $plugin_obj->attributes = $attributes;
         $plugin_obj->content = $content;
         $plugin_obj->context = $context;
         Debug::increment('plugins', $plugin);
         $output = call_user_func(array($plugin_obj, $call));
         if (is_array($output)) {
             $output = Parse::template($content, $output);
         }
     } catch (\Slim\Exception\Stop $e) {
         // allow plugins to halt the app
         throw $e;
     } catch (ResourceNotFoundException $e) {
         // resource not found, do nothing
     } catch (Exception $e) {
         // everything else, do nothing if debug is off
         if (Config::get('debug')) {
             throw $e;
         }
     }
     Debug::markEnd($hash);
     return $output;
 }