Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/Modules-and-Initialization.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ Environment cache behavior
- When it’s used: only in `production` and `staging` environment types (`wp_get_environment_type()`).
- How to clear: delete the `class-loader-cache` folder; it will be rebuilt on next discovery.
- How to disable in development: use `development` or `local` environment types, or define `VIP_GO_APP_ENVIRONMENT` to skip the cache.
- How to disable for hosts that don't support file-based caching: `define( 'TENUP_FRAMEWORK_DISABLE_CLASS_CACHE', true );` to skip caching altogether.

Hooks
- Action: `tenup_framework_module_init__{slug}` — fires before each module’s `register()` runs.
Expand Down
23 changes: 22 additions & 1 deletion src/ModuleInitialization.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public function get_classes( string $dir ): array {
$class_finder->classes();

// If we are in production or staging, cache the class loader to improve performance.
if ( ! defined( 'VIP_GO_APP_ENVIRONMENT' ) && in_array( wp_get_environment_type(), [ 'production', 'staging' ], true ) ) {
if ( $this->should_use_cache() ) {
$class_finder->withCache(
__NAMESPACE__,
new FileDiscoverCacheDriver( $dir . '/class-loader-cache' )
Expand All @@ -82,6 +82,27 @@ public function get_classes( string $dir ): array {
return $classes;
}

/**
* Should we set up and use the class cache?
*
* @return bool
*/
protected function should_use_cache(): bool {
if ( defined( 'VIP_GO_APP_ENVIRONMENT' ) ) {
return false;
}

if ( ! in_array( wp_get_environment_type(), [ 'production', 'staging' ], true ) ) {
return false;
}

if ( defined( 'TENUP_FRAMEWORK_DISABLE_CLASS_CACHE' ) && true === TENUP_FRAMEWORK_DISABLE_CLASS_CACHE ) {
return false;
}

return true;
}

/**
* Check if the directory exists.
*
Expand Down
72 changes: 72 additions & 0 deletions tests/ModuleInitializationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
namespace TenupFrameworkTests;

use PHPUnit\Framework\TestCase;
use function Brain\Monkey\Functions\stubs;

/**
* Test Class
Expand Down Expand Up @@ -147,4 +148,75 @@ public function testIsClassFullyLoadable() {
$this->assertInstanceOf( 'ReflectionClass', $module_init->get_fully_loadable_class( '\TenupFrameworkTestClasses\Loadable\ChildClass' ) );
$this->assertFalse( $module_init->get_fully_loadable_class( '\TenupFrameworkTestClasses\Loadable\InvalidChildClass' ) );
}


/**
* Ensure it returns false if VIP_GO_APP_ENVIRONMENT is defined.
*
* @return void
*/
public function test_should_use_cache_returns_false_when_vip_env_is_defined() {
define( 'VIP_GO_APP_ENVIRONMENT', true );
$module_init = \TenupFramework\ModuleInitialization::instance();
$reflection = new \ReflectionClass( $module_init );
$method = $reflection->getMethod( 'should_use_cache' );
$method->setAccessible( true );

$this->assertFalse( $method->invoke( $module_init ) );
}

/**
* Ensure it returns false in non-production or staging environments.
*
* @return void
*/
public function test_should_use_cache_returns_false_in_non_production_or_staging_env() {
stubs(
[
'wp_get_environment_type' => 'development',
]
);

$module_init = \TenupFramework\ModuleInitialization::instance();
$reflection = new \ReflectionClass( $module_init );
$method = $reflection->getMethod( 'should_use_cache' );
$method->setAccessible( true );

$this->assertFalse( $method->invoke( $module_init ) );
}

/**
* Ensure it returns false when TENUP_FRAMEWORK_DISABLE_CLASS_CACHE is defined.
*
* @return void
*/
public function test_should_use_cache_returns_false_when_disable_class_cache_is_defined() {
define( 'TENUP_FRAMEWORK_DISABLE_CLASS_CACHE', true );
$module_init = \TenupFramework\ModuleInitialization::instance();
$reflection = new \ReflectionClass( $module_init );
$method = $reflection->getMethod( 'should_use_cache' );
$method->setAccessible( true );

$this->assertFalse( $method->invoke( $module_init ) );
}

/**
* Ensure it returns true under default conditions.
*
* @return void
*/
public function test_should_use_cache_returns_true_under_default_conditions() {
stubs(
[
'wp_get_environment_type' => 'production',
]
);

$module_init = \TenupFramework\ModuleInitialization::instance();
$reflection = new \ReflectionClass( $module_init );
$method = $reflection->getMethod( 'should_use_cache' );
$method->setAccessible( true );

$this->assertTrue( $method->invoke( $module_init ) );
}
}