-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #352 from koriym/scrutinzer
Refactor Compiler
- Loading branch information
Showing
11 changed files
with
665 additions
and
361 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,194 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace BEAR\Package\Compiler; | ||
|
||
use ArrayObject; | ||
use BEAR\AppMeta\Meta; | ||
use BEAR\Package\Provide\Error\NullPage; | ||
use BEAR\Resource\Uri; | ||
use BEAR\Sunday\Extension\Application\AppInterface; | ||
use Ray\Di\InjectorInterface; | ||
use ReflectionClass; | ||
|
||
use function assert; | ||
use function class_exists; | ||
use function file_exists; | ||
use function in_array; | ||
use function interface_exists; | ||
use function is_float; | ||
use function is_int; | ||
use function memory_get_peak_usage; | ||
use function microtime; | ||
use function number_format; | ||
use function preg_quote; | ||
use function preg_replace; | ||
use function printf; | ||
use function property_exists; | ||
use function realpath; | ||
use function sprintf; | ||
use function strpos; | ||
use function trait_exists; | ||
|
||
use const PHP_EOL; | ||
|
||
class CompileAutoload | ||
{ | ||
/** @var string */ | ||
private $appDir; | ||
|
||
/** @var string */ | ||
private $context; | ||
|
||
/** @var Meta */ | ||
private $appMeta; | ||
|
||
/** @var ArrayObject<int, string> */ | ||
private $overwritten; | ||
|
||
/** @var ArrayObject<int, string> */ | ||
private $classes; | ||
|
||
/** @var InjectorInterface */ | ||
private $injector; | ||
|
||
/** @var FilePutContents */ | ||
private $filePutContents; | ||
|
||
/** | ||
* @param ArrayObject<int, string> $overwritten | ||
* @param ArrayObject<int, string> $classes | ||
*/ | ||
public function __construct( | ||
InjectorInterface $injector, | ||
FilePutContents $filePutContents, | ||
Meta $appMeta, | ||
ArrayObject $overwritten, | ||
ArrayObject $classes, | ||
string $appDir, | ||
string $context | ||
) { | ||
$this->appDir = $appDir; | ||
$this->context = $context; | ||
$this->appMeta = $appMeta; | ||
$this->overwritten = $overwritten; | ||
$this->classes = $classes; | ||
$this->injector = $injector; | ||
$this->filePutContents = $filePutContents; | ||
} | ||
|
||
public function getFileInfo(string $filename): string | ||
{ | ||
if (in_array($filename, (array) $this->overwritten, true)) { | ||
return $filename . ' (overwritten)'; | ||
} | ||
|
||
return $filename; | ||
} | ||
|
||
public function __invoke(): int | ||
{ | ||
echo PHP_EOL; | ||
$this->invokeTypicalRequest(); | ||
/** @var list<string> $classes */ | ||
$classes = (array) $this->classes; | ||
$paths = $this->getPaths($classes); | ||
$autolaod = $this->saveAutoloadFile($this->appMeta->appDir, $paths); | ||
$start = $_SERVER['REQUEST_TIME_FLOAT']; | ||
assert(is_float($start)); | ||
$time = number_format(microtime(true) - $start, 2); | ||
$memory = number_format(memory_get_peak_usage() / (1024 * 1024), 3); | ||
printf("Compilation (2/2) took %f seconds and used %fMB of memory\n", $time, $memory); | ||
printf("autoload.php: %s\n", $this->getFileInfo($autolaod)); | ||
|
||
return 0; | ||
} | ||
|
||
/** | ||
* @param array<string> $classes | ||
* | ||
* @return array<string> | ||
*/ | ||
public function getPaths(array $classes): array | ||
{ | ||
$paths = []; | ||
foreach ($classes as $class) { | ||
// could be phpdoc tag by annotation loader | ||
if ($this->isNotAutoloadble($class)) { | ||
continue; | ||
} | ||
|
||
/** @var class-string $class */ | ||
$filePath = (string) (new ReflectionClass($class))->getFileName(); // @phpstan-ignore-line | ||
if (! $this->isNotCompileFile($filePath)) { | ||
continue; // @codeCoverageIgnore | ||
} | ||
|
||
$paths[] = $this->getRelativePath($this->appDir, $filePath); | ||
} | ||
|
||
return $paths; | ||
} | ||
|
||
/** | ||
* @param array<string> $paths | ||
*/ | ||
public function saveAutoloadFile(string $appDir, array $paths): string | ||
{ | ||
$requiredFile = ''; | ||
foreach ($paths as $path) { | ||
$requiredFile .= sprintf( | ||
"require %s';\n", | ||
$this->getRelativePath($appDir, $path) | ||
); | ||
} | ||
|
||
$autoloadFile = sprintf("<?php | ||
// %s autoload | ||
%s | ||
require __DIR__ . '/vendor/autoload.php'; | ||
", $this->context, $requiredFile); | ||
$fileName = realpath($appDir) . '/autoload.php'; | ||
($this->filePutContents)($fileName, $autoloadFile); | ||
|
||
return $fileName; | ||
} | ||
|
||
/** | ||
* @psalm-suppress MixedFunctionCall | ||
* @psalm-suppress NoInterfaceProperties | ||
*/ | ||
public function invokeTypicalRequest(): void | ||
{ | ||
$app = $this->injector->getInstance(AppInterface::class); | ||
assert($app instanceof AppInterface); | ||
assert(property_exists($app, 'resource')); | ||
$ro = new NullPage(); | ||
$ro->uri = new Uri('app://self/'); | ||
/** @psalm-suppress MixedMethodCall */ | ||
$app->resource->get->object($ro)(); | ||
} | ||
|
||
private function isNotAutoloadble(string $class): bool | ||
{ | ||
return ! class_exists($class, false) && ! interface_exists($class, false) && ! trait_exists($class, false); | ||
} | ||
|
||
private function isNotCompileFile(string $filePath): bool | ||
{ | ||
return file_exists($filePath) || is_int(strpos($filePath, 'phar')); | ||
} | ||
|
||
private function getRelativePath(string $rootDir, string $file): string | ||
{ | ||
$dir = (string) realpath($rootDir); | ||
if (strpos($file, $dir) !== false) { | ||
return (string) preg_replace('#^' . preg_quote($dir, '#') . '#', "__DIR__ . '", $file); | ||
} | ||
|
||
return $file; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace BEAR\Package\Compiler; | ||
|
||
use BEAR\Resource\Exception\ParameterException; | ||
use BEAR\Resource\NamedParameterInterface; | ||
use Doctrine\Common\Annotations\Reader; | ||
use ReflectionClass; | ||
|
||
use function in_array; | ||
use function is_callable; | ||
use function sprintf; | ||
use function substr; | ||
|
||
final class CompileClassMetaInfo | ||
{ | ||
/** | ||
* Save annotation and method meta information | ||
* | ||
* @param class-string<T> $className | ||
* | ||
* @template T | ||
*/ | ||
public function __invoke(Reader $reader, NamedParameterInterface $namedParams, string $className): void | ||
{ | ||
$class = new ReflectionClass($className); | ||
$instance = $class->newInstanceWithoutConstructor(); | ||
if (! $instance instanceof $className) { | ||
return; // @codeCoverageIgnore | ||
} | ||
|
||
$reader->getClassAnnotations($class); | ||
$methods = $class->getMethods(); | ||
$log = sprintf('M %s:', $className); | ||
foreach ($methods as $method) { | ||
$methodName = $method->getName(); | ||
if ($this->isMagicMethod($methodName)) { | ||
continue; | ||
} | ||
|
||
if (substr($methodName, 0, 2) === 'on') { | ||
$log .= sprintf(' %s', $methodName); | ||
$this->saveNamedParam($namedParams, $instance, $methodName); | ||
} | ||
|
||
// method annotation | ||
$reader->getMethodAnnotations($method); | ||
$log .= sprintf('@ %s', $methodName); | ||
} | ||
} | ||
|
||
private function isMagicMethod(string $method): bool | ||
{ | ||
return in_array($method, ['__sleep', '__wakeup', 'offsetGet', 'offsetSet', 'offsetExists', 'offsetUnset', 'count', 'ksort', 'asort', 'jsonSerialize'], true); | ||
} | ||
|
||
private function saveNamedParam(NamedParameterInterface $namedParameter, object $instance, string $method): void | ||
{ | ||
// named parameter | ||
if (! in_array($method, ['onGet', 'onPost', 'onPut', 'onPatch', 'onDelete', 'onHead'], true)) { | ||
return; // @codeCoverageIgnore | ||
} | ||
|
||
$callable = [$instance, $method]; | ||
if (! is_callable($callable)) { | ||
return; // @codeCoverageIgnore | ||
} | ||
|
||
try { | ||
$namedParameter->getParameters($callable, []); | ||
} catch (ParameterException $e) { | ||
return; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace BEAR\Package\Compiler; | ||
|
||
use Ray\Di\AbstractModule; | ||
|
||
use function array_keys; | ||
use function assert; | ||
use function is_int; | ||
use function sort; | ||
use function strpos; | ||
use function substr; | ||
|
||
final class CompileDependencies | ||
{ | ||
/** @var NewInstance */ | ||
private $newInstance; | ||
|
||
public function __construct(NewInstance $newInstance) | ||
{ | ||
$this->newInstance = $newInstance; | ||
} | ||
|
||
public function __invoke(AbstractModule $module): AbstractModule | ||
{ | ||
$container = $module->getContainer()->getContainer(); | ||
$dependencies = array_keys($container); | ||
sort($dependencies); | ||
foreach ($dependencies as $dependencyIndex) { | ||
$pos = strpos((string) $dependencyIndex, '-'); | ||
assert(is_int($pos)); | ||
$interface = substr((string) $dependencyIndex, 0, $pos); | ||
$name = substr((string) $dependencyIndex, $pos + 1); | ||
($this->newInstance)($interface, $name); | ||
} | ||
|
||
return $module; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace BEAR\Package\Compiler; | ||
|
||
use BEAR\AppMeta\AbstractAppMeta; | ||
use BEAR\Resource\NamedParameterInterface; | ||
use BEAR\Sunday\Extension\Application\AppInterface; | ||
use Doctrine\Common\Annotations\Reader; | ||
use Ray\Di\InjectorInterface; | ||
|
||
use function assert; | ||
use function class_exists; | ||
|
||
final class CompileDiScripts | ||
{ | ||
/** @var CompileClassMetaInfo */ | ||
private $compilerScanClass; | ||
|
||
/** @var InjectorInterface */ | ||
private $injector; | ||
|
||
public function __construct(CompileClassMetaInfo $compilerScanClass, InjectorInterface $injector) | ||
{ | ||
$this->compilerScanClass = $compilerScanClass; | ||
$this->injector = $injector; | ||
} | ||
|
||
public function __invoke(AbstractAppMeta $appMeta): void | ||
{ | ||
$reader = $this->injector->getInstance(Reader::class); | ||
assert($reader instanceof Reader); | ||
$namedParams = $this->injector->getInstance(NamedParameterInterface::class); | ||
assert($namedParams instanceof NamedParameterInterface); | ||
// create DI factory class and AOP compiled class for all resources and save $app cache. | ||
$app = $this->injector->getInstance(AppInterface::class); | ||
assert($app instanceof AppInterface); | ||
|
||
// check resource injection and create annotation cache | ||
$metas = $appMeta->getResourceListGenerator(); | ||
/** @var array{0: string, 1:string} $meta */ | ||
foreach ($metas as $meta) { | ||
[$className] = $meta; | ||
assert(class_exists($className)); | ||
$this->scanClass($reader, $namedParams, $className); | ||
} | ||
} | ||
|
||
/** | ||
* Save annotation and method meta information | ||
* | ||
* @param class-string<T> $className | ||
* | ||
* @template T | ||
*/ | ||
private function scanClass(Reader $reader, NamedParameterInterface $namedParams, string $className): void | ||
{ | ||
($this->compilerScanClass)($reader, $namedParams, $className); | ||
} | ||
} |
Oops, something went wrong.