| 123456789101112131415161718192021222324252627282930313233343536373839 |
- <?php
- /** Architectural guardrails required by the project: HTTP -> services -> Eloquent models. */
- require dirname(__DIR__) . '/vendor/autoload.php';
- $checks = 0;
- $assert = function (bool $condition, string $label) use (&$checks): void {
- $checks++;
- if (!$condition) {
- throw new RuntimeException($label);
- }
- };
- $root = dirname(__DIR__) . '/app/miniapi';
- foreach (glob($root . '/controller/*.php') as $path) {
- $source = file_get_contents($path);
- $assert(!preg_match('/::(?:query|where|find|transaction)\s*\(|\\\\model\\\\|getConnection\s*\(/', $source), basename($path) . ': controller must not query models');
- $class = new ReflectionClass('app\\miniapi\\controller\\' . basename($path, '.php'));
- $assert(count($class->getAttributes(\support\annotation\route\DisableDefaultRoute::class)) === 1, basename($path) . ': explicit routes only');
- }
- foreach (new RecursiveIteratorIterator(new RecursiveDirectoryIterator($root)) as $file) {
- if ($file->getExtension() !== 'php') {
- continue;
- }
- $source = file_get_contents($file->getPathname());
- $assert(!preg_match('/\b(?:Db|DB|PDO)::|\bnew\s+\\\\?PDO\b|->(?:selectRaw|whereRaw|statement|unprepared)\s*\(/', $source), $file->getFilename() . ': data access must use models');
- if (str_contains($file->getPathname(), DIRECTORY_SEPARATOR . 'service' . DIRECTORY_SEPARATOR)) {
- $assert(!preg_match('/use\s+(?:support|Webman\\\\Http)\\\\(?:Request|Response);/', $source), $file->getFilename() . ': service must not depend on HTTP');
- }
- }
- foreach (['start', 'checkin', 'draft', 'complete', 'exception'] as $action) {
- $assert(\app\miniapi\service\driver\TaskActionFactory::make($action) instanceof \app\miniapi\service\driver\TaskAction, 'driver action implements command contract');
- }
- try {
- \app\miniapi\service\driver\TaskActionFactory::make('unsupported');
- throw new RuntimeException('Unknown driver action accepted');
- } catch (\app\miniapi\exception\ApiException $error) {
- $assert($error->getCode() === 40401, 'factory rejects unsupported action');
- }
- echo "PASS: $checks architecture checks.\n";
|