stripComments() public static method

We don't use the PHP php_strip_whitespace() function as we want the content to be readable and well-formatted.
public static stripComments ( string $source ) : string
$source string A PHP string
return string The PHP string with the comments removed
Ejemplo n.º 1
0
 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);
 }
Ejemplo n.º 2
0
 private static function stripComments($content)
 {
     if (class_exists(Kernel::class)) {
         $content = Kernel::stripComments($content);
     }
     return $content;
 }
 protected function addFile($phar, $file, $strip = true)
 {
     $path = str_replace(realpath(__DIR__ . '/../..') . '/', '', $file->getRealPath());
     $content = file_get_contents($file);
     if ($strip) {
         $content = Kernel::stripComments(file_get_contents($file));
     }
     $phar->addFromString($path, $content);
 }
 protected function addFile($phar, $file, $strip = true)
 {
     $path = str_replace(dirname(dirname(__DIR__)) . DIRECTORY_SEPARATOR, '', $file->getRealPath());
     $content = file_get_contents($file);
     if ($strip) {
         $content = Kernel::stripComments($content);
     }
     $phar->addFromString($path, $content);
 }
Ejemplo n.º 5
0
 public function compile($pharFile = 'silex.phar')
 {
     if (file_exists($pharFile)) {
         unlink($pharFile);
     }
     $phar = new \Phar($pharFile, 0, 'Silex');
     $phar->setSignatureAlgorithm(\Phar::SHA1);
     $phar->startBuffering();
     $finder = new Finder();
     $finder->files()->name('*.php')->exclude('tests')->in(__DIR__ . '/..');
     foreach ($finder as $file) {
         $path = str_replace(realpath(__DIR__ . '/../..') . '/', '', realpath($file));
         $content = Kernel::stripComments(file_get_contents($file));
         $phar->addFromString($path, $content);
     }
     // Stubs
     $phar['_cli_stub.php'] = $this->getStub();
     $phar['_web_stub.php'] = $this->getStub();
     $phar->setDefaultStub('_cli_stub.php', '_web_stub.php');
     $phar->stopBuffering();
     // $phar->compressFiles(\Phar::GZ);
     unset($phar);
 }
Ejemplo n.º 6
0
    public function testStripComments()
    {
        $source = <<<'EOF'
<?php

$string = 'string should not be   modified';

$string = 'string should not be

modified';


$heredoc = <<<HD


Heredoc should not be   modified {$a[1+$b]}


HD;

$nowdoc = <<<'ND'


Nowdoc should not be   modified


ND;

/**
 * some class comments to strip
 */
class TestClass
{
    /**
     * some method comments to strip
     */
    public function doStuff()
    {
        // inline comment
    }
}
EOF;
        $expected = <<<'EOF'
<?php
$string = 'string should not be   modified';
$string = 'string should not be

modified';
$heredoc = <<<HD


Heredoc should not be   modified {$a[1+$b]}


HD;
$nowdoc = <<<'ND'


Nowdoc should not be   modified


ND;
class TestClass
{
    public function doStuff()
    {
        }
}
EOF;
        $output = Kernel::stripComments($source);
        // Heredocs are preserved, making the output mixing Unix and Windows line
        // endings, switching to "\n" everywhere on Windows to avoid failure.
        if ('\\' === DIRECTORY_SEPARATOR) {
            $expected = str_replace("\r\n", "\n", $expected);
            $output = str_replace("\r\n", "\n", $output);
        }
        $this->assertEquals($expected, $output);
    }
Ejemplo n.º 7
0
 /**
  * Generates code for the proxies to be attached after the container class.
  *
  * @return string
  */
 private function addProxyClasses()
 {
     /* @var $definitions Definition[] */
     $definitions = array_filter($this->container->getDefinitions(), array($this->getProxyDumper(), 'isProxyCandidate'));
     $code = '';
     $strip = '' === $this->docStar && method_exists('Symfony\\Component\\HttpKernel\\Kernel', 'stripComments');
     foreach ($definitions as $definition) {
         $proxyCode = "\n" . $this->getProxyDumper()->getProxyCode($definition);
         if ($strip) {
             $proxyCode = "<?php\n" . $proxyCode;
             $proxyCode = substr(Kernel::stripComments($proxyCode), 5);
         }
         $code .= $proxyCode;
     }
     return $code;
 }
Ejemplo n.º 8
0
    public function testStripComments()
    {
        if (!function_exists('token_get_all')) {
            $this->markTestSkipped('The function token_get_all() is not available.');
            return;
        }
        $source = <<<'EOF'
<?php

$string = 'string should not be   modified';

$string = 'string should not be

modified';


$heredoc = <<<HD


Heredoc should not be   modified


HD;

$nowdoc = <<<'ND'


Nowdoc should not be   modified


ND;

/**
 * some class comments to strip
 */
class TestClass
{
    /**
     * some method comments to strip
     */
    public function doStuff()
    {
        // inline comment
    }
}
EOF;
        $expected = <<<'EOF'
<?php
$string = 'string should not be   modified';
$string = 'string should not be

modified';
$heredoc = <<<HD


Heredoc should not be   modified


HD;
$nowdoc = <<<'ND'


Nowdoc should not be   modified


ND;
class TestClass
{
    public function doStuff()
    {
        }
}
EOF;
        $output = Kernel::stripComments($source);
        // Heredocs are preserved, making the output mixing Unix and Windows line
        // endings, switching to "\n" everywhere on Windows to avoid failure.
        if (defined('PHP_WINDOWS_VERSION_MAJOR')) {
            $expected = str_replace("\r\n", "\n", $expected);
            $output = str_replace("\r\n", "\n", $output);
        }
        $this->assertEquals($expected, $output);
    }
Ejemplo n.º 9
0
    public function testStripComments()
    {
        if (!function_exists('token_get_all')) {
            $this->markTestSkipped();
            return;
        }
        $source = <<<EOF
<?php

/**
 * some class comments to strip
 */
class TestClass
{
    /**
     * some method comments to strip
     */
    public function doStuff()
    {
        // inline comment
    }
}
EOF;
        $expected = <<<EOF
<?php
class TestClass
{
    public function doStuff()
    {
            }
}
EOF;
        $this->assertEquals($expected, Kernel::stripComments($source));
    }
Ejemplo n.º 10
0
 /**
  * Dumps the service container to PHP code in the cache.
  *
  * @param ConfigCache $cache     The config cache
  * @param ContainerBuilder $container The service container
  * @param string $class     The name of the class to generate
  * @param string $baseClass The name of the container's base class
  */
 protected function dumpContainer(ConfigCache $cache, ContainerBuilder $container, $class, $baseClass)
 {
     // cache the container
     $dumper = new PhpDumper($container);
     $content = $dumper->dump(array('class' => $class, 'base_class' => $baseClass));
     if (!$this->debug) {
         $content = SymfonyKernel::stripComments($content);
     }
     $cache->write($content, $container->getResources());
 }
Ejemplo n.º 11
0
    public function testStripComments()
    {
        if (!function_exists('token_get_all')) {
            $this->markTestSkipped('The function token_get_all() is not available.');
            return;
        }
        $source = <<<'EOF'
<?php

$string = 'string should not be   modified';


$heredoc = <<<HD


Heredoc should not be   modified


HD;

$nowdoc = <<<'ND'


Nowdoc should not be   modified


ND;

/**
 * some class comments to strip
 */
class TestClass
{
    /**
     * some method comments to strip
     */
    public function doStuff()
    {
        // inline comment
    }
}
EOF;
        $expected = <<<'EOF'
<?php
$string = 'string should not be   modified';
$heredoc =
<<<HD


Heredoc should not be   modified


HD;
$nowdoc =
<<<'ND'


Nowdoc should not be   modified


ND;
class TestClass
{
    public function doStuff()
    {
            }
}
EOF;
        $this->assertEquals($expected, Kernel::stripComments($source));
    }