From 9d222c88f436dc1953af80b53837f79c567fae6f Mon Sep 17 00:00:00 2001 From: Vlad Byndych Date: Mon, 19 Jun 2023 17:19:46 +0200 Subject: [PATCH 1/3] fix: updated parsing to PHP 8.0+ standard also covered case with unit tests so we can spot it next time. see: https://wiki.php.net/rfc/namespaced_names_as_token --- helpers/class.PhpTools.php | 4 ++- test/unit/helpers/PhpToolsTest.php | 44 ++++++++++++++++++++++++++++++ 2 files changed, 47 insertions(+), 1 deletion(-) create mode 100644 test/unit/helpers/PhpToolsTest.php diff --git a/helpers/class.PhpTools.php b/helpers/class.PhpTools.php index 746a4fa14..c6ae3eb78 100755 --- a/helpers/class.PhpTools.php +++ b/helpers/class.PhpTools.php @@ -36,13 +36,15 @@ class helpers_PhpTools */ public static function getClassInfo($file) { + $namespaceToken = PHP_VERSION_ID < 80000 ? T_STRING : T_NAME_QUALIFIED; + $buffer = file_get_contents($file); $tokens = @token_get_all($buffer); $class = $namespace = $buffer = ''; for ($i = 0; $i < count($tokens); $i++) { if ($tokens[$i][0] === T_NAMESPACE) { for ($j = $i + 1; $j < count($tokens); $j++) { - if ($tokens[$j][0] === T_STRING) { + if ($tokens[$j][0] === $namespaceToken) { $namespace .= '\\' . $tokens[$j][1]; } elseif ($tokens[$j] === '{' || $tokens[$j] === ';') { break; diff --git a/test/unit/helpers/PhpToolsTest.php b/test/unit/helpers/PhpToolsTest.php new file mode 100644 index 000000000..191641758 --- /dev/null +++ b/test/unit/helpers/PhpToolsTest.php @@ -0,0 +1,44 @@ +assertEquals('\oat\generis\tests\unit\helpers', $info['ns'], 'Namespace is wrong.'); + $this->assertEquals('PhpToolsTest', $info['class'], 'Class is wrong.'); + } + + public function testNamespaceAndClassLegacy(): void + { + $reflectionHelper = new \ReflectionClass(\helpers_PhpTools::class); + $info = \helpers_PhpTools::getClassInfo($reflectionHelper->getFileName()); + + $this->assertEquals('', $info['ns'], 'Namespace is wrong.'); + $this->assertEquals('helpers_PhpTools', $info['class'], 'Class is wrong.'); + } +} From 6d0717a446a695e520d53a57b52d19f214fa64c0 Mon Sep 17 00:00:00 2001 From: Vlad Byndych Date: Mon, 19 Jun 2023 17:20:45 +0200 Subject: [PATCH 2/3] feat: add new command which allow to warmup of the application cache. --- core/data/event/CacheWarmupEvent.php | 54 ++++++++++++++ scripts/tools/ApplicationCacheWarmup.php | 91 ++++++++++++++++++++++++ 2 files changed, 145 insertions(+) create mode 100644 core/data/event/CacheWarmupEvent.php create mode 100644 scripts/tools/ApplicationCacheWarmup.php diff --git a/core/data/event/CacheWarmupEvent.php b/core/data/event/CacheWarmupEvent.php new file mode 100644 index 000000000..6cfab9c91 --- /dev/null +++ b/core/data/event/CacheWarmupEvent.php @@ -0,0 +1,54 @@ +reports[] = $report; + + return $this; + } + + /** + * @return Report[] + */ + public function getReports(): array + { + return $this->reports; + } +} diff --git a/scripts/tools/ApplicationCacheWarmup.php b/scripts/tools/ApplicationCacheWarmup.php new file mode 100644 index 000000000..49d13141c --- /dev/null +++ b/scripts/tools/ApplicationCacheWarmup.php @@ -0,0 +1,91 @@ + 'h', + 'longPrefix' => 'help', + 'description' => 'Warmup TAO Cache', + ]; + } + + protected function provideOptions(): array + { + return [ + 'clear' => [ + 'prefix' => 'c', + 'longPrefix' => 'clear', + 'flag' => true, + 'description' => 'Clear cache before warm it up.', + 'defaultValue' => false, + ], + ]; + } + + protected function provideDescription(): string + { + return 'Warmup TAO Cache'; + } + + protected function run(): Report + { + $reports = []; + + $clearCache = (bool)$this->getOption('clear'); + if ($clearCache) { + $this->getServiceLocator()->get(SimpleCache::SERVICE_ID)->clear(); + $reports[] = Report::createInfo('Cache was cleared.'); + } + + try { + $cacheWarmupEvent = new CacheWarmupEvent(); + /** @var EventManager $eventManager */ + $eventManager = $this->getServiceLocator()->get(EventManager::SERVICE_ID); + $eventManager->trigger($cacheWarmupEvent); + + $reports = array_merge($reports, $cacheWarmupEvent->getReports()); + } catch (\Throwable $e) { + return Report::createError(sprintf('Cache warmup failed: %s', $e->getMessage())); + } + + return Report::createSuccess('TAO cache warmed up!', null, $reports); + } +} From f04819ab0d28785b2bc348d6b093f799e78619af Mon Sep 17 00:00:00 2001 From: Vlad Byndych Date: Tue, 27 Jun 2023 16:19:07 +0200 Subject: [PATCH 3/3] fix: When driver is nor defined class_exists throw an error about type mismatch. - fixed the issue, so woyld be always a string. - added more verbose exception message so it would be easier to fix configuration. --- common/persistence/PersistenceManager.php | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/common/persistence/PersistenceManager.php b/common/persistence/PersistenceManager.php index 07d5b4ba8..c72a63d9e 100644 --- a/common/persistence/PersistenceManager.php +++ b/common/persistence/PersistenceManager.php @@ -131,11 +131,15 @@ private function createPersistence($persistenceId) $config = $configs[$persistenceId]; $driverString = $config['driver']; - $driverClassName = isset(self::DRIVER_MAP[$driverString]) ? self::DRIVER_MAP[$driverString] : $driverString; + $driverClassName = self::DRIVER_MAP[$driverString] ?? (string)$driverString; if (!class_exists($driverClassName)) { throw new \common_exception_Error( - 'Driver ' . $driverString . ' not found, check your database configuration' + sprintf( + 'Driver "%s" not found, check your database configuration for %s.', + $driverString, + $persistenceId + ) ); }