From d03a4889a278b51badbea9d9ba0e9f02ee1654f9 Mon Sep 17 00:00:00 2001 From: "Bruno O. Notario" Date: Mon, 11 Mar 2024 00:21:25 -0300 Subject: [PATCH] New Log API, preferences by app directory --- system/MagiQL/Builder/AbstractBuilder.php | 4 + .../Databases/Api/DriverInterface.php | 1 - .../database/Databases/Api/LogInterface.php | 20 +++++ system/database/autoload.php | 36 +++++++++ system/engine/api/log.php | 78 +++++++++++++++++++ system/engine/autoload.php | 1 + system/engine/log.php | 18 +++-- system/engine/registry.php | 1 + system/etc/preferences.json | 4 +- system/system.php | 17 ++-- 10 files changed, 164 insertions(+), 16 deletions(-) create mode 100644 system/database/Databases/Api/LogInterface.php create mode 100644 system/engine/api/log.php diff --git a/system/MagiQL/Builder/AbstractBuilder.php b/system/MagiQL/Builder/AbstractBuilder.php index 0a37337..ffc5d55 100644 --- a/system/MagiQL/Builder/AbstractBuilder.php +++ b/system/MagiQL/Builder/AbstractBuilder.php @@ -14,6 +14,10 @@ use Phacil\Framework\Databases\Api\DriverInterface as DatabaseDriverInterface; use Phacil\Framework\MagiQL\Syntax\Column; use Phacil\Framework\MagiQL\Syntax\Table; +/** + * @method \Phacil\Framework\Api\Database getDb() + * @package Phacil\Framework\MagiQL\Builder + */ abstract class AbstractBuilder extends MagiQLBuilder { private $writerAlternative; diff --git a/system/database/Databases/Api/DriverInterface.php b/system/database/Databases/Api/DriverInterface.php index 30f2fe0..4a03438 100644 --- a/system/database/Databases/Api/DriverInterface.php +++ b/system/database/Databases/Api/DriverInterface.php @@ -1,5 +1,4 @@ create(\Phacil\Framework\MagiQL::class, [$this]); } + + if (Config::DB_LOG_ALL()) { + $this->logQuery($sql, ['cacheUse' => $cacheUse]); + } if(Config::SQL_CACHE() && $cacheUse == true) { return $this->Cache($sql); @@ -222,6 +226,9 @@ class Database implements DatabaseApi { */ public function execute($sql, array $params = []) { + if(Config::DB_LOG_ALL()) { + $this->logQuery($sql, $params); + } return $this->driver->execute($sql, $params); } @@ -238,4 +245,33 @@ class Database implements DatabaseApi { public function getDBTypeId() { return $this->driver->getDBTypeId(); } + + /** + * @param string $sql + * @param array $params + * @return void + * @throws \Phacil\Framework\Exception + */ + protected function logQuery($sql, $params = array()) { + if (Config::DB_LOG_ALL()) { + /** @var \Phacil\Framework\Databases\Api\LogInterface */ + $logger = \Phacil\Framework\Registry::getInstance(\Phacil\Framework\Databases\Api\LogInterface::class, [\Phacil\Framework\Databases\Api\LogInterface::LOG_FILE_NAME]); + + $debugging = (\Phacil\Framework\Config::DEBUG()) ?: false; + $errorFormat = \Phacil\Framework\Config::DEBUG_FORMAT() ?: 'txt'; + + $debugStamp = [ + 'sql' => $sql, + 'parameters' => $params, + //'file' => $this->getFile(), + 'trace' => ($debugging) ? \Phacil\Framework\Debug::backtrace(true, false) : null + ]; + + $logger->write(($errorFormat == 'json') ? json_encode($debugStamp) : implode(PHP_EOL, array_map( + [\Phacil\Framework\Exception::class, 'convertArray'], + $debugStamp, + array_keys($debugStamp) + ))); + } + } } diff --git a/system/engine/api/log.php b/system/engine/api/log.php new file mode 100644 index 0000000..93d2682 --- /dev/null +++ b/system/engine/api/log.php @@ -0,0 +1,78 @@ +filename = $filename; - $this->filepath = DIR_LOGS . $filename; - if(!is_dir(DIR_LOGS)){ + $this->filepath = constant(self::CONFIGURATION_LOGS_CONST) . $filename; + if(!is_dir(constant(self::CONFIGURATION_LOGS_CONST))){ $old = umask(0); - mkdir(DIR_LOGS, 0764, true); + mkdir(constant(self::CONFIGURATION_LOGS_CONST), self::DIR_LOGS_PERMISSIONS, true); umask($old); } - if(!is_writable(DIR_LOGS)){ - trigger_error('The '.DIR_LOGS.' folder must to be writeable!', E_USER_ERROR); + if(!is_writable(constant(self::CONFIGURATION_LOGS_CONST))){ + trigger_error('The '. constant(self::CONFIGURATION_LOGS_CONST).' folder must to be writeable!', E_USER_ERROR); } $this->fileobj = fopen($this->filepath, 'a+'); return; diff --git a/system/engine/registry.php b/system/engine/registry.php index 04f0c3d..3a30915 100644 --- a/system/engine/registry.php +++ b/system/engine/registry.php @@ -182,6 +182,7 @@ final class Registry { * @throws \Phacil\Framework\Exception If there is an error reading the JSON file or if the JSON data is invalid. */ static public function addPreference($jsonFilePath) { + $dataArray = []; if (file_exists($jsonFilePath)) { $jsonData = file_get_contents($jsonFilePath); diff --git a/system/etc/preferences.json b/system/etc/preferences.json index 23707fc..4c4e4dc 100644 --- a/system/etc/preferences.json +++ b/system/etc/preferences.json @@ -8,6 +8,8 @@ "Cm\\RedisSession\\Handler\\ConfigInterface": "Phacil\\Framework\\Session\\Redis\\Config", "Cm\\RedisSession\\Handler\\LoggerInterface": "Phacil\\Framework\\Session\\Redis\\Logger", "Phacil\\Framework\\Mail\\Api\\MailInterface": "Phacil\\Framework\\Mail", - "Phacil\\Framework\\Api\\Document": "Phacil\\Framework\\Document" + "Phacil\\Framework\\Api\\Document": "Phacil\\Framework\\Document", + "Phacil\\Framework\\Api\\Log": "Phacil\\Framework\\Log", + "Phacil\\Framework\\Databases\\Api\\LogInterface": "Phacil\\Framework\\Log" } } \ No newline at end of file diff --git a/system/system.php b/system/system.php index 8425f32..b92a5e1 100644 --- a/system/system.php +++ b/system/system.php @@ -104,6 +104,8 @@ final class startEngineExacTI { \Phacil\Framework\Registry::addPreference(\Phacil\Framework\Config::DIR_SYSTEM()."etc/preferences.json"); + \Phacil\Framework\Registry::addPreference(\Phacil\Framework\Config::DIR_APP_MODULAR()."etc/preferences.json"); + \Phacil\Framework\Registry::addPreferenceByRoute(self::getRoute()); if($this->composer) { @@ -373,6 +375,11 @@ final class startEngineExacTI { $this->registry->set($key, $this->registry->getInstance(\Phacil\Framework\Api\Document::class)); break; + case 'log': + /** @var \Phacil\Framework\Api\Log */ + $this->registry->set($key, $this->registry->create(\Phacil\Framework\Api\Log::class, [$this->config->get('config_error_filename')])); + break; + default: $objectToCreate = false; break; @@ -484,9 +491,9 @@ if(!$engine->config->get('config_error_filename')){ } /** - * @var Log + * @var \Phacil\Framework\Api\Log */ -$engine->log = new Log($engine->config->get('config_error_filename')); +//$engine->log = $engine->getRegistry()->create(\Phacil\Framework\Api\Log::class, [$engine->config->get('config_error_filename')]); // Error Handler set_error_handler(function ($errno, $errstr, $errfile, $errline) use (&$engine){ @@ -533,12 +540,6 @@ $engine->session = $engine->getRegistry()->create(\Phacil\Framework\Session::cla */ $engine->cache = new Caches(); -/** - * Request - * @var Request - */ -//$engine->request = new Request(); - // Response /** @var Response */ $engine->response = $engine->registry->getInstance(\Phacil\Framework\Response::class);