Skip to content

Commit f2b09a7

Browse files
committed
Merge pull request naneau#34 from entrypass/resume-on-error
Option to resume on error
2 parents 072bc60 + aa6efc9 commit f2b09a7

File tree

3 files changed

+97
-9
lines changed

3 files changed

+97
-9
lines changed

src/Naneau/Obfuscator/Console/Command/ObfuscateCommand.php

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
use Naneau\Obfuscator\Obfuscator;
1414
use Naneau\Obfuscator\Obfuscator\Event\File as FileEvent;
15+
use Naneau\Obfuscator\Obfuscator\Event\FileError as FileErrorEvent;
1516

1617
use Symfony\Component\Console\Command\Command;
1718
use Symfony\Component\Console\Input\InputArgument;
@@ -70,6 +71,11 @@ protected function configure()
7071
null,
7172
InputOption::VALUE_NONE,
7273
'Leave whitespace in output?'
74+
)->addOption(
75+
'ignore_error',
76+
null,
77+
InputOption::VALUE_NONE,
78+
'Continue processing the next file when error is encountered'
7379
)->addOption(
7480
'config',
7581
null,
@@ -113,6 +119,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
113119

114120
// Strip whitespace?
115121
$stripWhitespace = !$input->getOption('leave_whitespace');
122+
$ignoreError = !!$input->getOption('ignore_error');
116123

117124
// Show every file
118125
$this->getObfuscator()->getEventDispatcher()->addListener(
@@ -124,9 +131,25 @@ function(FileEvent $event) use ($output, $directory) {
124131
));
125132
}
126133
);
134+
// Show error processing file
135+
if($ignoreError) {
136+
$this->getObfuscator()->getEventDispatcher()->addListener(
137+
'obfuscator.file.error',
138+
function(FileErrorEvent $event) use ($output, $directory) {
139+
$output->writeln(sprintf(
140+
'Error obfuscating <error>%s</error>',
141+
substr($event->getFile(), strlen($directory))
142+
));
143+
$output->writeln(sprintf(
144+
'Parsing error: <error>%s</error>', $event->getErrorMessage()
145+
));
146+
}
147+
);
148+
}
127149

128150
// Actual obfuscation
129-
$this->getObfuscator()->obfuscate($directory, $stripWhitespace);
151+
$this->getObfuscator()->obfuscate($directory, $stripWhitespace,
152+
$ignoreError);
130153
}
131154

132155
/**

src/Naneau/Obfuscator/Obfuscator.php

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
namespace Naneau\Obfuscator;
1010

1111
use Naneau\Obfuscator\Obfuscator\Event\File as FileEvent;
12+
use Naneau\Obfuscator\Obfuscator\Event\FileError as FileErrorEvent;
1213

1314
use PhpParser\NodeTraverserInterface as NodeTraverser;
1415

@@ -78,7 +79,8 @@ class Obfuscator
7879
* @param bool $stripWhitespace
7980
* @return void
8081
**/
81-
public function obfuscate($directory, $stripWhitespace = false)
82+
public function obfuscate($directory, $stripWhitespace = false,
83+
$ignoreError = false)
8284
{
8385
foreach ($this->getFiles($directory) as $file) {
8486
$this->getEventDispatcher()->dispatch(
@@ -87,7 +89,8 @@ public function obfuscate($directory, $stripWhitespace = false)
8789
);
8890

8991
// Write obfuscated source
90-
file_put_contents($file, $this->obfuscateFileContents($file));
92+
file_put_contents($file, $this->obfuscateFileContents($file,
93+
$ignoreError));
9194

9295
// Strip whitespace if required
9396
if ($stripWhitespace) {
@@ -230,9 +233,11 @@ private function getFiles($directory)
230233
* Obfuscate a single file's contents
231234
*
232235
* @param string $file
236+
* @param boolean $ignoreError if true, do not throw an Error and
237+
* exit, but continue with next file
233238
* @return string obfuscated contents
234239
**/
235-
private function obfuscateFileContents($file)
240+
private function obfuscateFileContents($file, $ignoreError)
236241
{
237242
try {
238243
// Input code
@@ -245,11 +250,19 @@ private function obfuscateFileContents($file)
245250

246251
return "<?php\n" . $this->getPrettyPrinter()->prettyPrint($ast);
247252
} catch (Exception $e) {
248-
throw new Exception(
249-
sprintf('Could not parse file "%s"', $file),
250-
null,
251-
$e
252-
);
253+
if($ignoreError) {
254+
sprintf('Could not parse file "%s"', $file);
255+
$this->getEventDispatcher()->dispatch(
256+
'obfuscator.file.error',
257+
new FileErrorEvent($file, $e->getMessage())
258+
);
259+
} else {
260+
throw new Exception(
261+
sprintf('Could not parse file "%s"', $file),
262+
null,
263+
$e
264+
);
265+
}
253266
}
254267
}
255268
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?php
2+
/**
3+
* The file that handles parsing error events
4+
*
5+
* @package Obfuscator
6+
* @subpackage Obfuscator
7+
*/
8+
9+
namespace Naneau\Obfuscator\Obfuscator\Event;
10+
11+
use Symfony\Component\EventDispatcher\Event;
12+
use Naneau\Obfuscator\Obfuscator\Event\File;
13+
14+
/**
15+
* FileError
16+
*
17+
* The file being obfuscated that causes an error
18+
*
19+
* @category Naneau
20+
* @package Obfuscator
21+
* @subpackage Obfuscator
22+
*/
23+
class FileError extends File
24+
{
25+
/**
26+
* The error message from Exception
27+
* @var string
28+
**/
29+
private $errorMessage;
30+
31+
/**
32+
* Constructor
33+
*
34+
* @param string $file
35+
* @return void
36+
**/
37+
public function __construct($file, $errorMessage)
38+
{
39+
parent::setFile($file);
40+
$this->errorMessage = $errorMessage;
41+
}
42+
43+
/**
44+
* Get the error message
45+
*
46+
* @return string
47+
*/
48+
public function getErrorMessage()
49+
{
50+
return $this->errorMessage;
51+
}
52+
}

0 commit comments

Comments
 (0)