public static stripComments ( string $source ) : string | ||
$source | string | A PHP string |
Результат | string | The PHP string with the comments removed |
static public function stripComments($source)
{
if (!function_exists('token_get_all')) {
return $source;
}
$output = '';
foreach (token_get_all($source) as $token) {
if (is_string($token)) {
$output .= $token;
} elseif (!in_array($token[0], array(T_COMMENT, T_DOC_COMMENT))) {
$output .= $token[1];
}
}
// replace multiple new lines with a single newline
$output = preg_replace(array('/\s+$/Sm', '/\n+/S'), "\n", $output);
// reformat {} "a la python"
$output = preg_replace(array('/\n\s*\{/', '/\n\s*\}/'), array(' {', ' }'), $output);
return $output;
}
public function compile() { if (file_exists('symfony.phar')) { unlink('symfony.phar'); } $phar = new \Phar('symfony.phar', 0, 'Symfony'); $phar->setSignatureAlgorithm(\Phar::SHA1); $phar->startBuffering(); // Files foreach ($this->getFiles() as $file) { $path = str_replace(__DIR__ . '/', '', $file); if (false !== strpos($file, '.php')) { $content = Kernel::stripComments(file_get_contents($file)); } else { $content = file_get_contents($file); } $phar->addFromString($path, $content); } // Stubs $phar['_cli_stub.php'] = $this->getCliStub(); $phar['_web_stub.php'] = $this->getWebStub(); $phar->setDefaultStub('_cli_stub.php', '_web_stub.php'); $phar->stopBuffering(); //$phar->compressFiles(\Phar::GZ); unset($phar); }