Skip to content

Commit

Permalink
Merge pull request #11: Added Production/Development environment option
Browse files Browse the repository at this point in the history
  • Loading branch information
WalterWoshid authored Nov 1, 2023
2 parents fadb1f2 + 3610521 commit ca49c58
Show file tree
Hide file tree
Showing 6 changed files with 99 additions and 14 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
@@ -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",
Expand All @@ -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",
Expand Down
35 changes: 32 additions & 3 deletions src/CodeTransformerKernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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
{
Expand Down Expand Up @@ -80,6 +84,19 @@ public function __construct() {}
*/
protected bool $debug = false;

/**
* The environment in which the application is running.
* <br><b>Default:</b> {@link Environment::DEVELOPMENT}<br><br>
*
* If {@link Environment::PRODUCTION}, the cache will not be checked for
* updates (better performance).<br>
* 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.
* <br><b>Default:</b> {@link false}<br>
Expand Down Expand Up @@ -135,6 +152,7 @@ public static function init(): void

// Initialize the services
$instance->preInit();
$instance->configureOptions();
$instance->registerServices();
$instance->registerAutoloadInterceptor();

Expand Down Expand Up @@ -163,12 +181,23 @@ protected function preInit(): void
cacheDir: $this->cacheDir,
cacheFileMode: $this->cacheFileMode,
debug: $this->debug,
environment: $this->environment,
);

// Add the transformers
$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.
*
Expand Down
20 changes: 18 additions & 2 deletions src/Core/AutoloadInterceptor/ClassLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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)) {
Expand Down
29 changes: 24 additions & 5 deletions src/Core/Options.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Composer\Autoload\ClassLoader;
use Okapi\CodeTransformer\CodeTransformerKernel;
use Okapi\CodeTransformer\Core\Options\Environment;
use Okapi\Path\Path;
use ReflectionClass;

Expand Down Expand Up @@ -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

/**
Expand All @@ -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();
Expand All @@ -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
Expand Down Expand Up @@ -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;
}
}
21 changes: 21 additions & 0 deletions src/Core/Options/Environment.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace Okapi\CodeTransformer\Core\Options;

/**
* # Environment
*
* The environment in which the application is running.
*/
enum Environment
{
/**
* Cache will not be checked for updates (better performance).
*/
case PRODUCTION;

/**
* Cache will be checked for updates (better development experience).
*/
case DEVELOPMENT;
}

0 comments on commit ca49c58

Please sign in to comment.