Skip to content

Commit

Permalink
Merge pull request #68 from GrahamCampbell/class-preloader-4
Browse files Browse the repository at this point in the history
Support class preloader v4
  • Loading branch information
joelhy authored Jan 2, 2020
2 parents 3a7cb3d + 22de42a commit c962263
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 11 deletions.
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"illuminate/support": "^5.4|^6.0",
"symfony/var-dumper": "^3.1|^4.1|^4.2|^4.3",
"psy/psysh": "0.8.*|0.9.*",
"classpreloader/classpreloader": "^3.0"
"classpreloader/classpreloader": "^3.0|^4.0"
},
"license": "MIT",
"authors": [
Expand All @@ -20,4 +20,4 @@
"Flipbox\\LumenGenerator\\": "src/LumenGenerator/"
}
}
}
}
52 changes: 43 additions & 9 deletions src/LumenGenerator/Console/OptimizeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,11 @@
use ClassPreloader\Factory;
use Illuminate\Console\Command;
use Illuminate\Support\Composer;
use ClassPreloader\OutputWriter;
use ClassPreloader\CodeGenerator;
use Symfony\Component\Console\Input\InputOption;
use ClassPreloader\Exceptions\VisitorExceptionInterface;
use ClassPreloader\Exception\VisitorExceptionInterface as V4VisitorException;
use ClassPreloader\Exceptions\VisitorExceptionInterface as V3VisitorException;

class OptimizeCommand extends Command
{
Expand Down Expand Up @@ -57,7 +60,11 @@ public function handle()
if ($this->option('force') || !env('APP_DEBUG')) {
$this->info('Compiling common classes');

$this->compileClasses();
if (class_exists(Factory::class)) {
$this->compileClassesV3();
} else {
$this->compileClassesV4();
}
} else {
$this->call('clear-compiled');
}
Expand All @@ -66,21 +73,48 @@ public function handle()
/**
* Generate the compiled class file.
*/
protected function compileClasses()
protected function compileClassesV3()
{
$preloader = (new Factory())->create(['skip' => true]);

$handle = $preloader->prepareOutput(base_path('bootstrap/cache/compiled.php'));

foreach ($this->getClassFiles() as $file) {
try {
fwrite($handle, $preloader->getCode($file, false)."\n");
} catch (VisitorExceptionInterface $e) {
//
try {
foreach ($this->getClassFiles() as $file) {
try {
fwrite($handle, $preloader->getCode($file, false)."\n");
} catch (V3VisitorException $e) {
//
}
}
} finally {
fclose($handle);
}
}

fclose($handle);
/**
* Generate the compiled class file.
*/
protected function compileClassesV4()
{
$codeGen = CodeGenerator::create(['skip' => true]);

$handle = OutputWriter::openOutputFile(base_path('bootstrap/cache/compiled.php'));

try {
OutputWriter::writeOpeningTag($handle, $strictTypes);

foreach ($this->getClassFiles() as $file) {
try {
$code = $codeGen->getCode($file, false);
OutputWriter::writeFileContent($handle, $code."\n");
} catch (V4VisitorException $e) {
//
}
}
} finally {
OutputWriter::closeHandle($handle);
}
}

/**
Expand Down

0 comments on commit c962263

Please sign in to comment.