1+ <?php
2+ /**
3+ * Created by PhpStorm.
4+ * User: inhere
5+ * Date: 2018-01-22
6+ * Time: 11:55
7+ */
8+
9+ namespace Inhere \Console \Examples \Controllers ;
10+
11+ use Inhere \Console \Components \PharCompiler ;
12+ use Inhere \Console \Controller ;
13+ use Inhere \Console \Utils \Helper ;
14+ use Inhere \Console \Utils \Show ;
15+
16+ /**
17+ * Class PharController
18+ * @package Inhere\Console\Examples\Controllers
19+ */
20+ class PharController extends Controller
21+ {
22+ protected static $ name = 'phar ' ;
23+ protected static $ description = 'Pack a project directory to phar or unpack phar to directory ' ;
24+
25+ /**
26+ * pack project to a phar package
27+ * @usage {fullCommand} [--dir DIR] [--output FILE]
28+ * @options
29+ * --dir STRING Setting the directory for packing.
30+ * - default is current work-dir.(<comment>{workDir}</comment>)
31+ * --output STRING Setting the output file name(<comment>app.phar</comment>)
32+ * --refresh BOOL Whether build vendor folder files on phar file exists(<comment>False</comment>)
33+ * @param \Inhere\Console\IO\Input $in
34+ * @param \Inhere\Console\IO\Output $out
35+ * @return int
36+ */
37+ public function packCommand ($ in , $ out ): int
38+ {
39+ $ time = microtime (1 );
40+ $ workDir = $ in ->getPwd ();
41+ $ dir = $ in ->getOpt ('dir ' ) ?: $ workDir ;
42+ $ pharFile = $ workDir . '/ ' . $ in ->getOpt ('output ' , 'app.phar ' );
43+
44+ $ cpr = new PharCompiler ($ dir );
45+
46+ // config
47+ $ cpr
48+ // ->stripComments(false)
49+ ->setShebang (true )
50+ ->addExclude ([
51+ 'demo ' ,
52+ 'tests ' ,
53+ 'tmp ' ,
54+ ])
55+ ->addFile ([
56+ 'LICENSE ' ,
57+ 'composer.json ' ,
58+ 'README.md ' ,
59+ 'tests/boot.php ' ,
60+ ])
61+ ->setCliIndex ('examples/app ' )
62+ // ->setWebIndex('web/index.php')
63+ // ->setVersionFile('config/config.php')
64+ ->in ($ dir )
65+ ;
66+
67+ $ cpr ->setStripFilter (function ($ file ) {
68+ /** @var \SplFileInfo $file */
69+ $ name = $ file ->getFilename ();
70+
71+ return false === strpos ($ name , 'Command.php ' ) && false === strpos ($ name , 'Controller.php ' );
72+ });
73+
74+ $ counter = null ;
75+ $ refresh = $ in ->boolOpt ('refresh ' );
76+
77+ $ out ->liteInfo (
78+ "Now, will begin building phar package. \n from path: <comment> $ workDir</comment> \n" .
79+ " phar file: <info> $ pharFile</info> "
80+ );
81+
82+ $ out ->info ('Pack file to Phar: ' );
83+
84+ $ cpr ->onError (function ($ error ) {
85+ $ this ->output ->warning ($ error );
86+ });
87+
88+ if ($ in ->getOpt ('debug ' )) {
89+ $ cpr ->onAdd (function ($ path ) {
90+ $ this ->output ->write (" <comment>+</comment> $ path " );
91+ });
92+ } else {
93+ $ counter = Show::counterTxt ('Handling ... ' , 'Done. ' );
94+
95+ $ cpr ->onAdd (function () use ($ counter ) {
96+ $ counter ->send (1 );
97+ });
98+ }
99+
100+ // packing ...
101+ $ cpr ->pack ($ pharFile , $ refresh );
102+
103+ if ($ counter ) {
104+ $ counter ->send (-1 );
105+ }
106+
107+ $ out ->write ([
108+ PHP_EOL . '<success>Phar build completed!</success> ' ,
109+ " - Phar file: $ pharFile " ,
110+ ' - Phar size: ' . round (filesize ($ pharFile ) / 1024 / 1024 , 2 ) . ' Mb ' ,
111+ ' - Pack Time: ' . round (microtime (1 ) - $ time , 3 ) . ' s ' ,
112+ ' - Pack File: ' . $ cpr ->getCounter (),
113+ ' - Commit ID: ' . $ cpr ->getVersion (),
114+ ]);
115+
116+ return 0 ;
117+ }
118+
119+ /**
120+ * unpack a phar package to a directory
121+ * @usage {fullCommand} -f FILE [-d DIR]
122+ * @options
123+ * -f, --file STRING The packed phar file path
124+ * -d, --dir STRING The output dir on extract phar package.
125+ * -y, --yes BOOL Whether display goon tips message.
126+ * --overwrite BOOL Whether overwrite exists files on extract phar
127+ * @example {fullCommand} -f myapp.phar -d var/www/app
128+ * @param \Inhere\Console\IO\Input $in
129+ * @param \Inhere\Console\IO\Output $out
130+ * @return int
131+ */
132+ public function unpackCommand ($ in , $ out ): int
133+ {
134+ if (!$ path = $ in ->getSameOpt (['f ' , 'file ' ])) {
135+ return $ out ->error ("Please input the phar file path by option '-f|--file' " );
136+ }
137+
138+ $ basePath = $ in ->getPwd ();
139+ $ file = realpath ($ basePath . '/ ' . $ path );
140+
141+ if (!file_exists ($ file )) {
142+ return $ out ->error ("The phar file not exists. File: $ file " );
143+ }
144+
145+ $ dir = $ in ->getSameOpt (['d ' , 'dir ' ]) ?: $ basePath ;
146+ $ overwrite = $ in ->getBoolOpt ('overwrite ' );
147+
148+ if (!is_dir ($ dir )) {
149+ Helper::mkdir ($ dir );
150+ }
151+
152+ $ out ->write ("Now, begin extract phar file: \n $ file \nto dir: \n $ dir " );
153+
154+ PharCompiler::unpack ($ file , $ dir , null , $ overwrite );
155+
156+ $ out ->success ("OK, phar package have been extract to the dir: $ dir " );
157+
158+ return 0 ;
159+ }
160+ }
0 commit comments