Yaffs site version 1.1
[yaffs-website] / vendor / psy / psysh / src / Psy / Compiler.php
1 <?php
2
3 /*
4  * This file is part of Psy Shell.
5  *
6  * (c) 2012-2017 Justin Hileman
7  *
8  * For the full copyright and license information, please view the LICENSE
9  * file that was distributed with this source code.
10  */
11
12 namespace Psy;
13
14 use Symfony\Component\Finder\Finder;
15
16 /**
17  * A Psy Shell Phar compiler.
18  */
19 class Compiler
20 {
21     /**
22      * Compiles psysh into a single phar file.
23      *
24      * @param string $pharFile The full path to the file to create
25      */
26     public function compile($pharFile = 'psysh.phar')
27     {
28         if (file_exists($pharFile)) {
29             unlink($pharFile);
30         }
31
32         $this->version = Shell::VERSION;
33
34         $phar = new \Phar($pharFile, 0, 'psysh.phar');
35         $phar->setSignatureAlgorithm(\Phar::SHA1);
36
37         $phar->startBuffering();
38
39         $finder = Finder::create()
40             ->files()
41             ->ignoreVCS(true)
42             ->name('*.php')
43             ->notName('Compiler.php')
44             ->notName('Autoloader.php')
45             ->in(__DIR__ . '/..');
46
47         foreach ($finder as $file) {
48             $this->addFile($phar, $file);
49         }
50
51         $finder = Finder::create()
52             ->files()
53             ->ignoreVCS(true)
54             ->name('*.php')
55             ->exclude('Tests')
56             ->exclude('tests')
57             ->exclude('Test')
58             ->exclude('test')
59             ->in(__DIR__ . '/../../build-vendor');
60
61         foreach ($finder as $file) {
62             $this->addFile($phar, $file);
63         }
64
65         // Stubs
66         $phar->setStub($this->getStub());
67
68         $phar->stopBuffering();
69
70         unset($phar);
71     }
72
73     /**
74      * Add a file to the psysh Phar.
75      *
76      * @param \Phar        $phar
77      * @param \SplFileInfo $file
78      * @param bool         $strip (default: true)
79      */
80     private function addFile($phar, $file, $strip = true)
81     {
82         $path = str_replace(dirname(dirname(__DIR__)) . DIRECTORY_SEPARATOR, '', $file->getRealPath());
83
84         $content = file_get_contents($file);
85         if ($strip) {
86             $content = $this->stripWhitespace($content);
87         } elseif ('LICENSE' === basename($file)) {
88             $content = "\n" . $content . "\n";
89         }
90
91         $phar->addFromString($path, $content);
92     }
93
94     /**
95      * Removes whitespace from a PHP source string while preserving line numbers.
96      *
97      * @param string $source A PHP string
98      *
99      * @return string The PHP string with the whitespace removed
100      */
101     private function stripWhitespace($source)
102     {
103         if (!function_exists('token_get_all')) {
104             return $source;
105         }
106
107         $output = '';
108         foreach (token_get_all($source) as $token) {
109             if (is_string($token)) {
110                 $output .= $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;
121             } else {
122                 $output .= $token[1];
123             }
124         }
125
126         return $output;
127     }
128
129     private static function getStubLicense()
130     {
131         $license = file_get_contents(__DIR__ . '/../../LICENSE');
132         $license = str_replace('The MIT License (MIT)', '', $license);
133         $license = str_replace("\n", "\n * ", trim($license));
134
135         return $license;
136     }
137
138     const STUB_AUTOLOAD = <<<'EOS'
139     Phar::mapPhar('psysh.phar');
140     require 'phar://psysh.phar/build-vendor/autoload.php';
141 EOS;
142
143     /**
144      * Get a Phar stub for psysh.
145      *
146      * This is basically the psysh bin, with the autoload require statements swapped out.
147      *
148      * @return string
149      */
150     private function getStub()
151     {
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);
155         }
156         $content = preg_replace('{/\* <<<.*?>>> \*/}sm', self::STUB_AUTOLOAD, $content);
157         $content = preg_replace('/\\(c\\) .*?with this source code./sm', self::getStubLicense(), $content);
158
159         $content .= '__HALT_COMPILER();';
160
161         return $content;
162     }
163 }