Skip to content

Commit

Permalink
Slight cleanup & micro-optimization
Browse files Browse the repository at this point in the history
  • Loading branch information
BelleNottelling committed May 1, 2024
1 parent 52e1d36 commit f1ed9e4
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 32 deletions.
29 changes: 14 additions & 15 deletions src/AntCMS/Cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,25 +18,24 @@ class Cache

public static function setup(array $allowed = []): void
{
// Setup the caching system
if ($allowed === []) {
self::$adapter = new ArrayAdapter();
} else {
$adapters = [];
$adapters = [];

// Add the APCu adapter first if allowed & supported
if (in_array('apcu', $allowed) && ApcuAdapter::isSupported()) {
$adapters[] = new ApcuAdapter('AntCMS_' . hash(HASH_ALGO, PATH_ROOT), self::$shortLifespan);
}
// Register cache adapters in order of fastest to slowest
if (in_array('apcu', $allowed) && ApcuAdapter::isSupported()) {
$adapters[] = new ApcuAdapter('AntCMS_' . hash(HASH_ALGO, PATH_ROOT), self::$shortLifespan);
}

// Add the PHP files adapter if allowed & supported
if (in_array('php_file', $allowed) && PhpFilesAdapter::isSupported()) {
$adapters[] = new PhpFilesAdapter('php_files', self::$mediumLifespan, PATH_CACHE);
}
if (in_array('php_file', $allowed) && PhpFilesAdapter::isSupported()) {
$adapters[] = new PhpFilesAdapter('php_files', self::$mediumLifespan, PATH_CACHE);
}

// We will always have the file system adapter
if (in_array('filesystem', $allowed)) {
$adapters[] = new FilesystemAdapter('filesystem', self::$longLifespan, PATH_CACHE);
}

if ($adapters === []) {
self::$adapter = new ArrayAdapter();
} else {
self::$adapter = new ChainAdapter($adapters);
}
}
Expand Down Expand Up @@ -75,6 +74,6 @@ public static function createCacheKey(string $content, string $salt = 'cache'):
public static function createCacheKeyFile(string $filePath, string $salt = 'cache'): string
{
$differentiator = filemtime($filePath) ?: hash_file(HASH_ALGO, $filePath);
return hash(HASH_ALGO, $filePath) . ".$differentiator.$salt";
return hash(HASH_ALGO, $filePath . ".$differentiator.$salt");
}
}
15 changes: 2 additions & 13 deletions src/AntCMS/Twig.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ class Twig
private static ?Environment $twigEnvironment = null;
private static ?string $theme = null;

public static function registerTwig(?Environment $twigEnvironment = null): void
public static function registerTwig(?Environment $twigEnvironment = null, ?string $theme = null): void
{
self::$theme = Config::get('activeTheme');
self::$theme = $theme ?? Config::get('activeTheme');

if (!is_null($twigEnvironment)) {
self::$twigEnvironment = $twigEnvironment;
Expand All @@ -39,16 +39,8 @@ public static function registerTwig(?Environment $twigEnvironment = null): void
}
}

private static function doSelfCheck(): void
{
if (is_null(self::$twigEnvironment)) {
self::registerTwig();
}
}

public static function setArrayLoaderTemplate(string $name, string $template): void
{
self::doSelfCheck();
$loaders = self::$twigEnvironment->getLoader()->getLoaders(); /** @phpstan-ignore-line */
foreach ($loaders as $loader) {
if (method_exists($loader, 'setTemplate')) {
Expand All @@ -59,7 +51,6 @@ public static function setArrayLoaderTemplate(string $name, string $template): v

public static function addLoaderPath(string $path, string $namespace = FilesystemLoader::MAIN_NAMESPACE): void
{
self::doSelfCheck();
$loaders = self::$twigEnvironment->getLoader()->getLoaders(); /** @phpstan-ignore-line */
foreach ($loaders as $loader) {
if (method_exists($loader, 'addPath')) {
Expand All @@ -70,13 +61,11 @@ public static function addLoaderPath(string $path, string $namespace = Filesyste

public static function templateExists(string $name): bool
{
self::doSelfCheck();
return self::$twigEnvironment->getLoader()->exists($name);
}

public static function render(string $template, array $data = []): string
{
self::doSelfCheck();
return self::$twigEnvironment->render($template, $data);
}
}
10 changes: 6 additions & 4 deletions src/Bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

use AntCMS\Config;
use AntCMS\Cache;
use AntCMS\Twig;

define('START', hrtime(true));

Expand Down Expand Up @@ -29,11 +30,9 @@
require_once __DIR__ . DIRECTORY_SEPARATOR . 'Vendor' . DIRECTORY_SEPARATOR . 'autoload.php';

// Setup and register AntLoader
$classMapPath = PATH_CACHE . DIRECTORY_SEPARATOR . 'classMap.php';
$loader = new \AntCMS\AntLoader(['path' => $classMapPath]);
$loader = new \AntCMS\AntLoader(['path' => PATH_CACHE . DIRECTORY_SEPARATOR . 'classMap.php']);
$loader->addNamespace('AntCMS\\', __DIR__ . DIRECTORY_SEPARATOR . 'AntCMS');
$loader->addNamespace('AntCMS\\Plugins\\', __DIR__ . DIRECTORY_SEPARATOR . 'Plugins');

$loader->checkClassMap();
$loader->register(true);

Expand All @@ -50,5 +49,8 @@
define('BASE_URL', $config['baseUrl']);
define('DEBUG_LEVEL', $config['debugLevel']);

// Setup the cache item
// Setup our cache adapter
Cache::setup($config['performance']['allowedCacheMethods'] ?? ['acpu', 'php_files', 'filesystem']);

// Setup our twig enviroment
Twig::registerTwig(null, $config['activeTheme']);

0 comments on commit f1ed9e4

Please sign in to comment.