Skip to content
This repository was archived by the owner on Feb 21, 2022. It is now read-only.

Commit 493bb65

Browse files
committed
Improved compiler
* Splitted in several granulated methods * By default, new compiled objects are placed into build/ folder * Removed phar element from gitignore
1 parent 71a95f4 commit 493bb65

File tree

5 files changed

+173
-100
lines changed

5 files changed

+173
-100
lines changed

.gitignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,3 @@
33
composer.phar
44
composer.lock
55
phpunit.xml
6-
php-formatter.phar

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,14 +47,14 @@ $ git clone [email protected]:mmoreram/php-formatter.git
4747
$ cd php-formatter
4848
$ composer update
4949
$ php bin/compile
50-
$ sudo chmod +x php-formatter.phar
51-
$ php-formatter.phar
50+
$ sudo chmod +x build/php-formatter.phar
51+
$ build/php-formatter.phar
5252
```
5353

5454
You can copy the `.phar` file as a global script
5555

5656
``` bash
57-
$ cp php-formatter.phar /usr/local/bin/php-formatter
57+
$ cp build/php-formatter.phar /usr/local/bin/php-formatter
5858
```
5959

6060
## Config

build/php-formatter.phar

928 Bytes
Binary file not shown.

src/PHPFormatter/Command/UseSortCommand.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
163163
*
164164
* @return array Config array
165165
*/
166-
public function getUsableConfig(InputInterface $input)
166+
private function getUsableConfig(InputInterface $input)
167167
{
168168
$configLoader = new ConfigLoader;
169169
$configFinder = new ConfigFinder;
@@ -203,7 +203,7 @@ public function getUsableConfig(InputInterface $input)
203203
*
204204
* @return UseSortCommand self Object
205205
*/
206-
public function printDryRunMessage(
206+
private function printDryRunMessage(
207207
InputInterface $input,
208208
OutputInterface $output,
209209
$path
@@ -236,7 +236,7 @@ public function printDryRunMessage(
236236
*
237237
* @return UseSortCommand self Object
238238
*/
239-
public function printConfigUsed(
239+
private function printConfigUsed(
240240
OutputInterface $output,
241241
array $options
242242
)
@@ -289,7 +289,7 @@ public function printConfigUsed(
289289
*
290290
* @return UseSortCommand self Object
291291
*/
292-
public function parseAndFixFiles(
292+
private function parseAndFixFiles(
293293
InputInterface $input,
294294
OutputInterface $output,
295295
IteratorAggregate $files,
@@ -335,7 +335,7 @@ public function parseAndFixFiles(
335335
*
336336
* @return UseSorter Use sorter instance
337337
*/
338-
public function createUseSorter(array $options)
338+
private function createUseSorter(array $options)
339339
{
340340
/**
341341
* Creates the new UseSorter file, given config values

src/PHPFormatter/Compiler/Compiler.php

Lines changed: 165 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
use DateTime;
1919
use Phar;
2020
use RuntimeException;
21+
use SplFileInfo;
2122
use Symfony\Component\Finder\Finder;
2223
use Symfony\Component\Process\Process;
2324

@@ -44,118 +45,52 @@ class Compiler
4445
* Compiles composer into a single phar file
4546
*
4647
* @throws RuntimeException
47-
*
48-
* @param string $pharFile The full path to the file to create
4948
*/
50-
public function compile($pharFile = 'php-formatter.phar')
49+
public function compile()
5150
{
52-
if (file_exists($pharFile)) {
53-
unlink($pharFile);
54-
}
51+
$pharFilePath = dirname(__FILE__) . '/../../../build/php-formatter.phar';
5552

56-
/**
57-
* Loading versions
58-
*/
59-
$process = new Process('git log --pretty="%H" -n1 HEAD', __DIR__);
60-
if ($process->run() != 0) {
61-
throw new \RuntimeException('Can\'t run git log. You must ensure to run compile from php-formatter git repository clone and that git binary is available.');
53+
if (file_exists($pharFilePath)) {
54+
unlink($pharFilePath);
6255
}
63-
$this->version = trim($process->getOutput());
6456

65-
$process = new Process('git log -n1 --pretty=%ci HEAD', __DIR__);
66-
if ($process->run() != 0) {
67-
throw new \RuntimeException('Can\'t run git log. You must ensure to run compile from php-formatter git repository clone and that git binary is available.');
68-
}
69-
$date = new \DateTime(trim($process->getOutput()));
70-
$date->setTimezone(new \DateTimeZone('UTC'));
71-
$this->versionDate = $date->format('Y-m-d H:i:s');
72-
73-
$process = new Process('git describe --tags HEAD');
74-
if ($process->run() == 0) {
75-
$this->version = trim($process->getOutput());
76-
}
57+
$this->loadVersion();
7758

7859
/**
7960
* Creating phar object
8061
*/
81-
$phar = new Phar($pharFile, 0, 'php-formatter.phar');
62+
$phar = new Phar($pharFilePath, 0, 'php-formatter.phar');
8263
$phar->setSignatureAlgorithm(\Phar::SHA1);
8364

8465
$phar->startBuffering();
8566

86-
/**
87-
* All *.php files
88-
*/
89-
$finder = new Finder();
90-
$finder
91-
->files()
92-
->ignoreVCS(true)
93-
->name('*.php')
94-
->notName('Compiler.php')
95-
->notName('ClassLoader.php')
96-
->in(realpath(__DIR__ . '/../../../src'));
97-
98-
foreach ($finder as $file) {
99-
$this->addFile($phar, $file);
100-
}
101-
102-
/**
103-
* All vendors (ignoring tests)
104-
*/
105-
$finder = new Finder();
106-
$finder
107-
->files()
108-
->ignoreVCS(true)
109-
->name('*.php')
110-
->exclude('Tests')
111-
->in(realpath(__DIR__ . '/../../../vendor/symfony/'));
112-
113-
foreach ($finder as $file) {
114-
$this->addFile($phar, $file);
115-
}
116-
117-
/**
118-
* Adding composer vendor files
119-
*/
120-
$this->addFile($phar, new \SplFileInfo(__DIR__ . '/../../../vendor/autoload.php'));
121-
$this->addFile($phar, new \SplFileInfo(__DIR__ . '/../../../vendor/composer/autoload_namespaces.php'));
122-
$this->addFile($phar, new \SplFileInfo(__DIR__ . '/../../../vendor/composer/autoload_psr4.php'));
123-
$this->addFile($phar, new \SplFileInfo(__DIR__ . '/../../../vendor/composer/autoload_classmap.php'));
124-
$this->addFile($phar, new \SplFileInfo(__DIR__ . '/../../../vendor/composer/autoload_real.php'));
125-
$this->addFile($phar, new \SplFileInfo(__DIR__ . '/../../../vendor/composer/ClassLoader.php'));
126-
127-
if (file_exists(__DIR__ . '/../../../vendor/composer/include_paths.php')) {
128-
$this->addFile($phar, new \SplFileInfo(__DIR__ . '/../../../vendor/composer/include_paths.php'));
129-
}
130-
131-
/**
132-
* Adding bin
133-
*/
134-
$this->addBin($phar);
135-
136-
/**
137-
* Adding stubs
138-
*/
139-
$phar->setStub($this->getStub());
67+
$this
68+
->addPHPFiles($phar)
69+
->addVendorFiles($phar)
70+
->addComposerVendorFiles($phar)
71+
->addBin($phar)
72+
->addStub($phar)
73+
->addLicense($phar);
14074

14175
$phar->stopBuffering();
14276

143-
/**
144-
* Adding LICENSE
145-
*/
146-
$this->addFile($phar, new \SplFileInfo(__DIR__ . '/../../../LICENSE'), false);
147-
14877
unset($phar);
14978
}
15079

15180
/**
15281
* Add a file into the phar package
15382
*
154-
* @param Phar $phar Phar object
155-
* @param string $file File to add
156-
* @param bool $strip strip
83+
* @param Phar $phar Phar object
84+
* @param SplFileInfo $file File to add
85+
* @param bool $strip strip
86+
*
87+
* @return Compiler self Object
15788
*/
158-
protected function addFile(Phar $phar, $file, $strip = true)
89+
protected function addFile(
90+
Phar $phar,
91+
SplFileInfo $file,
92+
$strip = true
93+
)
15994
{
16095
$path = strtr(str_replace(dirname(dirname(dirname(__DIR__))) . DIRECTORY_SEPARATOR, '', $file->getRealPath()), '\\', '/');
16196
$content = file_get_contents($file);
@@ -171,18 +106,24 @@ protected function addFile(Phar $phar, $file, $strip = true)
171106
}
172107

173108
$phar->addFromString($path, $content);
109+
110+
return $this;
174111
}
175112

176113
/**
177114
* Add bin into Phar
178115
*
179116
* @param Phar $phar Phar
117+
*
118+
* @return Compiler self Object
180119
*/
181120
protected function addBin(Phar $phar)
182121
{
183122
$content = file_get_contents(__DIR__ . '/../../../bin/php-formatter');
184123
$content = preg_replace('{^#!/usr/bin/env php\s*}', '', $content);
185124
$phar->addFromString('bin/php-formatter', $content);
125+
126+
return $this;
186127
}
187128

188129
/**
@@ -220,9 +161,9 @@ protected function stripWhitespace($source)
220161
return $output;
221162
}
222163

223-
protected function getStub()
164+
protected function addStub(Phar $phar)
224165
{
225-
return <<<'EOF'
166+
$stub = <<<'EOF'
226167
#!/usr/bin/env php
227168
<?php
228169
@@ -245,5 +186,138 @@ protected function getStub()
245186
246187
__HALT_COMPILER();
247188
EOF;
189+
$phar->setStub($stub);
190+
191+
return $this;
192+
}
193+
194+
/**
195+
* Add php files
196+
*
197+
* @param Phar $phar Phar instance
198+
*
199+
* @return Compiler self Object
200+
*/
201+
private function addPHPFiles(Phar $phar)
202+
{
203+
/**
204+
* All *.php files
205+
*/
206+
$finder = new Finder();
207+
$finder
208+
->files()
209+
->ignoreVCS(true)
210+
->name('*.php')
211+
->notName('Compiler.php')
212+
->notName('ClassLoader.php')
213+
->in(realpath(__DIR__ . '/../../../src'));
214+
215+
foreach ($finder as $file) {
216+
217+
$this->addFile($phar, $file);
218+
}
219+
220+
return $this;
221+
}
222+
223+
/**
224+
* Add vendor files
225+
*
226+
* @param Phar $phar Phar instance
227+
*
228+
* @return Compiler self Object
229+
*/
230+
private function addVendorFiles(Phar $phar)
231+
{
232+
$vendorPath = __DIR__ . '/../../../vendor/';
233+
234+
/**
235+
* All *.php files
236+
*/
237+
$finder = new Finder();
238+
$finder
239+
->files()
240+
->ignoreVCS(true)
241+
->name('*.php')
242+
->exclude('Tests')
243+
->in(realpath($vendorPath . 'symfony/'));
244+
245+
foreach ($finder as $file) {
246+
247+
$this->addFile($phar, $file);
248+
}
249+
250+
return $this;
251+
}
252+
253+
/**
254+
* Add composer vendor files
255+
*
256+
* @param Phar $phar Phar
257+
*
258+
* @return Compiler self Object
259+
*/
260+
private function addComposerVendorFiles(Phar $phar)
261+
{
262+
$vendorPath = __DIR__ . '/../../../vendor/';
263+
264+
/**
265+
* Adding composer vendor files
266+
*/
267+
$this
268+
->addFile($phar, new \SplFileInfo($vendorPath . 'autoload.php'))
269+
->addFile($phar, new \SplFileInfo($vendorPath . 'composer/autoload_namespaces.php'))
270+
->addFile($phar, new \SplFileInfo($vendorPath . 'composer/autoload_psr4.php'))
271+
->addFile($phar, new \SplFileInfo($vendorPath . 'composer/autoload_classmap.php'))
272+
->addFile($phar, new \SplFileInfo($vendorPath . 'composer/autoload_real.php'))
273+
->addFile($phar, new \SplFileInfo($vendorPath . 'composer/ClassLoader.php'));
274+
275+
if (file_exists($vendorPath . 'composer/include_paths.php')) {
276+
277+
$this->addFile($phar, new \SplFileInfo($vendorPath . 'composer/include_paths.php'));
278+
}
279+
280+
return $this;
281+
}
282+
283+
/**
284+
* Add license
285+
*
286+
* @param Phar $phar Phar
287+
*
288+
* @return Compiler self Object
289+
*/
290+
private function addLicense(Phar $phar)
291+
{
292+
$this->addFile($phar, new \SplFileInfo(__DIR__ . '/../../../LICENSE'), false);
293+
294+
return $this;
295+
}
296+
297+
/**
298+
* Load versions
299+
*/
300+
private function loadVersion()
301+
{
302+
$process = new Process('git log --pretty="%H" -n1 HEAD', __DIR__);
303+
if ($process->run() != 0) {
304+
throw new \RuntimeException('Can\'t run git log. You must ensure to run compile from php-formatter git repository clone and that git binary is available.');
305+
}
306+
$this->version = trim($process->getOutput());
307+
308+
$process = new Process('git log -n1 --pretty=%ci HEAD', __DIR__);
309+
if ($process->run() != 0) {
310+
throw new \RuntimeException('Can\'t run git log. You must ensure to run compile from php-formatter git repository clone and that git binary is available.');
311+
}
312+
$date = new \DateTime(trim($process->getOutput()));
313+
$date->setTimezone(new \DateTimeZone('UTC'));
314+
$this->versionDate = $date->format('Y-m-d H:i:s');
315+
316+
$process = new Process('git describe --tags HEAD');
317+
if ($process->run() == 0) {
318+
$this->version = trim($process->getOutput());
319+
}
320+
321+
return $this;
248322
}
249323
}

0 commit comments

Comments
 (0)