/bin/bear.compile
Unknown | 127 lines | 114 code | 13 blank | 0 comment | 0 complexity | 704fc1a4933e16698e5b67f218b062a9 MD5 | raw file
- #!/usr/bin/env php
- <?php
- /**
- * Application compiler
- *
- * usage: php -d apc.enable_cli=1 -d apc.stat=0 bin/bear.compile {appsDir} for APC
- * php bin/bear.compile {appsDir} for FileCache
- *
- * + Loader file at var/lib/preloader/preload.php
- * + Application object cache
- * + Resource object cache
- * + Aspect weaved resource files
- * + Annotation cache
- *
- * @see https://github.com/mtdowling/ClassPreloader
- */
- namespace BEAR\Package\Dev\Compiler{
- init: {
- $appDir = isset($argv[1]) ? $argv[1] : error();
- $configFile = $appDir . '/var/lib/preloader/config.php';
- if (! file_exists($configFile)) {
- error("invalid app-dir:{$appDir}");
- }
- error_log('bear.compile started app:' . $appDir);
- }
- main: {
- require $appDir . '/bootstrap/autoload.php';
- // error_log('compile loader ...');
- // compileLoader($appDir);
- error_log('compile resources ...');
- $classes = compileObjectGraph($appDir);
- error_log('compile annotations ...');
- compileAnnotation($classes, $appDir);
- $output = $appDir . '/var/tmp/apc.dump';
- if (ini_get('apc.enable_cli') && ini_get('apc.stat') == 0 ) {
- apc_bin_dumpfile([], null, $output);
- error_log("apc dumped -> {$output}");
- }
- error_log('bear.compile completed.');
- }
- function compileLoader($appDir) {
- ini_set('display_errors', 1);
- ini_set('xhprof.output_dir', sys_get_temp_dir());
- $packageDir = dirname((__DIR__));
- $preLoader = $packageDir . '/vendor/bin/classpreloader.php';
- $config = $appDir . '/var/lib/preloader/config.php';
- $output = $appDir . '/var/tmp/preloader/preload.php';
- $minOutput = $appDir . '/var/tmp/preloader/preload.min.php';
- $compilePreloader = "php {$preLoader} compile --strip_comments=0 --config={$config} --output={$output}";
- error_log($compilePreloader);
- passthru($compilePreloader);
- $lint = "php -l {$output}";
- error_log($lint);
- passthru($lint);
- /** @todo "min" loader */
- // file_put_contents($minOutput, php_strip_whitespace(file_get_contents($output)));
- // error_log("bear.compile preload.min.php -> {$minOutput}");
- }
- function compileObjectGraph($appDir)
- {
- $app = require $appDir . '/bootstrap/instance.php';
- /** @var $app \BEAR\Package\Provide\Application\AbstractApp */
- $res = 0;
- $classes = [];
- foreach ($app->resource as $meta) {
- $ro = $app->resource->newInstance($meta->uri);
- $classes[] = $class = get_class($ro);
- printf("%s -> %s" . PHP_EOL, $meta->uri, $class);
- $res++;
- }
- error_log($meta->uri . "{$res} resources created.");
- return $classes;
- }
- function compileAnnotation(array $classes, $appDir)
- {
- $reader = \Ray\Di\Injector::create([
- new \BEAR\Sunday\Module\Constant\NamedModule(['tmp_dir' => $appDir . '/tmp/cache']),
- new \BEAR\Sunday\Module\Cache\CacheModule,
- new \BEAR\Sunday\Module\Code\CachedAnnotationModule
- ])->getInstance('Doctrine\Common\Annotations\Reader');
- /** @var $reader \Doctrine\Common\Annotations\Reader' */
- foreach ($classes as $class) {
- if ((new \ReflectionClass($class))->implementsInterface('Ray\Aop\WeavedInterface')) {
- $class = get_parent_class($class);
- }
- $refClass = new \ReflectionClass($class);
- $annotations = $reader->getClassAnnotations($refClass);
- if ($annotations) {
- $annotationArr = [];
- foreach ($annotations as $annotation) {
- $annotationArr[] = get_class($annotation);
- }
- printf("%s @%s" . PHP_EOL, $class, implode(', ', $annotationArr));
- }
- // method
- $methods = $refClass->getMethods();
- foreach ($methods as $method) {
- $annotations = $reader->getMethodAnnotations($method);
- if ($annotations) {
- $annotationArr = [];
- foreach ($annotations as $annotation) {
- $annotationArr[] = get_class($annotation);
- }
- printf("%s::%s -> @%s" . PHP_EOL, $method->class, $method->name, implode(', ', $annotationArr));
- }
- }
- }
- }
- function error($msg = 'Usage: php [-d apc.enable_cli=1 -d apc.stat=0] bear.compiler <app-dir>')
- {
- error_log($msg);
- exit(1);
- }
- }