4 * This file is part of Psy Shell.
6 * (c) 2012-2018 Justin Hileman
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
14 use Symfony\Component\Finder\Finder;
17 * A Psy Shell Phar compiler.
24 * Compiles psysh into a single phar file.
26 * @param string $pharFile The full path to the file to create
28 public function compile($pharFile = 'psysh.phar')
30 if (file_exists($pharFile)) {
34 $this->version = Shell::VERSION;
36 $phar = new \Phar($pharFile, 0, 'psysh.phar');
37 $phar->setSignatureAlgorithm(\Phar::SHA1);
39 $phar->startBuffering();
41 $finder = Finder::create()
45 ->notName('Compiler.php')
48 foreach ($finder as $file) {
49 $this->addFile($phar, $file);
52 $finder = Finder::create()
60 ->in(dirname(__DIR__) . '/build-vendor');
62 foreach ($finder as $file) {
63 $this->addFile($phar, $file);
67 $phar->setStub($this->getStub());
69 $phar->stopBuffering();
75 * Add a file to the psysh Phar.
78 * @param \SplFileInfo $file
79 * @param bool $strip (default: true)
81 private function addFile($phar, $file, $strip = true)
83 $path = str_replace(dirname(__DIR__) . DIRECTORY_SEPARATOR, '', $file->getRealPath());
85 $content = file_get_contents($file);
87 $content = $this->stripWhitespace($content);
88 } elseif ('LICENSE' === basename($file)) {
89 $content = "\n" . $content . "\n";
92 $phar->addFromString($path, $content);
96 * Removes whitespace from a PHP source string while preserving line numbers.
98 * @param string $source A PHP string
100 * @return string The PHP string with the whitespace removed
102 private function stripWhitespace($source)
104 if (!function_exists('token_get_all')) {
109 foreach (token_get_all($source) as $token) {
110 if (is_string($token)) {
112 } elseif (in_array($token[0], [T_COMMENT, T_DOC_COMMENT])) {
113 $output .= str_repeat("\n", substr_count($token[1], "\n"));
114 } elseif (T_WHITESPACE === $token[0]) {
115 // reduce wide spaces
116 $whitespace = preg_replace('{[ \t]+}', ' ', $token[1]);
117 // normalize newlines to \n
118 $whitespace = preg_replace('{(?:\r\n|\r|\n)}', "\n", $whitespace);
119 // trim leading spaces
120 $whitespace = preg_replace('{\n +}', "\n", $whitespace);
121 $output .= $whitespace;
123 $output .= $token[1];
130 private static function getStubLicense()
132 $license = file_get_contents(dirname(__DIR__) . '/LICENSE');
133 $license = str_replace('The MIT License (MIT)', '', $license);
134 $license = str_replace("\n", "\n * ", trim($license));
139 const STUB_AUTOLOAD = <<<'EOS'
140 Phar::mapPhar('psysh.phar');
141 require 'phar://psysh.phar/build-vendor/autoload.php';
145 * Get a Phar stub for psysh.
147 * This is basically the psysh bin, with the autoload require statements swapped out.
151 private function getStub()
153 $content = file_get_contents(dirname(__DIR__) . '/bin/psysh');
154 $content = preg_replace('{/\* <<<.*?>>> \*/}sm', self::STUB_AUTOLOAD, $content);
155 $content = preg_replace('/\\(c\\) .*?with this source code./sm', self::getStubLicense(), $content);
157 $content .= '__HALT_COMPILER();';