1- <?php namespace Conso \Commands ;
1+ <?php
2+
3+ namespace Conso \Commands ;
24
35/**
46 * @author <contact@lotfio.net>
5- * @package Conso PHP Console Creator
7+ *
68 * @version 2.0.0
9+ *
710 * @license MIT
11+ *
812 * @category CLI
13+ *
914 * @copyright 2019 Lotfio Lakehal
1015 */
1116
1217use Conso \Command ;
13- use Conso \Exceptions \{CompileException ,InputException };
14- use Conso \Contracts \{CommandInterface ,InputInterface ,OutputInterface };
18+ use Conso \Contracts \CommandInterface ;
19+ use Conso \Contracts \InputInterface ;
20+ use Conso \Contracts \OutputInterface ;
21+ use Conso \Exceptions \CompileException ;
22+ use Conso \Exceptions \InputException ;
1523
1624class Compile extends Command implements CommandInterface
1725{
1826 /**
19- * sub commands
27+ * sub commands.
2028 *
2129 * @var array
2230 */
2331 protected $ sub = [
24- 'init '
32+ 'init ' ,
2533 ];
2634
2735 /**
28- * flags
36+ * flags.
2937 *
3038 * @var array
3139 */
3240 protected $ flags = [
33- '--no-shebang '
41+ '--no-shebang ' ,
3442 ];
3543
3644 /**
37- * command help
45+ * command help.
3846 *
3947 * @var array
4048 */
41- protected $ help = [
49+ protected $ help = [
4250 'sub ' => [
43- 'init ' => 'initialize .json build file '
51+ 'init ' => 'initialize .json build file ' ,
4452 ],
4553 'flags ' => [
46- '--no-shebang ' => 'no stub shebang, usefull when invoking phar from http '
47- ]
54+ '--no-shebang ' => 'no stub shebang, usefull when invoking phar from http ' ,
55+ ],
4856 ];
4957
5058 /**
51- * command description
59+ * command description.
5260 *
5361 * @var string
5462 */
5563 protected $ description = 'Compile your package to a shareable phar file. ' ;
5664
5765 /**
58- * set up command dependencies
66+ * set up command dependencies.
5967 */
6068 public function __construct ()
6169 {
6270 $ this ->cwd = getcwd ();
63- $ this ->packageFile = $ this ->cwd . '/conso.json ' ;
71+ $ this ->packageFile = $ this ->cwd . '/conso.json ' ;
6472 }
6573
6674 /**
67- * execute method
75+ * execute method.
76+ *
77+ * @param InputInterface $input
78+ * @param OutputInterface $output
6879 *
69- * @param InputInterface $input
70- * @param OutputInterface $output
7180 * @return void
7281 */
73- public function execute (InputInterface $ input , OutputInterface $ output ) : void
82+ public function execute (InputInterface $ input , OutputInterface $ output ): void
7483 {
75- if (!\file_exists ($ this ->packageFile ))
84+ if (!\file_exists ($ this ->packageFile )) {
7685 throw new CompileException ("package file ( $ this ->packageFile ) not found " );
77-
78- if (ini_get ('phar.readonly ' ) == 1 )
79- throw new CompileException (" phar is read only mode ! it should be turned off from php.init to compile. " );
80-
86+ }
87+ if (ini_get ('phar.readonly ' ) == 1 ) {
88+ throw new CompileException (' phar is read only mode ! it should be turned off from php.init to compile. ' );
89+ }
8190 // read package file
8291 $ buildFile = (array ) json_decode (file_get_contents ($ this ->packageFile ));
8392
8493 // validate build file
8594 $ this ->validateBuildFile ($ buildFile );
86-
95+
8796 // create a phar file if everything went well
8897 $ shebang = ($ input ->flag ('--no-shebang ' ) === false ) ? true : false ;
89- if ($ this ->createPhar ($ buildFile , $ shebang ))
98+ if ($ this ->createPhar ($ buildFile , $ shebang )) {
9099 exit ($ output ->writeLn ("\npackage compiled successfully . \n\n" , 'green ' ));
91-
100+ }
101+
92102 $ output ->writeLn ("error compiling package. \n" , 'red ' );
93103 }
94104
95105 /**
96- * init package to compile
106+ * init package to compile.
97107 *
98108 * @return void
99109 */
100- public function init (InputInterface $ input , OutputInterface $ output ) : void
110+ public function init (InputInterface $ input , OutputInterface $ output ): void
101111 {
102- if (!\is_writable ($ this ->cwd ))
112+ if (!\is_writable ($ this ->cwd )) {
103113 throw new InputException ("{$ this ->cwd } is not writable. " );
104-
105- if (\file_exists ($ this ->packageFile ))
114+ }
115+ if (\file_exists ($ this ->packageFile )) {
106116 throw new InputException ("build file ( {$ this ->packageFile }) exists already. " );
107-
117+ }
108118 $ content = [
109- " src " => [
110- " src/Conso " ,
111- " vendor "
119+ ' src ' => [
120+ ' src/Conso ' ,
121+ ' vendor ' ,
112122 ],
113- " build " => " build " ,
114- " stub " => " conso " ,
115- " phar " => " conso.phar "
123+ ' build ' => ' build ' ,
124+ ' stub ' => ' conso ' ,
125+ ' phar ' => ' conso.phar ' ,
116126 ];
117127
118- if (\file_put_contents ($ this ->packageFile , json_encode ($ content , JSON_PRETTY_PRINT )))
128+ if (\file_put_contents ($ this ->packageFile , json_encode ($ content , JSON_PRETTY_PRINT ))) {
119129 exit ($ output ->writeLn ("\nbuild file created successfully. \n\n" , 'green ' ));
130+ }
120131
121132 $ output ->writeLn ("error creating build file. \n" , 'red ' );
122133 }
123134
124135 /**
125- * validate build file
136+ * validate build file.
126137 *
127138 * @param string
139+ *
128140 * @return void
129141 */
130- private function validateBuildFile (array $ file ) : void
142+ private function validateBuildFile (array $ file ): void
131143 {
132- if (!is_array ($ file ) || count ($ file ) < 4 )
133- throw new CompileException (" build file is not a valid json file. " );
134-
135- if (!in_array ('src ' , array_keys ($ file )))
136- throw new CompileException (" source (src) directory is missing from package file. " );
137-
138- if (!in_array ('build ' , array_keys ($ file )))
139- throw new CompileException (" build (build) directory is missing from package file. " );
140-
141- if (!in_array ('stub ' , array_keys ($ file )))
142- throw new CompileException (" stub (stub) file is missing from package file. " );
143-
144- if (!in_array ('phar ' , array_keys ($ file )))
145- throw new CompileException (" output (phar) file is missing from package file. " );
146-
147- if (!is_array ($ file ['src ' ]))
148- throw new CompileException (" source (src) directory must be an array of valid directories. " );
149-
150- foreach ($ file ['src ' ] as $ dir )
151- if (!is_dir ($ dir ))
144+ if (!is_array ($ file ) || count ($ file ) < 4 ) {
145+ throw new CompileException (' build file is not a valid json file. ' );
146+ }
147+ if (!in_array ('src ' , array_keys ($ file ))) {
148+ throw new CompileException (' source (src) directory is missing from package file. ' );
149+ }
150+ if (!in_array ('build ' , array_keys ($ file ))) {
151+ throw new CompileException (' build (build) directory is missing from package file. ' );
152+ }
153+ if (!in_array ('stub ' , array_keys ($ file ))) {
154+ throw new CompileException (' stub (stub) file is missing from package file. ' );
155+ }
156+ if (!in_array ('phar ' , array_keys ($ file ))) {
157+ throw new CompileException (' output (phar) file is missing from package file. ' );
158+ }
159+ if (!is_array ($ file ['src ' ])) {
160+ throw new CompileException (' source (src) directory must be an array of valid directories. ' );
161+ }
162+ foreach ($ file ['src ' ] as $ dir ) {
163+ if (!is_dir ($ dir )) {
152164 throw new CompileException ("source ( $ dir) directory is not a valid directory. " );
153-
154- if (!is_string ($ file ['build ' ]) || !is_dir ($ file ['build ' ]) || !is_writable ($ file ['build ' ]))
165+ }
166+ }
167+ if (!is_string ($ file ['build ' ]) || !is_dir ($ file ['build ' ]) || !is_writable ($ file ['build ' ])) {
155168 throw new CompileException ("build ( {$ file ['build ' ]}) directory is not a valid directory. " );
156-
157- if (!is_string ($ file ['stub ' ]) || !file_exists ($ file ['stub ' ]))
169+ }
170+ if (!is_string ($ file ['stub ' ]) || !file_exists ($ file ['stub ' ])) {
158171 throw new CompileException ("stub file ( {$ file ['stub ' ]}) not found. " );
159-
160- if (!is_string ($ file ['phar ' ]) || strlen ($ file ['phar ' ]) < 1 || preg_match ('/\.phar$/ ' , $ file ['phar ' ]) == false )
172+ }
173+ if (!is_string ($ file ['phar ' ]) || strlen ($ file ['phar ' ]) < 1 || preg_match ('/\.phar$/ ' , $ file ['phar ' ]) == false ) {
161174 throw new CompileException ("phar file ( {$ file ['phar ' ]}) must be a valid file ends with .phar extension. " );
175+ }
162176 }
163177
164178 /**
165- * create a phar
179+ * create a phar.
180+ *
181+ * @param array $rules
166182 *
167- * @param array $rules
168183 * @return void
169184 */
170- private function createPhar (array $ rules , bool $ shebang = false ) : bool
185+ private function createPhar (array $ rules , bool $ shebang = false ): bool
171186 {
172- $ buildLocation = rtrim ($ rules ['build ' ], '/ ' ) . " /package/ " ;
187+ $ buildLocation = rtrim ($ rules ['build ' ], '/ ' ). ' /package/ ' ;
173188
174189 // delete old build files if any
175190 deleteTree ($ buildLocation );
176191
177192 // copy project
178- foreach ($ rules ['src ' ] as $ src )
179- copyDirectory ($ src , $ buildLocation . rtrim ($ src , '/ ' ));
193+ foreach ($ rules ['src ' ] as $ src ) {
194+ copyDirectory ($ src , $ buildLocation .rtrim ($ src , '/ ' ));
195+ }
180196
181197 // copy stub file
182- copy ($ rules ['stub ' ], $ buildLocation . $ rules ['stub ' ]);
198+ copy ($ rules ['stub ' ], $ buildLocation. $ rules ['stub ' ]);
183199
184200 // create phar
185- $ phar = new \Phar (rtrim ($ rules ['build ' ], '/ ' ) . " / " . $ rules ['phar ' ]);
201+ $ phar = new \Phar (rtrim ($ rules ['build ' ], '/ ' ). ' / ' . $ rules ['phar ' ]);
186202
187203 // start buffering. Mandatory to modify stub to add shebang
188204 $ phar ->startBuffering ();
@@ -194,7 +210,7 @@ private function createPhar(array $rules, bool $shebang = false) : bool
194210 $ phar ->buildFromDirectory ($ buildLocation );
195211
196212 // add shebang
197- $ stub = "#!/usr/bin/env php \n" . $ defaultStub ;
213+ $ stub = "#!/usr/bin/env php \n" . $ defaultStub ;
198214 $ phar ->setStub ($ shebang === true ? $ stub : $ defaultStub );
199215
200216 // stop buffering
@@ -203,8 +219,9 @@ private function createPhar(array $rules, bool $shebang = false) : bool
203219 // gzip compressin
204220 $ phar ->compressFiles (\Phar::GZ );
205221
206- # Make the file executable
207- chmod (rtrim ($ rules ['build ' ], '/ ' ) . "/ " . $ rules ['phar ' ], 0770 );
222+ // Make the file executable
223+ chmod (rtrim ($ rules ['build ' ], '/ ' ).'/ ' .$ rules ['phar ' ], 0770 );
224+
208225 return deleteTree ($ buildLocation );
209226 }
210- }
227+ }
0 commit comments