4 * This file is part of Psy Shell.
6 * (c) 2012-2017 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.
22 * Compiles psysh into a single phar file.
24 * @param string $pharFile The full path to the file to create
26 public function compile($pharFile = 'psysh.phar')
28 if (file_exists($pharFile)) {
32 $this->version = Shell::VERSION;
34 $phar = new \Phar($pharFile, 0, 'psysh.phar');
35 $phar->setSignatureAlgorithm(\Phar::SHA1);
37 $phar->startBuffering();
39 $finder = Finder::create()
43 ->notName('Compiler.php')
44 ->notName('Autoloader.php')
45 ->in(__DIR__ . '/..');
47 foreach ($finder as $file) {
48 $this->addFile($phar, $file);
51 $finder = Finder::create()
59 ->in(__DIR__ . '/../../build-vendor');
61 foreach ($finder as $file) {
62 $this->addFile($phar, $file);
66 $phar->setStub($this->getStub());
68 $phar->stopBuffering();
74 * Add a file to the psysh Phar.
77 * @param \SplFileInfo $file
78 * @param bool $strip (default: true)
80 private function addFile($phar, $file, $strip = true)
82 $path = str_replace(dirname(dirname(__DIR__)) . DIRECTORY_SEPARATOR, '', $file->getRealPath());
84 $content = file_get_contents($file);
86 $content = $this->stripWhitespace($content);
87 } elseif ('LICENSE' === basename($file)) {
88 $content = "\n" . $content . "\n";
91 $phar->addFromString($path, $content);
95 * Removes whitespace from a PHP source string while preserving line numbers.
97 * @param string $source A PHP string
99 * @return string The PHP string with the whitespace removed
101 private function stripWhitespace($source)
103 if (!function_exists('token_get_all')) {
108 foreach (token_get_all($source) as $token) {
109 if (is_string($token)) {
111 } elseif (in_array($token[0], array(T_COMMENT, T_DOC_COMMENT))) {
112 $output .= str_repeat("\n", substr_count($token[1], "\n"));
113 } elseif (T_WHITESPACE === $token[0]) {
114 // reduce wide spaces
115 $whitespace = preg_replace('{[ \t]+}', ' ', $token[1]);
116 // normalize newlines to \n
117 $whitespace = preg_replace('{(?:\r\n|\r|\n)}', "\n", $whitespace);
118 // trim leading spaces
119 $whitespace = preg_replace('{\n +}', "\n", $whitespace);
120 $output .= $whitespace;
122 $output .= $token[1];
129 private static function getStubLicense()
131 $license = file_get_contents(__DIR__ . '/../../LICENSE');
132 $license = str_replace('The MIT License (MIT)', '', $license);
133 $license = str_replace("\n", "\n * ", trim($license));
138 const STUB_AUTOLOAD = <<<'EOS'
139 Phar::mapPhar('psysh.phar');
140 require 'phar://psysh.phar/build-vendor/autoload.php';
144 * Get a Phar stub for psysh.
146 * This is basically the psysh bin, with the autoload require statements swapped out.
150 private function getStub()
152 $content = file_get_contents(__DIR__ . '/../../bin/psysh');
153 if (version_compare(PHP_VERSION, '5.4', '<')) {
154 $content = str_replace('#!/usr/bin/env php', '#!/usr/bin/env php -d detect_unicode=Off', $content);
156 $content = preg_replace('{/\* <<<.*?>>> \*/}sm', self::STUB_AUTOLOAD, $content);
157 $content = preg_replace('/\\(c\\) .*?with this source code./sm', self::getStubLicense(), $content);
159 $content .= '__HALT_COMPILER();';