/** * Returns an underscore-syntaxed version or the CamelCased string. * * @param string $camel_cased_word String to underscore. * * @return string Underscored string. */ public static function underscore($camel_cased_word) { $tmp = $camel_cased_word; $tmp = str_replace('::', '/', $tmp); $tmp = sfToolkit::pregtr($tmp, array('/([A-Z]+)([A-Z][a-z])/' => '\\1_\\2', '/([a-z\\d])([A-Z])/' => '\\1_\\2')); return strtolower($tmp); }
/** * Executes this configuration handler. * * @param array $configFiles An array of absolute filesystem path to a configuration file * * @return string Data to be written to a cache file * * @throws sfConfigurationException If a requested configuration file does not exist or is not readable * @throws sfParseException If a requested configuration file is improperly formatted */ public function execute($configFiles) { // parse the yaml $config = self::getConfiguration($configFiles); // init our data $data = ''; // let's do our fancy work foreach ($config as $file) { if (!is_readable($file)) { // file doesn't exist throw new sfParseException(sprintf('Configuration file "%s" specifies nonexistent or unreadable file "%s".', $configFiles[0], $file)); } $contents = file_get_contents($file); // strip comments (not in debug mode) if (!sfConfig::get('sf_debug')) { $contents = sfToolkit::stripComments($contents); } // strip php tags $contents = sfToolkit::pregtr($contents, array('/^\\s*<\\?(php\\s*)?/m' => '', '/^\\s*\\?>/m' => '')); // replace windows and mac format with unix format $contents = str_replace("\r", "\n", $contents); // replace multiple new lines with a single newline $contents = preg_replace(array('/\\s+$/Sm', '/\\n+/S'), "\n", $contents); // append file data $data .= "\n" . $contents; } // compile data return sprintf("<?php\n" . "// auto-generated by sfCompileConfigHandler\n" . "// date: %s\n" . "%s\n", date('Y/m/d H:i:s'), $data); }
/** * Executes this configuration handler. * * @param array $configFiles An array of absolute filesystem path to a configuration file * * @return string Data to be written to a cache file * * @throws sfConfigurationException If a requested configuration file does not exist or is not readable * @throws sfParseException If a requested configuration file is improperly formatted */ public function execute($configFiles) { // parse the yaml $config = self::getConfiguration($configFiles); // init our data $data = ''; // let's do our fancy work foreach ($config as $file) { if (!is_readable($file)) { // file doesn't exist throw new sfParseException(sprintf('Configuration file "%s" specifies nonexistent or unreadable file "%s".', $configFiles[0], $file)); } $contents = file_get_contents($file); // strip comments (not in debug mode) if (!sfConfig::get('sf_debug')) { $contents = sfToolkit::stripComments($contents); } // insert configuration files /* $contents = preg_replace_callback(array('#(require|include)(_once)?\((sfContext::getInstance\(\)\->getConfigCache\(\)|\$configCache)->checkConfig\(\'config/([^\']+)\'\)\);#m', '#()()(sfContext::getInstance\(\)\->getConfigCache\(\)|\$configCache)->import\(\'config/([^\']+)\'(, false)?\);#m'), array($this, 'insertConfigFileCallback'), $contents); */ // strip php tags $contents = sfToolkit::pregtr($contents, array('/^\\s*<\\?(php)?/m' => '', '/^\\s*\\?>/m' => '')); // replace windows and mac format with unix format $contents = str_replace("\r", "\n", $contents); // replace multiple new lines with a single newline $contents = preg_replace(array('/\\s+$/Sm', '/\\n+/S'), "\n", $contents); // append file data $data .= "\n" . $contents; } // compile data $retval = sprintf("<?php\n" . "// auto-generated by sfCompileConfigHandler\n" . "// date: %s\n%s\n", date('Y/m/d H:i:s'), $data); return $retval; }
function v_simple_format_text($text, $options = array(), $ender = "") { $css = isset($options['class']) ? ' class="' . $options['class'] . '"' : ''; $text = sfToolkit::pregtr($text, array("/(\r\n|\r)/" => "\n", "/\n{2,}/" => "</p><p{$css}>")); // turn two and more newlines into paragraph // turn single newline into <br/> $text = str_replace("\n", "\n<br />", $text); return '<p' . $css . '>' . $text . $ender . '</p>'; // wrap the first and last line in paragraphs before we're done }
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); } } }
/** * Formats a log line. * * @param string $logLine The log line to format * * @return string The formatted log lin */ protected function formatLogLine($logLine) { static $constants; if (!$constants) { foreach (array('sf_app_dir', 'sf_root_dir', 'sf_symfony_lib_dir') as $constant) { $constants[realpath(sfConfig::get($constant)) . DIRECTORY_SEPARATOR] = $constant . DIRECTORY_SEPARATOR; } } // escape HTML $logLine = htmlspecialchars($logLine, ENT_QUOTES, sfConfig::get('sf_charset')); // replace constants value with constant name $logLine = str_replace(array_keys($constants), array_values($constants), $logLine); $logLine = sfToolkit::pregtr($logLine, array('/"(.+?)"/s' => '"<span class="sfWebDebugLogInfo">\\1</span>"', '/^(.+?)\\(\\)\\:/S' => '<span class="sfWebDebugLogInfo">\\1()</span>:', '/line (\\d+)$/' => 'line <span class="sfWebDebugLogInfo">\\1</span>')); // special formatting for SQL lines $logLine = preg_replace('/\\b(SELECT|FROM|AS|LIMIT|ASC|COUNT|DESC|WHERE|LEFT JOIN|INNER JOIN|RIGHT JOIN|ORDER BY|GROUP BY|IN|LIKE|DISTINCT|DELETE|INSERT|INTO|VALUES)\\b/', '<span class="sfWebDebugLogInfo">\\1</span>', $logLine); // remove username/password from DSN if (strpos($logLine, 'DSN') !== false) { $logLine = preg_replace("/=>\\s+'?[^'\\s,]+'?/", "=> '****'", $logLine); } return $logLine; }
/** * Formats a log line. * * @param string $logLine The log line to format * * @return string The formatted log lin */ protected function formatLogLine($logLine) { static $constants; if (!$constants) { foreach (array('sf_app_dir', 'sf_root_dir', 'sf_symfony_lib_dir') as $constant) { $constants[realpath(sfConfig::get($constant)) . DIRECTORY_SEPARATOR] = $constant . DIRECTORY_SEPARATOR; } } // escape HTML $logLine = htmlspecialchars($logLine, ENT_QUOTES, sfConfig::get('sf_charset')); // replace constants value with constant name $logLine = str_replace(array_keys($constants), array_values($constants), $logLine); $logLine = sfToolkit::pregtr($logLine, array('/"(.+?)"/s' => '"<span class="sfWebDebugLogInfo">\\1</span>"', '/^(.+?)\\(\\)\\:/S' => '<span class="sfWebDebugLogInfo">\\1()</span>:', '/line (\\d+)$/' => 'line <span class="sfWebDebugLogInfo">\\1</span>')); // special formatting for SQL lines $logLine = $this->formatSql($logLine); // remove username/password from DSN if (strpos($logLine, 'DSN') !== false) { $logLine = preg_replace("/=>\\s+'?[^'\\s,]+'?/", "=> '****'", $logLine); } return $logLine; }
/** * Executes this configuration handler. * * @param array An array of absolute filesystem path to a configuration file * * @return string Data to be written to a cache file * * @throws sfConfigurationException If a requested configuration file does not exist or is not readable * @throws sfParseException If a requested configuration file is improperly formatted */ public function execute($configFiles) { // parse the yaml $config = array(); foreach ($configFiles as $configFile) { $config = array_merge($config, $this->parseYaml($configFile)); } // init our data $data = ''; // let's do our fancy work foreach ($config as $file) { $file = $this->replaceConstants($file); $file = $this->replacePath($file); if (!is_readable($file)) { // file doesn't exist $error = sprintf('Configuration file "%s" specifies nonexistent or unreadable file "%s"', $configFiles[0], $file); throw new sfParseException($error); } $contents = file_get_contents($file); // strip comments (not in debug mode) if (!sfConfig::get('sf_debug')) { $contents = sfToolkit::stripComments($contents); } // insert configuration files $contents = preg_replace_callback(array('#(require|include)(_once)?\\((sfConfigCache::getInstance\\(\\)|\\$configCache)->checkConfig\\([^_]+sf_app_config_dir_name[^\\.]*\\.\'/([^\']+)\'\\)\\);#m', '#()()(sfConfigCache::getInstance\\(\\)|\\$configCache)->import\\(.sf_app_config_dir_name\\.\'/([^\']+)\'(, false)?\\);#m'), array($this, 'insertConfigFileCallback'), $contents); // strip php tags $contents = sfToolkit::pregtr($contents, array('/^\\s*<\\?(php)?/m' => '', '/^\\s*\\?>/m' => '')); // replace windows and mac format with unix format $contents = str_replace("\r", "\n", $contents); // replace multiple new lines with a single newline $contents = preg_replace(array('/\\s+$/Sm', '/\\n+/S'), "\n", $contents); // append file data $data .= "\n" . $contents; } // compile data $retval = sprintf("<?php\n" . "// auto-generated by sfCompileConfigHandler\n" . "// date: %s\n%s\n", date('Y/m/d H:i:s'), $data); // save current symfony release file_put_contents(sfConfig::get('sf_config_cache_dir') . '/VERSION', file_get_contents(sfConfig::get('sf_symfony_lib_dir') . '/VERSION')); return $retval; }
protected function camelize($text) { return sfToolkit::pregtr($text, array('#/(.?)#e' => "'::'.strtoupper('\\1')", '/(^|_|-)+(.)/e' => "strtoupper('\\2')")); }
public static function stripComments($source) { if (!sfConfig::get('sf_strip_comments', true)) { return $source; } // tokenizer available? if (!function_exists('token_get_all')) { $source = sfToolkit::pregtr($source, array('#/\\*((?!\\*/)[\\d\\D\\s])*\\*/#' => '', '#^\\s*//.*$#m' => '')); // remove // ... return $source; } $output = ''; $tokens = token_get_all($source); foreach ($tokens as $token) { if (is_string($token)) { // simple 1-character token $output .= $token; } else { // token array list($id, $text) = $token; switch ($id) { case T_COMMENT: case T_DOC_COMMENT: // no action on comments break; default: // anything else -> output "as is" $output .= $text; break; } } } return $output; }
function simple_format_text($text, $options = array()) { $css = isset($options['class']) ? ' class="' . $options['class'] . '"' : ''; $text = sfToolkit::pregtr($text, array("/(\r\n|\r)/" => "\n", "/\n{3,}/" => "\n\n", "/\n\n/" => "</p>\\0<p{$css}>", "/([^\n])\n([^\n])/" => "\\1\n<br />\\2")); // turn single newline into <br/> return '<p' . $css . '>' . $text . '</p>'; // wrap the first and last line in paragraphs before we're done }
function simple_format_text($text, $options = array()) { $css = (isset($options['class'])) ? ' class="'.$options['class'].'"' : ''; $text = sfToolkit::pregtr($text, array("/(\r\n|\r)/" => "\n", // lets make them newlines crossplatform "/\n{3,}/" => "\n\n", // zap dupes "/\n\n/" => "</p>\\0<p$css>", // turn two newlines into paragraph "/([^\n])\n([^\n])/" => "\\1\n<br />\\2")); // turn single newline into <br/> return '<p'.$css.'>'.$text.'</p>'; // wrap the first and last line in paragraphs before we're done }
function _labelise($id) { return sfToolkit::pregtr($id, array('#/(.?)#e' => "'::'.strtoupper('\\1')", '/(^|_)(.)/e' => "strtoupper(' \\2')")); }