Skip to content

Commit ca49c58

Browse files
authored
Merge pull request #11: Added Production/Development environment option
2 parents fadb1f2 + 3610521 commit ca49c58

File tree

6 files changed

+99
-14
lines changed

6 files changed

+99
-14
lines changed

.github/workflows/tests.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ jobs:
4545
run: composer install --prefer-dist --no-progress
4646

4747
- name: PHPUnit Tests
48-
run: vendor/bin/phpunit --coverage-clover ./tests/coverage.xml
48+
run: vendor/bin/phpunit --coverage-clover ./tests/coverage.xml --display-notices
4949

5050
- name: Upload coverage reports to Codecov
5151
uses: codecov/codecov-action@v3

composer.json

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "okapi/code-transformer",
33
"description": "PHP Code Transformer is a PHP library that allows you to modify and transform the source code of a loaded PHP class.",
4-
"version": "1.3.2",
4+
"version": "1.3.3",
55
"type": "library",
66
"homepage": "https://github.com/okapi-web/php-code-transformer",
77
"license": "MIT",
@@ -19,8 +19,8 @@
1919
"php-code"
2020
],
2121
"scripts": {
22-
"test": "phpunit",
23-
"test-coverage": "phpunit --coverage-html tests/coverage"
22+
"test": "phpunit --display-notices",
23+
"test-coverage": "phpunit --coverage-html tests/coverage --display-notices"
2424
},
2525
"require": {
2626
"php": ">=8.1",

src/CodeTransformerKernel.php

+32-3
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use Okapi\CodeTransformer\Core\DI;
1010
use Okapi\CodeTransformer\Core\Exception\Kernel\DirectKernelInitializationException;
1111
use Okapi\CodeTransformer\Core\Options;
12+
use Okapi\CodeTransformer\Core\Options\Environment;
1213
use Okapi\CodeTransformer\Core\StreamFilter;
1314
use Okapi\Singleton\Singleton;
1415

@@ -18,9 +19,12 @@
1819
* This class is the heart of the Code Transformer library.
1920
* It manages an environment for Code Transformation.
2021
*
21-
* 1. Extends this class and define a list of transformers in the
22-
* `$transformers` property.
23-
* 2. Call the `init()` method early in the application lifecycle.
22+
* 1. Extend this class and define a list of transformers in the
23+
* {@link $transformers} property.
24+
* 2. Call the {@link init()} method early in the application lifecycle.
25+
*
26+
* If you want to modify the kernel options dynamically, override the
27+
* {@link configureOptions()} method.
2428
*/
2529
abstract class CodeTransformerKernel
2630
{
@@ -80,6 +84,19 @@ public function __construct() {}
8084
*/
8185
protected bool $debug = false;
8286

87+
/**
88+
* The environment in which the application is running.
89+
* <br><b>Default:</b> {@link Environment::DEVELOPMENT}<br><br>
90+
*
91+
* If {@link Environment::PRODUCTION}, the cache will not be checked for
92+
* updates (better performance).<br>
93+
* If {@link Environment::DEVELOPMENT}, the cache will be checked for
94+
* updates (better development experience).
95+
*
96+
* @var Environment
97+
*/
98+
protected Environment $environment = Environment::DEVELOPMENT;
99+
83100
/**
84101
* Throw an exception if the kernel is initialized twice.
85102
* <br><b>Default:</b> {@link false}<br>
@@ -135,6 +152,7 @@ public static function init(): void
135152

136153
// Initialize the services
137154
$instance->preInit();
155+
$instance->configureOptions();
138156
$instance->registerServices();
139157
$instance->registerAutoloadInterceptor();
140158

@@ -163,12 +181,23 @@ protected function preInit(): void
163181
cacheDir: $this->cacheDir,
164182
cacheFileMode: $this->cacheFileMode,
165183
debug: $this->debug,
184+
environment: $this->environment,
166185
);
167186

168187
// Add the transformers
169188
$this->transformerManager->addTransformers($this->transformers);
170189
}
171190

191+
/**
192+
* Configure or modify kernel options.
193+
*
194+
* @return void
195+
*/
196+
protected function configureOptions(): void
197+
{
198+
// Override this method to configure the options dynamically
199+
}
200+
172201
/**
173202
* Register the services.
174203
*

src/Core/AutoloadInterceptor/ClassLoader.php

+18-2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Okapi\CodeTransformer\Core\Cache\CacheStateManager;
99
use Okapi\CodeTransformer\Core\Matcher\TransformerMatcher;
1010
use Okapi\CodeTransformer\Core\Options;
11+
use Okapi\CodeTransformer\Core\Options\Environment;
1112
use Okapi\CodeTransformer\Core\StreamFilter;
1213
use Okapi\CodeTransformer\Core\StreamFilter\FilterInjector;
1314
use Okapi\Path\Path;
@@ -108,13 +109,28 @@ public function findFile($namespacedClass): false|string
108109
// Query cache state
109110
$cacheState = $this->cacheStateManager->queryCacheState($filePath);
110111

111-
// Check if the file is cached and up to date
112-
if ($cacheState?->isFresh() && !$this->options->isDebug()) {
112+
// When debugging, bypass the caching mechanism
113+
if ($this->options->isDebug()) {
114+
// ...
115+
}
116+
117+
// In production mode, use the cache without checking if it is fresh
118+
elseif ($this->options->getEnvironment() === Environment::PRODUCTION
119+
&& $cacheState
120+
) {
113121
// Use the cached file if transformations have been applied
114122
// Or return the original file if no transformations have been applied
115123
return $cacheState->getFilePath() ?? $filePath;
116124
}
117125

126+
// In development mode, check if the cache is fresh
127+
elseif ($this->options->getEnvironment() === Environment::DEVELOPMENT
128+
&& $cacheState
129+
&& $cacheState->isFresh()
130+
) {
131+
return $cacheState->getFilePath() ?? $filePath;
132+
}
133+
118134

119135
// Check if the class should be transformed
120136
if (!$this->transformerMatcher->match($namespacedClass, $filePath)) {

src/Core/Options.php

+24-5
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Composer\Autoload\ClassLoader;
66
use Okapi\CodeTransformer\CodeTransformerKernel;
7+
use Okapi\CodeTransformer\Core\Options\Environment;
78
use Okapi\Path\Path;
89
use ReflectionClass;
910

@@ -45,6 +46,13 @@ class Options implements ServiceInterface
4546
*/
4647
private bool $debug;
4748

49+
/**
50+
* The environment in which the application is running.
51+
*
52+
* @var Environment
53+
*/
54+
private Environment $environment;
55+
4856
// endregion
4957

5058
/**
@@ -63,12 +71,14 @@ class Options implements ServiceInterface
6371
*
6472
* @param string|null $cacheDir
6573
* @param int|null $cacheFileMode
66-
* @param bool|null $debug
74+
* @param bool $debug
75+
* @param Environment $environment
6776
*/
6877
public function setOptions(
69-
?string $cacheDir,
70-
?int $cacheFileMode,
71-
?bool $debug,
78+
?string $cacheDir,
79+
?int $cacheFileMode,
80+
bool $debug,
81+
Environment $environment,
7282
): void {
7383
$composerRef = new ReflectionClass(ClassLoader::class);
7484
$composerDir = $composerRef->getFileName();
@@ -77,7 +87,8 @@ public function setOptions(
7787
$this->appDir = $rootDir;
7888
$this->cacheDir = $cacheDir ?? Path::join($rootDir, $this->defaultCacheDir);
7989
$this->cacheFileMode = $cacheFileMode ?? (0777 & ~umask());
80-
$this->debug = $debug ?? false;
90+
$this->debug = $debug;
91+
$this->environment = $environment;
8192
}
8293

8394
// endregion
@@ -125,4 +136,12 @@ public function isDebug(): bool
125136
{
126137
return $this->debug;
127138
}
139+
140+
/**
141+
* Get the environment in which the application is running.
142+
*/
143+
public function getEnvironment(): Environment
144+
{
145+
return $this->environment;
146+
}
128147
}

src/Core/Options/Environment.php

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
namespace Okapi\CodeTransformer\Core\Options;
4+
5+
/**
6+
* # Environment
7+
*
8+
* The environment in which the application is running.
9+
*/
10+
enum Environment
11+
{
12+
/**
13+
* Cache will not be checked for updates (better performance).
14+
*/
15+
case PRODUCTION;
16+
17+
/**
18+
* Cache will be checked for updates (better development experience).
19+
*/
20+
case DEVELOPMENT;
21+
}

0 commit comments

Comments
 (0)