PageRenderTime 26ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/bin/bear.compile

https://github.com/kenjis/BEAR.Package
Unknown | 127 lines | 114 code | 13 blank | 0 comment | 0 complexity | 704fc1a4933e16698e5b67f218b062a9 MD5 | raw file
  1. #!/usr/bin/env php
  2. <?php
  3. /**
  4. * Application compiler
  5. *
  6. * usage: php -d apc.enable_cli=1 -d apc.stat=0 bin/bear.compile {appsDir} for APC
  7. * php bin/bear.compile {appsDir} for FileCache
  8. *
  9. * + Loader file at var/lib/preloader/preload.php
  10. * + Application object cache
  11. * + Resource object cache
  12. * + Aspect weaved resource files
  13. * + Annotation cache
  14. *
  15. * @see https://github.com/mtdowling/ClassPreloader
  16. */
  17. namespace BEAR\Package\Dev\Compiler{
  18. init: {
  19. $appDir = isset($argv[1]) ? $argv[1] : error();
  20. $configFile = $appDir . '/var/lib/preloader/config.php';
  21. if (! file_exists($configFile)) {
  22. error("invalid app-dir:{$appDir}");
  23. }
  24. error_log('bear.compile started app:' . $appDir);
  25. }
  26. main: {
  27. require $appDir . '/bootstrap/autoload.php';
  28. // error_log('compile loader ...');
  29. // compileLoader($appDir);
  30. error_log('compile resources ...');
  31. $classes = compileObjectGraph($appDir);
  32. error_log('compile annotations ...');
  33. compileAnnotation($classes, $appDir);
  34. $output = $appDir . '/var/tmp/apc.dump';
  35. if (ini_get('apc.enable_cli') && ini_get('apc.stat') == 0 ) {
  36. apc_bin_dumpfile([], null, $output);
  37. error_log("apc dumped -> {$output}");
  38. }
  39. error_log('bear.compile completed.');
  40. }
  41. function compileLoader($appDir) {
  42. ini_set('display_errors', 1);
  43. ini_set('xhprof.output_dir', sys_get_temp_dir());
  44. $packageDir = dirname((__DIR__));
  45. $preLoader = $packageDir . '/vendor/bin/classpreloader.php';
  46. $config = $appDir . '/var/lib/preloader/config.php';
  47. $output = $appDir . '/var/tmp/preloader/preload.php';
  48. $minOutput = $appDir . '/var/tmp/preloader/preload.min.php';
  49. $compilePreloader = "php {$preLoader} compile --strip_comments=0 --config={$config} --output={$output}";
  50. error_log($compilePreloader);
  51. passthru($compilePreloader);
  52. $lint = "php -l {$output}";
  53. error_log($lint);
  54. passthru($lint);
  55. /** @todo "min" loader */
  56. // file_put_contents($minOutput, php_strip_whitespace(file_get_contents($output)));
  57. // error_log("bear.compile preload.min.php -> {$minOutput}");
  58. }
  59. function compileObjectGraph($appDir)
  60. {
  61. $app = require $appDir . '/bootstrap/instance.php';
  62. /** @var $app \BEAR\Package\Provide\Application\AbstractApp */
  63. $res = 0;
  64. $classes = [];
  65. foreach ($app->resource as $meta) {
  66. $ro = $app->resource->newInstance($meta->uri);
  67. $classes[] = $class = get_class($ro);
  68. printf("%s -> %s" . PHP_EOL, $meta->uri, $class);
  69. $res++;
  70. }
  71. error_log($meta->uri . "{$res} resources created.");
  72. return $classes;
  73. }
  74. function compileAnnotation(array $classes, $appDir)
  75. {
  76. $reader = \Ray\Di\Injector::create([
  77. new \BEAR\Sunday\Module\Constant\NamedModule(['tmp_dir' => $appDir . '/tmp/cache']),
  78. new \BEAR\Sunday\Module\Cache\CacheModule,
  79. new \BEAR\Sunday\Module\Code\CachedAnnotationModule
  80. ])->getInstance('Doctrine\Common\Annotations\Reader');
  81. /** @var $reader \Doctrine\Common\Annotations\Reader' */
  82. foreach ($classes as $class) {
  83. if ((new \ReflectionClass($class))->implementsInterface('Ray\Aop\WeavedInterface')) {
  84. $class = get_parent_class($class);
  85. }
  86. $refClass = new \ReflectionClass($class);
  87. $annotations = $reader->getClassAnnotations($refClass);
  88. if ($annotations) {
  89. $annotationArr = [];
  90. foreach ($annotations as $annotation) {
  91. $annotationArr[] = get_class($annotation);
  92. }
  93. printf("%s @%s" . PHP_EOL, $class, implode(', ', $annotationArr));
  94. }
  95. // method
  96. $methods = $refClass->getMethods();
  97. foreach ($methods as $method) {
  98. $annotations = $reader->getMethodAnnotations($method);
  99. if ($annotations) {
  100. $annotationArr = [];
  101. foreach ($annotations as $annotation) {
  102. $annotationArr[] = get_class($annotation);
  103. }
  104. printf("%s::%s -> @%s" . PHP_EOL, $method->class, $method->name, implode(', ', $annotationArr));
  105. }
  106. }
  107. }
  108. }
  109. function error($msg = 'Usage: php [-d apc.enable_cli=1 -d apc.stat=0] bear.compiler <app-dir>')
  110. {
  111. error_log($msg);
  112. exit(1);
  113. }
  114. }