Skip to content

Commit

Permalink
Use reflection to find class path.
Browse files Browse the repository at this point in the history
  • Loading branch information
letr0n committed Aug 23, 2014
1 parent a4ae540 commit 5eb16b8
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 55 deletions.
96 changes: 50 additions & 46 deletions config/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,61 +3,65 @@
return
[
/**
* Path to the vendor dir
* Path to the compile path.
*/

'vendor_path' => realpath(__DIR__ . '/../../../../vendor'),
'compile_path' => realpath(__DIR__ . '/../../../storage'),

/**
* List of classes to compile.
*/

'classes' =>
[
// Core classes

':vendor:/mako/framework/src/mako/file/FileSystem.php',
':vendor:/mako/framework/src/mako/config/Config.php',
':vendor:/mako/framework/src/mako/utility/Arr.php',
':vendor:/mako/framework/src/mako/application/Application.php',
':vendor:/mako/framework/src/mako/application/Web.php',
':vendor:/mako/framework/src/mako/application/services/Service.php',
':vendor:/mako/framework/src/mako/error/ErrorHandler.php',
':vendor:/mako/framework/src/mako/security/Comparer.php',
':vendor:/mako/framework/src/mako/security/Signer.php',
':vendor:/mako/framework/src/mako/http/routing/Routes.php',
':vendor:/mako/framework/src/mako/http/routing/Route.php',
':vendor:/mako/framework/src/mako/http/routing/Router.php',
':vendor:/mako/framework/src/mako/http/routing/Dispatcher.php',
':vendor:/mako/framework/src/mako/http/routing/Controller.php',
':vendor:/mako/framework/src/mako/view/renderers/RendererInterface.php',
':vendor:/mako/framework/src/mako/view/renderers/CacheableInterface.php',
':vendor:/mako/framework/src/mako/view/renderers/Renderer.php',
':vendor:/mako/framework/src/mako/view/renderers/Template.php',
':vendor:/mako/framework/src/mako/view/ViewFactory.php',
':vendor:/mako/framework/src/mako/http/Request.php',
':vendor:/mako/framework/src/mako/http/Response.php',
':vendor:/mako/framework/src/mako/syringe/Container.php',

// Services
],

':vendor:/mako/framework/src/mako/application/services/ErrorHandlerService.php',
':vendor:/mako/framework/src/mako/application/services/RequestService.php',
':vendor:/mako/framework/src/mako/application/services/ResponseService.php',
':vendor:/mako/framework/src/mako/application/services/SignerService.php',
':vendor:/mako/framework/src/mako/application/services/RouteService.php',
':vendor:/mako/framework/src/mako/application/services/URLBuilderService.php',
':vendor:/mako/framework/src/mako/application/services/LoggerService.php',
':vendor:/mako/framework/src/mako/application/services/ViewFactoryService.php',
':vendor:/mako/framework/src/mako/application/services/SessionService.php',
':vendor:/mako/framework/src/mako/application/services/DatabaseService.php',
':vendor:/mako/framework/src/mako/application/services/RedisService.php',
':vendor:/mako/framework/src/mako/application/services/I18nService.php',
':vendor:/mako/framework/src/mako/application/services/HumanizerService.php',
':vendor:/mako/framework/src/mako/application/services/CacheService.php',
':vendor:/mako/framework/src/mako/application/services/CryptoService.php',
':vendor:/mako/framework/src/mako/application/services/ValidatorFactoryService.php',
':vendor:/mako/framework/src/mako/application/services/PaginationFactoryService.php',
':vendor:/mako/framework/src/mako/application/services/GatekeeperService.php',
/**
* List of core classes to compile.
*/

'core_classes' =>
[
'\mako\file\FileSystem',
'\mako\config\Config',
'\mako\utility\Arr',
'\mako\application\Application',
'\mako\application\Web',
'\mako\application\services\Service',
'\mako\error\ErrorHandler',
'\mako\security\Comparer',
'\mako\security\Signer',
'\mako\http\routing\Routes',
'\mako\http\routing\Route',
'\mako\http\routing\Router',
'\mako\http\routing\Dispatcher',
'\mako\http\routing\Controller',
'\mako\view\renderers\RendererInterface',
'\mako\view\renderers\CacheableInterface',
'\mako\view\renderers\Renderer',
'\mako\view\renderers\Template',
'\mako\view\ViewFactory',
'\mako\http\Request',
'\mako\http\Response',
'\mako\syringe\Container',
'\mako\application\services\ErrorHandlerService',
'\mako\application\services\RequestService',
'\mako\application\services\ResponseService',
'\mako\application\services\SignerService',
'\mako\application\services\RouteService',
'\mako\application\services\URLBuilderService',
'\mako\application\services\LoggerService',
'\mako\application\services\ViewFactoryService',
'\mako\application\services\SessionService',
'\mako\application\services\DatabaseService',
'\mako\application\services\RedisService',
'\mako\application\services\I18nService',
'\mako\application\services\HumanizerService',
'\mako\application\services\CacheService',
'\mako\application\services\CryptoService',
'\mako\application\services\ValidatorFactoryService',
'\mako\application\services\PaginationFactoryService',
'\mako\application\services\GatekeeperService',
],
];
];
34 changes: 25 additions & 9 deletions tasks/Compiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace optimizer\tasks;

use \ReflectionClass;

use \PhpParser\Lexer;
use \PhpParser\Parser;
use \PhpParser\PrettyPrinter\Standard as Printer;
Expand Down Expand Up @@ -47,7 +49,7 @@ class Compiler extends \mako\reactor\Task

protected function getCompilePath()
{
return $this->app->getApplicationPath() . '/storage';
return $this->config->get('optimizer::config.compile_path');
}

/**
Expand All @@ -62,6 +64,18 @@ public function getCompileFile()
return $this->getCompilePath() . '/compiled.php';
}

/**
* Returns array of classes to compile.
*
* @access protected
* @return array
*/

public function getClasses()
{
return array_unique(array_merge($this->config->get('optimizer::config.core_classes'), $this->config->get('optimizer::config.classes')));
}

/**
* Compiles a class.
*
Expand Down Expand Up @@ -91,12 +105,11 @@ protected function compileClass($class, $stripComments)
*
* @access protected
* @param string $classes Array of classes we want to compile
* @param string $vendorPath Path to the vendor directory
* @param boolean $stripComments TRUE to strip comments and FALSE to leave them
* @return void
*/

protected function compileClasses($classes, $vendorPath, $stripComments)
protected function compileClasses($classes, $stripComments)
{
$progress = $this->output->progress(count($classes));

Expand All @@ -106,7 +119,12 @@ protected function compileClasses($classes, $vendorPath, $stripComments)

foreach($classes as $class)
{
fwrite($fileResource, $this->compileClass(str_replace(':vendor:', $vendorPath, $class), $stripComments));
$classPath = (new ReflectionClass($class))->getFileName();

if($classPath !== false)
{
fwrite($fileResource, $this->compileClass($classPath, $stripComments));
}

$progress->advance();
}
Expand All @@ -130,13 +148,11 @@ public function compile()
return $this->output->stderr()->writeln(sprintf('Unable to compile classes. Make sure that the compile directory [ %s ] is writable.', $this->getCompilePath()));
}

$vendorPath = $this->config->get('optimizer::config.vendor_path');

$classes = $this->config->get('optimizer::config.classes');
$classes = $this->getClasses();

$stripComments = $this->input->param('strip-comments', false);

$this->compileClasses($classes, $vendorPath, $stripComments);
$this->compileClasses($classes, $stripComments);
}

/**
Expand Down Expand Up @@ -164,4 +180,4 @@ public function clear()
$this->output->writeln('<green>Nothing to clear.</green>');
}
}
}
}

0 comments on commit 5eb16b8

Please sign in to comment.