public function upgrade() { $specVersion = sfYaml::getSpecVersion(); $queue = array(); $success = true; $finder = sfFinder::type('file')->name('*.yml')->prune('vendor'); foreach ($finder->in(sfConfig::get('sf_root_dir')) as $file) { // attempt to upgrade booleans $original = file_get_contents($file); $upgraded = sfToolkit::pregtr($original, array('/^([^:]+: +)(?:on|y(?:es)?|\\+)(\\s*(#.*)?)$/im' => '\\1true\\2', '/^([^:]+: +)(?:off|no?|-)(\\s*(#.*)?)$/im' => '\\1false\\2')); try { sfYaml::setSpecVersion('1.1'); $yaml11 = sfYaml::load($original); sfYaml::setSpecVersion('1.2'); $yaml12 = sfYaml::load($upgraded); } catch (Exception $e) { // unable to load the YAML $yaml11 = 'foo'; $yaml12 = 'bar'; } if ($yaml11 == $yaml12) { if ($original != $upgraded) { $this->getFilesystem()->touch($file); file_put_contents($file, $upgraded); } } else { $this->logSection('yaml', 'Unable to upgrade ' . sfDebug::shortenFilePath($file), null, 'ERROR'); // force project to use YAML 1.1 spec if ('1.1' != $specVersion) { $specVersion = '1.1'; $class = sfClassManipulator::fromFile(sfConfig::get('sf_config_dir') . '/ProjectConfiguration.class.php'); $original = $class->getCode(); $modified = $class->wrapMethod('setup', 'sfYaml::setSpecVersion(\'1.1\');'); if ($original != $modified && $this->askConfirmation(array('Unable to convert YAML file:', sfDebug::shortenFilePath($file), '', 'Would you like to force YAML to be parsed with the 1.1 specification? (Y/n)'), 'QUESTION_LARGE')) { $this->logSection('yaml', 'Forcing YAML 1.1 spec'); $this->getFilesystem()->touch($class->getFile()); $class->save(); } else { $this->logBlock(array('Unable to either upgrade YAML files or force 1.1 spec.', '(see UPGRADE_TO_1_3 file for more information)'), 'ERROR_LARGE'); } } $success = false; } } if ($success && '1.1' == $specVersion) { $file = sfConfig::get('sf_config_dir') . '/ProjectConfiguration.class.php'; $original = file_get_contents($file); $modified = preg_replace('/^\\s*sfYaml::setSpecVersion\\(\'1\\.1\'\\);\\n/im', '', $original); if ($original != $modified) { $this->logSection('yaml', 'Removing setting of YAML 1.1 spec'); $this->getFilesystem()->touch($file); file_put_contents($file, $modified); } } }
public function __construct($data) { try { $version = sfYaml::getSpecVersion(); sfYaml::setSpecVersion('1.2'); $this->data = $this->parse($data); sfYaml::setSpecVersion($version); } catch (InvalidArgumentException $e) { $this->errors[] = $e->getMessage(); } }
public function getYaml($entities, $locale) { // Use the Zend copy of this script to prevent class conflicts when RailsYaml is included require_once 'thirdparty/zend_translate_railsyaml/library/Translate/Adapter/thirdparty/sfYaml/lib' . '/sfYamlDumper.php'; // Unflatten array $entitiesNested = array(); foreach ($entities as $entity => $spec) { // Legacy support: Don't count *.ss as namespace $entity = preg_replace('/\\.ss\\./', '___ss.', $entity); $parts = explode('.', $entity); $currLevel =& $entitiesNested; while ($part = array_shift($parts)) { $part = str_replace('___ss', '.ss', $part); if (!isset($currLevel[$part])) { $currLevel[$part] = array(); } $currLevel =& $currLevel[$part]; } $currLevel = $spec[0]; } // Write YAML $oldVersion = sfYaml::getSpecVersion(); sfYaml::setSpecVersion('1.1'); $yamlHandler = new sfYaml(); // TODO Dumper can't handle YAML comments, so the context information is currently discarded $result = $yamlHandler->dump(array($locale => $entitiesNested), 99); sfYaml::setSpecVersion($oldVersion); return $result; }
/** * Evaluates scalars and replaces magic values. * * @param string $scalar * * @return string A YAML string */ protected static function evaluateScalar($scalar) { $scalar = trim($scalar); $trueValues = '1.1' == sfYaml::getSpecVersion() ? array('true', 'on', '+', 'yes', 'y') : array('true'); $falseValues = '1.1' == sfYaml::getSpecVersion() ? array('false', 'off', '-', 'no', 'n') : array('false'); switch (true) { case 'null' == strtolower($scalar): case '' == $scalar: case '~' == $scalar: return null; case 0 === strpos($scalar, '!str'): return (string) substr($scalar, 5); case 0 === strpos($scalar, '! '): return intval(self::parseScalar(substr($scalar, 2))); case 0 === strpos($scalar, '!!php/object:'): return unserialize(substr($scalar, 13)); case ctype_digit($scalar): $raw = $scalar; $cast = intval($scalar); return '0' == $scalar[0] ? octdec($scalar) : ((string) $raw == (string) $cast ? $cast : $raw); case in_array(strtolower($scalar), $trueValues): return true; case in_array(strtolower($scalar), $falseValues): return false; case is_numeric($scalar): return '0x' == $scalar[0] . $scalar[1] ? hexdec($scalar) : floatval($scalar); case 0 == strcasecmp($scalar, '.inf'): case 0 == strcasecmp($scalar, '.NaN'): return -log(0); case 0 == strcasecmp($scalar, '-.inf'): return log(0); case preg_match('/^(-|\\+)?[0-9,]+(\\.[0-9]+)?$/', $scalar): return floatval(str_replace(',', '', $scalar)); case preg_match(self::getTimestampRegex(), $scalar): return strtotime($scalar); default: return (string) $scalar; } }