From 97ccbe9d6df42046d2e87f39abf1a328ff091ec0 Mon Sep 17 00:00:00 2001 From: WalterWoshid Date: Wed, 1 Nov 2023 16:22:44 +0100 Subject: [PATCH 1/4] Added Production/Development environment --- src/CodeTransformerKernel.php | 24 ++++++++++++++-- src/Core/AutoloadInterceptor/ClassLoader.php | 20 ++++++++++++-- src/Core/Options.php | 29 ++++++++++++++++---- src/Core/Options/Environment.php | 21 ++++++++++++++ 4 files changed, 84 insertions(+), 10 deletions(-) create mode 100644 src/Core/Options/Environment.php diff --git a/src/CodeTransformerKernel.php b/src/CodeTransformerKernel.php index 54ae496..49a0a0e 100644 --- a/src/CodeTransformerKernel.php +++ b/src/CodeTransformerKernel.php @@ -9,6 +9,7 @@ use Okapi\CodeTransformer\Core\DI; use Okapi\CodeTransformer\Core\Exception\Kernel\DirectKernelInitializationException; use Okapi\CodeTransformer\Core\Options; +use Okapi\CodeTransformer\Core\Options\Environment; use Okapi\CodeTransformer\Core\StreamFilter; use Okapi\Singleton\Singleton; @@ -18,9 +19,12 @@ * This class is the heart of the Code Transformer library. * It manages an environment for Code Transformation. * - * 1. Extends this class and define a list of transformers in the - * `$transformers` property. - * 2. Call the `init()` method early in the application lifecycle. + * 1. Extend this class and define a list of transformers in the + * {@link $transformers} property. + * 2. Call the {@link init()} method early in the application lifecycle. + * + * If you want to modify the kernel options dynamically, override the + * {@link configureOptions()} method. */ abstract class CodeTransformerKernel { @@ -80,6 +84,19 @@ public function __construct() {} */ protected bool $debug = false; + /** + * The environment in which the application is running. + *
Default: {@link Environment::DEVELOPMENT}

+ * + * If {@link Environment::PRODUCTION}, the cache will not be checked for + * updates (better performance).
+ * If {@link Environment::DEVELOPMENT}, the cache will be checked for + * updates (better development experience). + * + * @var Environment + */ + protected Environment $environment = Environment::DEVELOPMENT; + /** * Throw an exception if the kernel is initialized twice. *
Default: {@link false}
@@ -163,6 +180,7 @@ protected function preInit(): void cacheDir: $this->cacheDir, cacheFileMode: $this->cacheFileMode, debug: $this->debug, + environment: $this->environment, ); // Add the transformers diff --git a/src/Core/AutoloadInterceptor/ClassLoader.php b/src/Core/AutoloadInterceptor/ClassLoader.php index 631b51f..38b9280 100644 --- a/src/Core/AutoloadInterceptor/ClassLoader.php +++ b/src/Core/AutoloadInterceptor/ClassLoader.php @@ -8,6 +8,7 @@ use Okapi\CodeTransformer\Core\Cache\CacheStateManager; use Okapi\CodeTransformer\Core\Matcher\TransformerMatcher; use Okapi\CodeTransformer\Core\Options; +use Okapi\CodeTransformer\Core\Options\Environment; use Okapi\CodeTransformer\Core\StreamFilter; use Okapi\CodeTransformer\Core\StreamFilter\FilterInjector; use Okapi\Path\Path; @@ -108,13 +109,28 @@ public function findFile($namespacedClass): false|string // Query cache state $cacheState = $this->cacheStateManager->queryCacheState($filePath); - // Check if the file is cached and up to date - if ($cacheState?->isFresh() && !$this->options->isDebug()) { + // When debugging, bypass the caching mechanism + if ($this->options->isDebug()) { + // ... + } + + // In production mode, use the cache without checking if it is fresh + elseif ($this->options->getEnvironment() === Environment::PRODUCTION + && $cacheState + ) { // Use the cached file if transformations have been applied // Or return the original file if no transformations have been applied return $cacheState->getFilePath() ?? $filePath; } + // In development mode, check if the cache is fresh + elseif ($this->options->getEnvironment() === Environment::DEVELOPMENT + && $cacheState + && $cacheState->isFresh() + ) { + return $cacheState->getFilePath() ?? $filePath; + } + // Check if the class should be transformed if (!$this->transformerMatcher->match($namespacedClass, $filePath)) { diff --git a/src/Core/Options.php b/src/Core/Options.php index eacaeab..b2555f5 100644 --- a/src/Core/Options.php +++ b/src/Core/Options.php @@ -4,6 +4,7 @@ use Composer\Autoload\ClassLoader; use Okapi\CodeTransformer\CodeTransformerKernel; +use Okapi\CodeTransformer\Core\Options\Environment; use Okapi\Path\Path; use ReflectionClass; @@ -45,6 +46,13 @@ class Options implements ServiceInterface */ private bool $debug; + /** + * The environment in which the application is running. + * + * @var Environment + */ + private Environment $environment; + // endregion /** @@ -63,12 +71,14 @@ class Options implements ServiceInterface * * @param string|null $cacheDir * @param int|null $cacheFileMode - * @param bool|null $debug + * @param bool $debug + * @param Environment $environment */ public function setOptions( - ?string $cacheDir, - ?int $cacheFileMode, - ?bool $debug, + ?string $cacheDir, + ?int $cacheFileMode, + bool $debug, + Environment $environment, ): void { $composerRef = new ReflectionClass(ClassLoader::class); $composerDir = $composerRef->getFileName(); @@ -77,7 +87,8 @@ public function setOptions( $this->appDir = $rootDir; $this->cacheDir = $cacheDir ?? Path::join($rootDir, $this->defaultCacheDir); $this->cacheFileMode = $cacheFileMode ?? (0777 & ~umask()); - $this->debug = $debug ?? false; + $this->debug = $debug; + $this->environment = $environment; } // endregion @@ -125,4 +136,12 @@ public function isDebug(): bool { return $this->debug; } + + /** + * Get the environment in which the application is running. + */ + public function getEnvironment(): Environment + { + return $this->environment; + } } diff --git a/src/Core/Options/Environment.php b/src/Core/Options/Environment.php new file mode 100644 index 0000000..eaaacd1 --- /dev/null +++ b/src/Core/Options/Environment.php @@ -0,0 +1,21 @@ + Date: Wed, 1 Nov 2023 16:23:18 +0100 Subject: [PATCH 2/4] Added "configureOptions()" --- src/CodeTransformerKernel.php | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/src/CodeTransformerKernel.php b/src/CodeTransformerKernel.php index 49a0a0e..17616fc 100644 --- a/src/CodeTransformerKernel.php +++ b/src/CodeTransformerKernel.php @@ -152,6 +152,7 @@ public static function init(): void // Initialize the services $instance->preInit(); + $instance->configureOptions(); $instance->registerServices(); $instance->registerAutoloadInterceptor(); @@ -187,6 +188,16 @@ protected function preInit(): void $this->transformerManager->addTransformers($this->transformers); } + /** + * Configure or modify kernel options. + * + * @return void + */ + protected function configureOptions(): void + { + // Override this method to configure the options dynamically + } + /** * Register the services. * From b7ca1ceecec98b2428f6c55d9bd770eba3024820 Mon Sep 17 00:00:00 2001 From: WalterWoshid Date: Wed, 1 Nov 2023 16:23:41 +0100 Subject: [PATCH 3/4] Bumped Composer version --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 638b3c8..f25784d 100644 --- a/composer.json +++ b/composer.json @@ -1,7 +1,7 @@ { "name": "okapi/code-transformer", "description": "PHP Code Transformer is a PHP library that allows you to modify and transform the source code of a loaded PHP class.", - "version": "1.3.2", + "version": "1.3.3", "type": "library", "homepage": "https://github.com/okapi-web/php-code-transformer", "license": "MIT", From 361052144a502b29be9c3efb9684a8a897034011 Mon Sep 17 00:00:00 2001 From: WalterWoshid Date: Wed, 1 Nov 2023 16:24:44 +0100 Subject: [PATCH 4/4] Added "--display-notices" option to test scripts --- .github/workflows/tests.yml | 2 +- composer.json | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 9f03a6c..75b9d5f 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -45,7 +45,7 @@ jobs: run: composer install --prefer-dist --no-progress - name: PHPUnit Tests - run: vendor/bin/phpunit --coverage-clover ./tests/coverage.xml + run: vendor/bin/phpunit --coverage-clover ./tests/coverage.xml --display-notices - name: Upload coverage reports to Codecov uses: codecov/codecov-action@v3 diff --git a/composer.json b/composer.json index f25784d..5e0fc3f 100644 --- a/composer.json +++ b/composer.json @@ -19,8 +19,8 @@ "php-code" ], "scripts": { - "test": "phpunit", - "test-coverage": "phpunit --coverage-html tests/coverage" + "test": "phpunit --display-notices", + "test-coverage": "phpunit --coverage-html tests/coverage --display-notices" }, "require": { "php": ">=8.1",