public function testDecoding() { $formatter = new YAML(); $data = array('name' => 'Joe', 'age' => 21, 'employed' => true); $raw = file_get_contents(__DIR__ . '/fixtures/joe.yaml'); $this->assertEquals($data, $formatter->decode($raw)); }
public function decode($data) { $parts = preg_split('/[\\n]*[-]{3}[\\n]/', $data, 3); $yaml = parent::decode($parts[1]); $yaml[$this->contentFieldName] = $parts[2]; return $yaml; }
private function parse_manifest($package_path) { $package_path = preg_replace('/\\/$/', '', $package_path) . '/'; $manifest = YAML::decode_file($package_path . 'package.yml'); if (empty($manifest)) { throw new Exception("package.yml not found in {$package_path}, or unable to parse manifest."); } $package_name = $manifest['name']; if ($this->root == null) { $this->root = $package_name; } if (array_has($this->manifests, $package_name)) { return; } $manifest['path'] = $package_path; $this->manifests[$package_name] = $manifest; foreach ($manifest['sources'] as $i => $path) { $path = $package_path . $path; // this is where we "hook" for possible other replacers. $source = $this->replace_build($package_path, file_get_contents($path)); $descriptor = array(); // get contents of first comment preg_match('/^\\s*\\/\\*\\s*(.*?)\\s*\\*\\//s', $source, $matches); if (!empty($matches)) { // get contents of YAML front matter preg_match('/^-{3}\\s*$(.*?)^(?:-{3}|\\.{3})\\s*$/ms', $matches[1], $matches); if (!empty($matches)) { $descriptor = YAML::decode($matches[1]); } } // populate / convert to array requires and provides $requires = (array) (!empty($descriptor['requires']) ? $descriptor['requires'] : array()); $provides = (array) (!empty($descriptor['provides']) ? $descriptor['provides'] : array()); $file_name = !empty($descriptor['name']) ? $descriptor['name'] : basename($path, '.js'); // "normalization" for requires. Fills up the default package name from requires, if not present. foreach ($requires as $i => $require) { $requires[$i] = implode('/', $this->parse_name($package_name, $require)); } $license = array_get($descriptor, 'license'); $this->packages[$package_name][$file_name] = array_merge($descriptor, array('package' => $package_name, 'requires' => $requires, 'provides' => $provides, 'source' => $source, 'path' => $path, 'package/name' => $package_name . '/' . $file_name, 'license' => empty($license) ? array_get($manifest, 'license') : $license)); } }
public function __construct($manifest_path) { $this->package_path = dirname($manifest_path) . '/'; $this->manifest = YAML::decode_file($manifest_path); $this->files = array(); foreach ($this->manifest['sources'] as $i => $path) { $path = $this->package_path . $path; $file = file_get_contents($path); // yaml header preg_match("/\\/\\*\\s*[-]{3}(.*)[.]{3}\\s*\\*\\//s", $file, $matches); // this is a crappy regexp :) // hack to support unindented lists. hell might break loose. -- Taken from http://github.com/Guille/PluginsKit by Guillermo Rauch $rawYAML = preg_replace('/$([\\s]+)-/m', '$1 -', trim($matches[1])); $descriptor = YAML::decode($rawYAML); // populate / convert to array requires and provides if (!empty($descriptor['requires'])) { if (!is_array($descriptor['requires'])) { $descriptor['requires'] = array($descriptor['requires']); } } else { $descriptor['requires'] = array(); } if (!empty($descriptor['provides'])) { if (!is_array($descriptor['provides'])) { $descriptor['provides'] = array($descriptor['provides']); } } else { $descriptor['provides'] = array(); } if (!array_key_exists('name', $descriptor)) { $descriptor['name'] = basename($path, '.js'); } // Strip out beginning "/" to support `requires: [/Foo, /Bar]` foreach ($descriptor['requires'] as $key => $require) { $descriptor['requires'][$key] = preg_replace('/^\\//', '', $require); } $this->files[$descriptor['name']] = array('description' => $descriptor['description'], 'requires' => $descriptor['requires'], 'provides' => $descriptor['provides'], 'source' => $file, 'path' => $path); } }
private function load_repo($name, $config) { $path = $this->find_path($config['paths']['js']); //grab a recursiveDirectoryIterator and process each file it finds $it = new RecursiveDirectoryIterator($path); foreach (new RecursiveIteratorIterator($it) as $filename => $file) { if ($file->isFile()) { $p = $file->getRealPath(); $source = file_get_contents($p); $descriptor = array(); // get contents of first comment preg_match('/\\s*\\/\\*\\s*(.*?)\\s*\\*\\//s', $source, $matches); if (!empty($matches)) { //echo "<br>Got contents of first comment."; // get contents of YAML front matter preg_match('/^-{3}\\s*$(.*?)^(?:-{3}|\\.{3})\\s*$/ms', $matches[1], $matches); if (!empty($matches)) { $descriptor = YAML::decode($matches[1]); } } // populate / convert to array requires and provides $requires = (array) (!empty($descriptor['requires']) ? $descriptor['requires'] : array()); $provides = (array) (!empty($descriptor['provides']) ? $descriptor['provides'] : array()); $optional = (array) (!empty($descriptor['optional']) ? $descriptor['optional'] : array()); $file_name = $file->getFilename(); // "normalization" for requires. Fills up the default package name from requires, if not present. // and removes any version information foreach ($requires as $i => $require) { $requires[$i] = implode('/', $this->parse_name($name, $require)); } //do same for any optional ones... foreach ($optional as $i => $require) { $optional[$i] = implode('/', $this->parse_name($name, $require)); } $this->repos[$name][$file_name] = array_merge($descriptor, array('repo' => $name, 'requires' => $requires, 'provides' => $provides, 'optional' => $optional, 'path' => $p)); } } }
private function parse_manifest($path) { $pathinfo = pathinfo($path); if (is_dir($path)) { $package_path = $pathinfo['dirname'] . '/' . $pathinfo['basename'] . '/'; if (file_exists($package_path . 'package.yml')) { $manifest_path = $package_path . 'package.yml'; $manifest_format = 'yaml'; } else { if (file_exists($package_path . 'package.yaml')) { $manifest_path = $package_path . 'package.yaml'; $manifest_format = 'yaml'; } else { if (file_exists($package_path . 'package.json')) { $manifest_path = $package_path . 'package.json'; $manifest_format = 'json'; } } } } else { if (file_exists($path)) { $package_path = $pathinfo['dirname'] . '/'; $manifest_path = $package_path . $pathinfo['basename']; $manifest_format = $pathinfo['extension']; } } if ($manifest_format == 'json') { $manifest = json_decode(file_get_contents($manifest_path), true); } else { if ($manifest_format == 'yaml' || $manifest_format == 'yml') { $manifest = YAML::decode_file($manifest_path); } } if (empty($manifest)) { throw new Exception("manifest not found in {$package_path}, or unable to parse manifest."); } $package_name = $manifest['name']; if ($this->root == null) { $this->root = $package_name; } if (array_has($this->manifests, $package_name)) { return; } $manifest['path'] = $package_path; $manifest['manifest'] = $manifest_path; $this->manifests[$package_name] = $manifest; foreach ($manifest['sources'] as $i => $path) { $path = $package_path . $path; // this is where we "hook" for possible other replacers. $source = file_get_contents($path); $descriptor = array(); // get contents of first comment preg_match('/\\/\\*\\s*^---(.*?)^\\.\\.\\.\\s*\\*\\//ms', $source, $matches); if (!empty($matches)) { $descriptor = YAML::decode($matches[0]); } // populate / convert to array requires and provides $requires = (array) (!empty($descriptor['requires']) ? $descriptor['requires'] : array()); $provides = (array) (!empty($descriptor['provides']) ? $descriptor['provides'] : array()); $file_name = !empty($descriptor['name']) ? $descriptor['name'] : basename($path, '.js'); // "normalization" for requires. Fills up the default package name from requires, if not present. foreach ($requires as $i => $require) { $requires[$i] = implode('/', $this->parse_name($package_name, $require)); } $license = array_get($descriptor, 'license'); $this->packages[$package_name][$file_name] = array_merge($descriptor, array('package' => $package_name, 'requires' => $requires, 'provides' => $provides, 'source' => $source, 'path' => $path, 'package/name' => $package_name . '/' . $file_name, 'license' => empty($license) ? array_get($manifest, 'license') : $license)); } }
<?php $demo = false; if (isset($_GET['demo'])) { $demo = $_GET['demo']; // check if the demo is valid if (strpos($demo, '/') !== false || !is_dir(dirname(__FILE__) . '/demos/' . $demo)) { $demo = false; } else { include dirname(__FILE__) . '/libs/yaml.php'; $path = dirname(__FILE__) . '/demos/' . $_GET['demo'] . '/'; $details = file_get_contents($path . 'demo.details'); preg_match('/\\/\\*\\s*^---(.*?)^\\.\\.\\.\\s*\\*\\/(.*)/ms', $details, $matches); $descriptor = array(); if (!empty($matches)) { $descriptor = YAML::decode($matches[1]); $description = $matches[2]; } $html = file_get_contents($path . 'demo.html'); $css = file_get_contents($path . 'demo.css'); $js = file_get_contents($path . 'demo.js'); // Fix links for Request, so they both work here and on jsfiddle $html_demo = preg_replace('/\\/echo\\/(html|json)\\//', 'Request.php', $html); $js_demo = preg_replace('/\\/echo\\/(html|json)\\//', 'Request.php', $js); } } ?> <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>