1 <?php
2
3 4 5 6 7 8 9 10
11
12 $package_name = 'Brickrouge';
13
14 $exclude = array
15 (
16 '.*\.less',
17 '.*\.md',
18 '.*-uncompressed\..*',
19 '.git/.*',
20 '.gitignore',
21 '.travis.yml',
22 'build/.*',
23 'lib/.*\.js',
24 'Makefile',
25 'phpunit.xml.dist',
26 'README.md',
27 'tests/.*'
28 );
29
30 $do_not_compress = array('gif' => true, 'jpg' => true, 'jpeg' => true, 'png' => true);
31
32 33 34
35
36 function ($source)
37 {
38 if (!function_exists('token_get_all'))
39 {
40 return $source;
41 }
42
43 $output = '';
44
45 foreach (token_get_all($source) as $token)
46 {
47 if (is_string($token))
48 {
49 $output .= $token;
50 }
51 else if ($token[0] == T_COMMENT || $token[0] == T_DOC_COMMENT)
52 {
53 $output .= str_repeat("\n", substr_count($token[1], "\n"));
54 }
55 else
56 {
57 $output .= $token[1];
58 }
59 }
60
61 return $output;
62 }
63
64 $dir = dirname(__DIR__);
65
66 chdir($dir);
67
68 $phar_pathname = dirname($dir) . "/{$package_name}.phar";
69
70 if (file_exists($phar_pathname))
71 {
72 unlink($phar_pathname);
73 }
74
75 $phar = new Phar($phar_pathname);
76 $phar->setSignatureAlgorithm(\Phar::SHA1);
77 $phar->setStub(<<<EOT
78 <?php
79
80 define('{$package_name}\ROOT', 'phar://' . __FILE__ . DIRECTORY_SEPARATOR);
81
82 require_once {$package_name}\ROOT . 'bootstrap.php';
83 require_once {$package_name}\ROOT . 'lib/helpers.php';
84
85 __HALT_COMPILER();
86 EOT
87 );
88
89 $phar->startBuffering();
90
91 $rii = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($dir, FilesystemIterator::KEY_AS_PATHNAME | FilesystemIterator::CURRENT_AS_FILEINFO | FilesystemIterator::SKIP_DOTS | FilesystemIterator::UNIX_PATHS));
92
93 $n = 0;
94 $root_length = strlen($dir . DIRECTORY_SEPARATOR);
95
96 array_walk($exclude, function(&$v) { $v = "~^{$v}$~"; });
97
98 function is_excluded($pathname)
99 {
100 global $exclude;
101
102 foreach ($exclude as $pattern)
103 {
104 if (preg_match($pattern, $pathname))
105 {
106 return true;
107 }
108 }
109 }
110
111 foreach ($rii as $pathname => $file)
112 {
113 $extension = $file->getExtension();
114 $relative_pathname = substr($pathname, $root_length);
115
116 if (is_excluded($relative_pathname))
117 {
118 continue;
119 }
120
121 echo $relative_pathname . PHP_EOL;
122
123 $contents = file_get_contents($pathname);
124
125 if ($extension == 'php')
126 {
127 $contents = strip_comments($contents);
128 }
129
130 $pathname = substr($pathname, $root_length);
131
132 $phar[$pathname] = $contents;
133
134 if (empty($do_not_compress[$extension]))
135 {
136 $phar[$pathname]->compress(Phar::GZ);
137 }
138
139 $n++;
140 }
141
142 $phar->stopBuffering();
143
144 echo "Phar created: $phar_pathname ($n files, " . round((filesize($phar_pathname) / 1024)) . ' Ko)' . PHP_EOL;
145