From 63ec9a01669fb33aa210c891ae879cce3b3fe0b5 Mon Sep 17 00:00:00 2001 From: roufy235 Date: Tue, 13 Apr 2021 08:42:56 +0100 Subject: [PATCH 1/2] Logger --- .idea/php.xml | 1 + .idea/slimWeb.iml | 2 + assets/app/README.md | 5 ++ bin/Commands/ProductionCommand.php | 11 ++- composer.json | 6 +- composer.lock | 102 +++++++++++++++++++++++--- controllers/factory/LoggerFactory.php | 63 ++++++++++++++++ gulpfile.js | 2 +- index.php | 22 +++++- logs/Multive.log | 1 + logs/README.md | 5 ++ logs/error-2021-04-13.log | 3 + router/route.php | 3 + 13 files changed, 206 insertions(+), 20 deletions(-) create mode 100644 assets/app/README.md create mode 100644 controllers/factory/LoggerFactory.php create mode 100644 logs/Multive.log create mode 100644 logs/README.md create mode 100755 logs/error-2021-04-13.log diff --git a/.idea/php.xml b/.idea/php.xml index 1a29106..3327417 100644 --- a/.idea/php.xml +++ b/.idea/php.xml @@ -73,6 +73,7 @@ + diff --git a/.idea/slimWeb.iml b/.idea/slimWeb.iml index f773ec6..f61b17f 100644 --- a/.idea/slimWeb.iml +++ b/.idea/slimWeb.iml @@ -5,6 +5,7 @@ + @@ -76,6 +77,7 @@ + diff --git a/assets/app/README.md b/assets/app/README.md new file mode 100644 index 0000000..edff8ae --- /dev/null +++ b/assets/app/README.md @@ -0,0 +1,5 @@ +# ASSETS/APP + +**This directory is required.** + +This directory contains your scss,js,images files that are going to be compiled by GulpJs. diff --git a/bin/Commands/ProductionCommand.php b/bin/Commands/ProductionCommand.php index 4f6ab75..7c99fe5 100644 --- a/bin/Commands/ProductionCommand.php +++ b/bin/Commands/ProductionCommand.php @@ -16,14 +16,17 @@ protected function configure (): void { } protected function execute (InputInterface $input, OutputInterface $output): int { - if (file_exists('cache/errors.log')) { - unlink('cache/errors.log'); - } - if (file_exists('cache/cache.php')) { unlink('cache/cache.php'); } + $path = 'logs/_*.json'; + foreach (glob($path) as $file) { + if (file_exists($file)) { + unlink($file); + } + } + $output->writeln('Completed!'); return 0; } diff --git a/composer.json b/composer.json index 58bc05b..2feb040 100644 --- a/composer.json +++ b/composer.json @@ -38,7 +38,8 @@ "slim/csrf": "^1.2", "catfan/medoo": "^1.7", "ext-readline": "*", - "symfony/console": "^5.2" + "symfony/console": "^5.2", + "symfony/polyfill-uuid": "^1.22" }, "autoload": { "classmap": ["controllers/", "tests/", "bin/Commands"], @@ -49,7 +50,8 @@ "./helpers/session.php" ], "psr-4": { - "MultiveCLI\\": "bin/Commands" + "MultiveCLI\\": "bin/Commands", + "MultiveLogger\\": "controllers/factory" } }, "scripts": { diff --git a/composer.lock b/composer.lock index 0bd7dad..cca1fde 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "59bc8171ab317b18cfa9bce46e5e6414", + "content-hash": "1db90977a93cb218a5a4f68befd2ca25", "packages": [ { "name": "catfan/medoo", @@ -488,16 +488,16 @@ }, { "name": "opis/closure", - "version": "3.6.1", + "version": "3.6.2", "source": { "type": "git", "url": "https://github.com/opis/closure.git", - "reference": "943b5d70cc5ae7483f6aff6ff43d7e34592ca0f5" + "reference": "06e2ebd25f2869e54a306dda991f7db58066f7f6" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/opis/closure/zipball/943b5d70cc5ae7483f6aff6ff43d7e34592ca0f5", - "reference": "943b5d70cc5ae7483f6aff6ff43d7e34592ca0f5", + "url": "https://api.github.com/repos/opis/closure/zipball/06e2ebd25f2869e54a306dda991f7db58066f7f6", + "reference": "06e2ebd25f2869e54a306dda991f7db58066f7f6", "shasum": "" }, "require": { @@ -547,9 +547,9 @@ ], "support": { "issues": "https://github.com/opis/closure/issues", - "source": "https://github.com/opis/closure/tree/3.6.1" + "source": "https://github.com/opis/closure/tree/3.6.2" }, - "time": "2020-11-07T02:01:34+00:00" + "time": "2021-04-09T13:42:10+00:00" }, { "name": "php-di/invoker", @@ -2344,6 +2344,85 @@ ], "time": "2021-01-07T16:49:33+00:00" }, + { + "name": "symfony/polyfill-uuid", + "version": "v1.22.1", + "source": { + "type": "git", + "url": "https://github.com/symfony/polyfill-uuid.git", + "reference": "9773608c15d3fe6ba2b6456a124777a7b8ffee2a" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/symfony/polyfill-uuid/zipball/9773608c15d3fe6ba2b6456a124777a7b8ffee2a", + "reference": "9773608c15d3fe6ba2b6456a124777a7b8ffee2a", + "shasum": "" + }, + "require": { + "php": ">=7.1" + }, + "suggest": { + "ext-uuid": "For best performance" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-main": "1.22-dev" + }, + "thanks": { + "name": "symfony/polyfill", + "url": "https://github.com/symfony/polyfill" + } + }, + "autoload": { + "psr-4": { + "Symfony\\Polyfill\\Uuid\\": "" + }, + "files": [ + "bootstrap.php" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Grégoire Pineau", + "email": "lyrixx@lyrixx.info" + }, + { + "name": "Symfony Community", + "homepage": "https://symfony.com/contributors" + } + ], + "description": "Symfony polyfill for uuid functions", + "homepage": "https://symfony.com", + "keywords": [ + "compatibility", + "polyfill", + "portable", + "uuid" + ], + "support": { + "source": "https://github.com/symfony/polyfill-uuid/tree/v1.22.1" + }, + "funding": [ + { + "url": "https://symfony.com/sponsor", + "type": "custom" + }, + { + "url": "https://github.com/fabpot", + "type": "github" + }, + { + "url": "https://tidelift.com/funding/github/packagist/symfony/symfony", + "type": "tidelift" + } + ], + "time": "2021-01-22T09:19:47+00:00" + }, { "name": "symfony/service-contracts", "version": "v2.2.0", @@ -3534,12 +3613,12 @@ "source": { "type": "git", "url": "https://github.com/Roave/SecurityAdvisories.git", - "reference": "07d2f0c0e6553fd7433f2eb7d043260d3bfd351d" + "reference": "aa48fe959b0236eede9c51a38f47df2bb81ef137" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/Roave/SecurityAdvisories/zipball/07d2f0c0e6553fd7433f2eb7d043260d3bfd351d", - "reference": "07d2f0c0e6553fd7433f2eb7d043260d3bfd351d", + "url": "https://api.github.com/repos/Roave/SecurityAdvisories/zipball/aa48fe959b0236eede9c51a38f47df2bb81ef137", + "reference": "aa48fe959b0236eede9c51a38f47df2bb81ef137", "shasum": "" }, "conflict": { @@ -3796,6 +3875,7 @@ "verot/class.upload.php": "<=1.0.3|>=2,<=2.0.4", "vrana/adminer": "<4.7.9", "wallabag/tcpdf": "<6.2.22", + "wikimedia/parsoid": "<0.12.2", "willdurand/js-translation-bundle": "<2.1.1", "yii2mod/yii2-cms": "<1.9.2", "yiisoft/yii": ">=1.1.14,<1.1.15", @@ -3865,7 +3945,7 @@ "type": "tidelift" } ], - "time": "2021-04-07T21:01:39+00:00" + "time": "2021-04-09T08:01:23+00:00" }, { "name": "sebastian/cli-parser", diff --git a/controllers/factory/LoggerFactory.php b/controllers/factory/LoggerFactory.php new file mode 100644 index 0000000..efb5121 --- /dev/null +++ b/controllers/factory/LoggerFactory.php @@ -0,0 +1,63 @@ +path = (string)$settings['path']; + $this->level = (int)$settings['level']; + + // This can be used for testing to make the Factory testable + if (isset($settings['test'])) { + $this->testLogger = $settings['test']; + } + } + + public function createLogger(string $name = null): LoggerInterface { + if ($this->testLogger) { + return $this->testLogger; + } + + $logger = new Logger($name ?: uuid_create()); + + foreach ($this->handler as $handler) { + $logger->pushHandler($handler); + } + + $this->handler = []; + + return $logger; + } + + public function addFileHandler(string $filename, int $level = null): self { + $filename = sprintf('%s/%s', $this->path, $filename); + $rotatingFileHandler = new RotatingFileHandler($filename, 0, $level ?? $this->level, true, 0777); + + // The last "true" here tells monolog to remove empty []'s + $rotatingFileHandler->setFormatter(new LineFormatter(null, null, false, true)); + + $this->handler[] = $rotatingFileHandler; + + return $this; + } + + public function addConsoleHandler(int $level = null): self { + $streamHandler = new StreamHandler('php://stdout', $level ?? $this->level); + $streamHandler->setFormatter(new LineFormatter(null, null, false, true)); + + $this->handler[] = $streamHandler; + + return $this; + } +} diff --git a/gulpfile.js b/gulpfile.js index ad0cc2c..c57069d 100644 --- a/gulpfile.js +++ b/gulpfile.js @@ -19,7 +19,7 @@ const files = { scssPath: 'assets/app/scss/**/*.scss', jsPath: 'assets/app/js/**/*.js', distPath: 'dist', - imagePath: 'assets/images/**/*.+(png|jpg|gif|svg|jpeg)', + imagePath: 'assets/app/images/**/*.+(png|jpg|gif|svg|jpeg)', } diff --git a/index.php b/index.php index 1db5e1b..f185cad 100644 --- a/index.php +++ b/index.php @@ -1,6 +1,8 @@ build(); $container->set('upload_directory', __DIR__ . '/uploads'. DIRECTORY_SEPARATOR); // e.g $path = $this->get('upload_directory'); + +const LOGGER_SETTINGS = [ + 'name' => 'app', + 'path' => __DIR__ . DIRECTORY_SEPARATOR. 'logs', + 'filename' => 'app.log', + 'level' => Logger::DEBUG, + 'file_permission' => 0775, +]; + +$container->set('MultiveLoggerFactory', function() : LoggerFactory { + return new LoggerFactory(LOGGER_SETTINGS); +}); +// e.g $this->get('MultiveLoggerFactory')->error('Bar', ['hello', __FILE__, __LINE__]); + + $container->set('logger', function() : Logger { $name = $_ENV['PROJECT_NAME']; $logger = new Logger($name); - $file_handler = new StreamHandler(__DIR__ . '/cache/'. $name .'.log', Logger::WARNING); + $file_handler = new StreamHandler(__DIR__ . '/logs/'. $name .'.log', Logger::WARNING); $logger->pushHandler($file_handler); return $logger; }); @@ -84,7 +101,8 @@ $app->addRoutingMiddleware(); $app->setBasePath(getBasePath()); if (in_array($_SERVER['REMOTE_ADDR'], REMOTE_ADDR)) { - $app->addErrorMiddleware(true, true, true); + $logger = $app->getContainer()->get('MultiveLoggerFactory')->addFileHandler('error.log')->createLogger(); + $app->addErrorMiddleware(true, true, true, $logger); error_reporting(E_ALL); // Error/Exception engine, always use E_ALL ini_set('ignore_repeated_errors', TRUE); // always use TRUE diff --git a/logs/Multive.log b/logs/Multive.log new file mode 100644 index 0000000..23c77ae --- /dev/null +++ b/logs/Multive.log @@ -0,0 +1 @@ +[2021-04-13T08:04:11.758083+01:00] Multive.ERROR: Bar ["hello","/Applications/XAMPP/xamppfiles/htdocs/WorkStation/Multive/router/route.php",14] [] diff --git a/logs/README.md b/logs/README.md new file mode 100644 index 0000000..a16c717 --- /dev/null +++ b/logs/README.md @@ -0,0 +1,5 @@ +# LOGS + +**This directory is required.** + +Your Multive Framework application's log files will be written to this directory. diff --git a/logs/error-2021-04-13.log b/logs/error-2021-04-13.log new file mode 100755 index 0000000..7646084 --- /dev/null +++ b/logs/error-2021-04-13.log @@ -0,0 +1,3 @@ +[2021-04-13T08:36:01.346245+01:00] 1875f98d-4048-43c3-b974-30d7dc8787b3.ERROR: 404 Not Found Type: Slim\Exception\HttpNotFoundException Code: 404 Message: Not found. File: /Applications/XAMPP/xamppfiles/htdocs/WorkStation/Multive/vendor/slim/slim/Slim/Middleware/RoutingMiddleware.php Line: 91 Trace: #0 /Applications/XAMPP/xamppfiles/htdocs/WorkStation/Multive/vendor/slim/slim/Slim/Middleware/RoutingMiddleware.php(58): Slim\Middleware\RoutingMiddleware->performRouting(Object(Slim\Psr7\Request)) #1 /Applications/XAMPP/xamppfiles/htdocs/WorkStation/Multive/vendor/slim/slim/Slim/MiddlewareDispatcher.php(147): Slim\Middleware\RoutingMiddleware->process(Object(Slim\Psr7\Request), Object(Psr\Http\Server\RequestHandlerInterface@anonymous)) #2 /Applications/XAMPP/xamppfiles/htdocs/WorkStation/Multive/vendor/slim/slim/Slim/Middleware/ErrorMiddleware.php(107): Psr\Http\Server\RequestHandlerInterface@anonymous->handle(Object(Slim\Psr7\Request)) #3 /Applications/XAMPP/xamppfiles/htdocs/WorkStation/Multive/vendor/slim/slim/Slim/MiddlewareDispatcher.php(147): Slim\Middleware\ErrorMiddleware->process(Object(Slim\Psr7\Request), Object(Psr\Http\Server\RequestHandlerInterface@anonymous)) #4 /Applications/XAMPP/xamppfiles/htdocs/WorkStation/Multive/index.php(129): Psr\Http\Server\RequestHandlerInterface@anonymous->handle(Object(Slim\Psr7\Request)) #5 /Applications/XAMPP/xamppfiles/htdocs/WorkStation/Multive/vendor/slim/slim/Slim/MiddlewareDispatcher.php(313): Closure->{closure}(Object(Slim\Psr7\Request), Object(Psr\Http\Server\RequestHandlerInterface@anonymous)) #6 /Applications/XAMPP/xamppfiles/htdocs/WorkStation/Multive/vendor/slim/slim/Slim/MiddlewareDispatcher.php(81): Psr\Http\Server\RequestHandlerInterface@anonymous->handle(Object(Slim\Psr7\Request)) #7 /Applications/XAMPP/xamppfiles/htdocs/WorkStation/Multive/vendor/slim/slim/Slim/App.php(215): Slim\MiddlewareDispatcher->handle(Object(Slim\Psr7\Request)) #8 /Applications/XAMPP/xamppfiles/htdocs/WorkStation/Multive/vendor/slim/slim/Slim/App.php(199): Slim\App->handle(Object(Slim\Psr7\Request)) #9 /Applications/XAMPP/xamppfiles/htdocs/WorkStation/Multive/index.php(145): Slim\App->run() #10 {main} +[2021-04-13T08:36:36.941523+01:00] ba16de00-e805-4538-a530-6f6809a3693a.ERROR: 404 Not Found Type: Slim\Exception\HttpNotFoundException Code: 404 Message: Not found. File: /Applications/XAMPP/xamppfiles/htdocs/WorkStation/Multive/vendor/slim/slim/Slim/Middleware/RoutingMiddleware.php Line: 91 Trace: #0 /Applications/XAMPP/xamppfiles/htdocs/WorkStation/Multive/vendor/slim/slim/Slim/Middleware/RoutingMiddleware.php(58): Slim\Middleware\RoutingMiddleware->performRouting(Object(Slim\Psr7\Request)) #1 /Applications/XAMPP/xamppfiles/htdocs/WorkStation/Multive/vendor/slim/slim/Slim/MiddlewareDispatcher.php(147): Slim\Middleware\RoutingMiddleware->process(Object(Slim\Psr7\Request), Object(Psr\Http\Server\RequestHandlerInterface@anonymous)) #2 /Applications/XAMPP/xamppfiles/htdocs/WorkStation/Multive/vendor/slim/slim/Slim/Middleware/ErrorMiddleware.php(107): Psr\Http\Server\RequestHandlerInterface@anonymous->handle(Object(Slim\Psr7\Request)) #3 /Applications/XAMPP/xamppfiles/htdocs/WorkStation/Multive/vendor/slim/slim/Slim/MiddlewareDispatcher.php(147): Slim\Middleware\ErrorMiddleware->process(Object(Slim\Psr7\Request), Object(Psr\Http\Server\RequestHandlerInterface@anonymous)) #4 /Applications/XAMPP/xamppfiles/htdocs/WorkStation/Multive/index.php(129): Psr\Http\Server\RequestHandlerInterface@anonymous->handle(Object(Slim\Psr7\Request)) #5 /Applications/XAMPP/xamppfiles/htdocs/WorkStation/Multive/vendor/slim/slim/Slim/MiddlewareDispatcher.php(313): Closure->{closure}(Object(Slim\Psr7\Request), Object(Psr\Http\Server\RequestHandlerInterface@anonymous)) #6 /Applications/XAMPP/xamppfiles/htdocs/WorkStation/Multive/vendor/slim/slim/Slim/MiddlewareDispatcher.php(81): Psr\Http\Server\RequestHandlerInterface@anonymous->handle(Object(Slim\Psr7\Request)) #7 /Applications/XAMPP/xamppfiles/htdocs/WorkStation/Multive/vendor/slim/slim/Slim/App.php(215): Slim\MiddlewareDispatcher->handle(Object(Slim\Psr7\Request)) #8 /Applications/XAMPP/xamppfiles/htdocs/WorkStation/Multive/vendor/slim/slim/Slim/App.php(199): Slim\App->handle(Object(Slim\Psr7\Request)) #9 /Applications/XAMPP/xamppfiles/htdocs/WorkStation/Multive/index.php(145): Slim\App->run() #10 {main} +[2021-04-13T08:36:53.185531+01:00] b598ded6-8d23-4f44-9bdd-5ec3c5c66bb5.ERROR: 404 Not Found Type: Slim\Exception\HttpNotFoundException Code: 404 Message: Not found. File: /Applications/XAMPP/xamppfiles/htdocs/WorkStation/Multive/vendor/slim/slim/Slim/Middleware/RoutingMiddleware.php Line: 91 Trace: #0 /Applications/XAMPP/xamppfiles/htdocs/WorkStation/Multive/vendor/slim/slim/Slim/Middleware/RoutingMiddleware.php(58): Slim\Middleware\RoutingMiddleware->performRouting(Object(Slim\Psr7\Request)) #1 /Applications/XAMPP/xamppfiles/htdocs/WorkStation/Multive/vendor/slim/slim/Slim/MiddlewareDispatcher.php(147): Slim\Middleware\RoutingMiddleware->process(Object(Slim\Psr7\Request), Object(Psr\Http\Server\RequestHandlerInterface@anonymous)) #2 /Applications/XAMPP/xamppfiles/htdocs/WorkStation/Multive/vendor/slim/slim/Slim/Middleware/ErrorMiddleware.php(107): Psr\Http\Server\RequestHandlerInterface@anonymous->handle(Object(Slim\Psr7\Request)) #3 /Applications/XAMPP/xamppfiles/htdocs/WorkStation/Multive/vendor/slim/slim/Slim/MiddlewareDispatcher.php(147): Slim\Middleware\ErrorMiddleware->process(Object(Slim\Psr7\Request), Object(Psr\Http\Server\RequestHandlerInterface@anonymous)) #4 /Applications/XAMPP/xamppfiles/htdocs/WorkStation/Multive/index.php(129): Psr\Http\Server\RequestHandlerInterface@anonymous->handle(Object(Slim\Psr7\Request)) #5 /Applications/XAMPP/xamppfiles/htdocs/WorkStation/Multive/vendor/slim/slim/Slim/MiddlewareDispatcher.php(313): Closure->{closure}(Object(Slim\Psr7\Request), Object(Psr\Http\Server\RequestHandlerInterface@anonymous)) #6 /Applications/XAMPP/xamppfiles/htdocs/WorkStation/Multive/vendor/slim/slim/Slim/MiddlewareDispatcher.php(81): Psr\Http\Server\RequestHandlerInterface@anonymous->handle(Object(Slim\Psr7\Request)) #7 /Applications/XAMPP/xamppfiles/htdocs/WorkStation/Multive/vendor/slim/slim/Slim/App.php(215): Slim\MiddlewareDispatcher->handle(Object(Slim\Psr7\Request)) #8 /Applications/XAMPP/xamppfiles/htdocs/WorkStation/Multive/vendor/slim/slim/Slim/App.php(199): Slim\App->handle(Object(Slim\Psr7\Request)) #9 /Applications/XAMPP/xamppfiles/htdocs/WorkStation/Multive/index.php(145): Slim\App->run() #10 {main} diff --git a/router/route.php b/router/route.php index 65288b1..fc8e3c3 100644 --- a/router/route.php +++ b/router/route.php @@ -10,6 +10,9 @@ return function (App $app) { $app->get('/', function ($request, $response, $args) { + + + $renderer = new PhpRenderer(__DIR__ . '/../views/'); return $renderer->render($response, "index.php", [ From 0a297cebe9581eba9370432cbeeec02d89d0c781 Mon Sep 17 00:00:00 2001 From: roufy235 Date: Tue, 13 Apr 2021 09:18:17 +0100 Subject: [PATCH 2/2] Logger factory implemented --- bin/Commands/ProductionCommand.php | 2 +- controllers/factory/LoggerFactory.php | 3 +- controllers/factory/LoggerNewAccount.php | 36 ++++++++++++++++++++++++ controllers/factory/models/UserModel.php | 18 ++++++++++++ index.php | 31 +++++++++----------- logs/Multive.log | 1 - logs/error-2021-04-13.log | 3 -- router/route.php | 5 +++- 8 files changed, 74 insertions(+), 25 deletions(-) create mode 100644 controllers/factory/LoggerNewAccount.php create mode 100644 controllers/factory/models/UserModel.php delete mode 100644 logs/Multive.log delete mode 100755 logs/error-2021-04-13.log diff --git a/bin/Commands/ProductionCommand.php b/bin/Commands/ProductionCommand.php index 7c99fe5..39bfbf8 100644 --- a/bin/Commands/ProductionCommand.php +++ b/bin/Commands/ProductionCommand.php @@ -20,7 +20,7 @@ protected function execute (InputInterface $input, OutputInterface $output): int unlink('cache/cache.php'); } - $path = 'logs/_*.json'; + $path = 'logs/*.log'; foreach (glob($path) as $file) { if (file_exists($file)) { unlink($file); diff --git a/controllers/factory/LoggerFactory.php b/controllers/factory/LoggerFactory.php index efb5121..84184b7 100644 --- a/controllers/factory/LoggerFactory.php +++ b/controllers/factory/LoggerFactory.php @@ -1,4 +1,4 @@ -level); $streamHandler->setFormatter(new LineFormatter(null, null, false, true)); diff --git a/controllers/factory/LoggerNewAccount.php b/controllers/factory/LoggerNewAccount.php new file mode 100644 index 0000000..6ba86cf --- /dev/null +++ b/controllers/factory/LoggerNewAccount.php @@ -0,0 +1,36 @@ +logger = $logger + ->addFileHandler('user_creator.log') + ->createLogger(); + } + + /** + * @throws Exception + * @noinspection PhpUnused + */ + public function registerUser(UserModel $user): void { + try { + // Log success + $this->logger->info(sprintf('User created: %s', $user->getUserId())); + } catch (Exception $exception) { + // Log error message + $this->logger->error($exception->getMessage()); + throw $exception; + } + } + +} diff --git a/controllers/factory/models/UserModel.php b/controllers/factory/models/UserModel.php new file mode 100644 index 0000000..51e4268 --- /dev/null +++ b/controllers/factory/models/UserModel.php @@ -0,0 +1,18 @@ +userId = 0; + } + public function getUserId(): int { + return $this->userId; + } + public function setName(int $userId): void { + $this->userId = $userId; + } +} diff --git a/index.php b/index.php index f185cad..b96e1a1 100644 --- a/index.php +++ b/index.php @@ -4,9 +4,8 @@ use MultiveLogger\LoggerFactory; use DI\ContainerBuilder; -use Medoo\Medoo; -use Monolog\Handler\StreamHandler; use Monolog\Logger; +use MultiveLogger\LoggerNewAccount; use Psr\Http\Message\ServerRequestInterface; use Psr\Http\Server\RequestHandlerInterface; use Slim\Exception\HttpNotFoundException; @@ -53,21 +52,18 @@ 'level' => Logger::DEBUG, 'file_permission' => 0775, ]; - -$container->set('MultiveLoggerFactory', function() : LoggerFactory { +$container->set('MultiveErrorLoggerFactory', function() : LoggerFactory { return new LoggerFactory(LOGGER_SETTINGS); }); -// e.g $this->get('MultiveLoggerFactory')->error('Bar', ['hello', __FILE__, __LINE__]); - - -$container->set('logger', function() : Logger { - $name = $_ENV['PROJECT_NAME']; - $logger = new Logger($name); - $file_handler = new StreamHandler(__DIR__ . '/logs/'. $name .'.log', Logger::WARNING); - $logger->pushHandler($file_handler); - return $logger; +$container->set('LoggerNewAccount', function() : LoggerNewAccount { + return new LoggerNewAccount(new LoggerFactory(LOGGER_SETTINGS)); }); -// e.g $this->get('logger')->error('Bar', ['hello', __FILE__, __LINE__]); +/* + * USAGE + * $newUser = new \MultiveLogger\models\UserModel(); + * $newUser->setName(234); + * $this->get('LoggerNewAccount')->registerUser($newUser); + */ $container->set('databaseConnection', function() : array { return [ @@ -95,14 +91,13 @@ $response = $response->withHeader('Access-Control-Allow-Methods', implode(',', $methods)); $response = $response->withHeader('Access-Control-Allow-Headers', $requestHeaders); // Optional: Allow Ajax CORS requests with Authorization header - $response = $response->withHeader('Access-Control-Allow-Credentials', 'true'); - return $response; + return $response->withHeader('Access-Control-Allow-Credentials', 'true'); }); $app->addRoutingMiddleware(); $app->setBasePath(getBasePath()); if (in_array($_SERVER['REMOTE_ADDR'], REMOTE_ADDR)) { - $logger = $app->getContainer()->get('MultiveLoggerFactory')->addFileHandler('error.log')->createLogger(); - $app->addErrorMiddleware(true, true, true, $logger); + $MultiveErrorLoggerFactory = $app->getContainer()->get('MultiveErrorLoggerFactory')->addFileHandler('error.log')->createLogger(); + $app->addErrorMiddleware(true, true, true, $MultiveErrorLoggerFactory); error_reporting(E_ALL); // Error/Exception engine, always use E_ALL ini_set('ignore_repeated_errors', TRUE); // always use TRUE diff --git a/logs/Multive.log b/logs/Multive.log deleted file mode 100644 index 23c77ae..0000000 --- a/logs/Multive.log +++ /dev/null @@ -1 +0,0 @@ -[2021-04-13T08:04:11.758083+01:00] Multive.ERROR: Bar ["hello","/Applications/XAMPP/xamppfiles/htdocs/WorkStation/Multive/router/route.php",14] [] diff --git a/logs/error-2021-04-13.log b/logs/error-2021-04-13.log deleted file mode 100755 index 7646084..0000000 --- a/logs/error-2021-04-13.log +++ /dev/null @@ -1,3 +0,0 @@ -[2021-04-13T08:36:01.346245+01:00] 1875f98d-4048-43c3-b974-30d7dc8787b3.ERROR: 404 Not Found Type: Slim\Exception\HttpNotFoundException Code: 404 Message: Not found. File: /Applications/XAMPP/xamppfiles/htdocs/WorkStation/Multive/vendor/slim/slim/Slim/Middleware/RoutingMiddleware.php Line: 91 Trace: #0 /Applications/XAMPP/xamppfiles/htdocs/WorkStation/Multive/vendor/slim/slim/Slim/Middleware/RoutingMiddleware.php(58): Slim\Middleware\RoutingMiddleware->performRouting(Object(Slim\Psr7\Request)) #1 /Applications/XAMPP/xamppfiles/htdocs/WorkStation/Multive/vendor/slim/slim/Slim/MiddlewareDispatcher.php(147): Slim\Middleware\RoutingMiddleware->process(Object(Slim\Psr7\Request), Object(Psr\Http\Server\RequestHandlerInterface@anonymous)) #2 /Applications/XAMPP/xamppfiles/htdocs/WorkStation/Multive/vendor/slim/slim/Slim/Middleware/ErrorMiddleware.php(107): Psr\Http\Server\RequestHandlerInterface@anonymous->handle(Object(Slim\Psr7\Request)) #3 /Applications/XAMPP/xamppfiles/htdocs/WorkStation/Multive/vendor/slim/slim/Slim/MiddlewareDispatcher.php(147): Slim\Middleware\ErrorMiddleware->process(Object(Slim\Psr7\Request), Object(Psr\Http\Server\RequestHandlerInterface@anonymous)) #4 /Applications/XAMPP/xamppfiles/htdocs/WorkStation/Multive/index.php(129): Psr\Http\Server\RequestHandlerInterface@anonymous->handle(Object(Slim\Psr7\Request)) #5 /Applications/XAMPP/xamppfiles/htdocs/WorkStation/Multive/vendor/slim/slim/Slim/MiddlewareDispatcher.php(313): Closure->{closure}(Object(Slim\Psr7\Request), Object(Psr\Http\Server\RequestHandlerInterface@anonymous)) #6 /Applications/XAMPP/xamppfiles/htdocs/WorkStation/Multive/vendor/slim/slim/Slim/MiddlewareDispatcher.php(81): Psr\Http\Server\RequestHandlerInterface@anonymous->handle(Object(Slim\Psr7\Request)) #7 /Applications/XAMPP/xamppfiles/htdocs/WorkStation/Multive/vendor/slim/slim/Slim/App.php(215): Slim\MiddlewareDispatcher->handle(Object(Slim\Psr7\Request)) #8 /Applications/XAMPP/xamppfiles/htdocs/WorkStation/Multive/vendor/slim/slim/Slim/App.php(199): Slim\App->handle(Object(Slim\Psr7\Request)) #9 /Applications/XAMPP/xamppfiles/htdocs/WorkStation/Multive/index.php(145): Slim\App->run() #10 {main} -[2021-04-13T08:36:36.941523+01:00] ba16de00-e805-4538-a530-6f6809a3693a.ERROR: 404 Not Found Type: Slim\Exception\HttpNotFoundException Code: 404 Message: Not found. File: /Applications/XAMPP/xamppfiles/htdocs/WorkStation/Multive/vendor/slim/slim/Slim/Middleware/RoutingMiddleware.php Line: 91 Trace: #0 /Applications/XAMPP/xamppfiles/htdocs/WorkStation/Multive/vendor/slim/slim/Slim/Middleware/RoutingMiddleware.php(58): Slim\Middleware\RoutingMiddleware->performRouting(Object(Slim\Psr7\Request)) #1 /Applications/XAMPP/xamppfiles/htdocs/WorkStation/Multive/vendor/slim/slim/Slim/MiddlewareDispatcher.php(147): Slim\Middleware\RoutingMiddleware->process(Object(Slim\Psr7\Request), Object(Psr\Http\Server\RequestHandlerInterface@anonymous)) #2 /Applications/XAMPP/xamppfiles/htdocs/WorkStation/Multive/vendor/slim/slim/Slim/Middleware/ErrorMiddleware.php(107): Psr\Http\Server\RequestHandlerInterface@anonymous->handle(Object(Slim\Psr7\Request)) #3 /Applications/XAMPP/xamppfiles/htdocs/WorkStation/Multive/vendor/slim/slim/Slim/MiddlewareDispatcher.php(147): Slim\Middleware\ErrorMiddleware->process(Object(Slim\Psr7\Request), Object(Psr\Http\Server\RequestHandlerInterface@anonymous)) #4 /Applications/XAMPP/xamppfiles/htdocs/WorkStation/Multive/index.php(129): Psr\Http\Server\RequestHandlerInterface@anonymous->handle(Object(Slim\Psr7\Request)) #5 /Applications/XAMPP/xamppfiles/htdocs/WorkStation/Multive/vendor/slim/slim/Slim/MiddlewareDispatcher.php(313): Closure->{closure}(Object(Slim\Psr7\Request), Object(Psr\Http\Server\RequestHandlerInterface@anonymous)) #6 /Applications/XAMPP/xamppfiles/htdocs/WorkStation/Multive/vendor/slim/slim/Slim/MiddlewareDispatcher.php(81): Psr\Http\Server\RequestHandlerInterface@anonymous->handle(Object(Slim\Psr7\Request)) #7 /Applications/XAMPP/xamppfiles/htdocs/WorkStation/Multive/vendor/slim/slim/Slim/App.php(215): Slim\MiddlewareDispatcher->handle(Object(Slim\Psr7\Request)) #8 /Applications/XAMPP/xamppfiles/htdocs/WorkStation/Multive/vendor/slim/slim/Slim/App.php(199): Slim\App->handle(Object(Slim\Psr7\Request)) #9 /Applications/XAMPP/xamppfiles/htdocs/WorkStation/Multive/index.php(145): Slim\App->run() #10 {main} -[2021-04-13T08:36:53.185531+01:00] b598ded6-8d23-4f44-9bdd-5ec3c5c66bb5.ERROR: 404 Not Found Type: Slim\Exception\HttpNotFoundException Code: 404 Message: Not found. File: /Applications/XAMPP/xamppfiles/htdocs/WorkStation/Multive/vendor/slim/slim/Slim/Middleware/RoutingMiddleware.php Line: 91 Trace: #0 /Applications/XAMPP/xamppfiles/htdocs/WorkStation/Multive/vendor/slim/slim/Slim/Middleware/RoutingMiddleware.php(58): Slim\Middleware\RoutingMiddleware->performRouting(Object(Slim\Psr7\Request)) #1 /Applications/XAMPP/xamppfiles/htdocs/WorkStation/Multive/vendor/slim/slim/Slim/MiddlewareDispatcher.php(147): Slim\Middleware\RoutingMiddleware->process(Object(Slim\Psr7\Request), Object(Psr\Http\Server\RequestHandlerInterface@anonymous)) #2 /Applications/XAMPP/xamppfiles/htdocs/WorkStation/Multive/vendor/slim/slim/Slim/Middleware/ErrorMiddleware.php(107): Psr\Http\Server\RequestHandlerInterface@anonymous->handle(Object(Slim\Psr7\Request)) #3 /Applications/XAMPP/xamppfiles/htdocs/WorkStation/Multive/vendor/slim/slim/Slim/MiddlewareDispatcher.php(147): Slim\Middleware\ErrorMiddleware->process(Object(Slim\Psr7\Request), Object(Psr\Http\Server\RequestHandlerInterface@anonymous)) #4 /Applications/XAMPP/xamppfiles/htdocs/WorkStation/Multive/index.php(129): Psr\Http\Server\RequestHandlerInterface@anonymous->handle(Object(Slim\Psr7\Request)) #5 /Applications/XAMPP/xamppfiles/htdocs/WorkStation/Multive/vendor/slim/slim/Slim/MiddlewareDispatcher.php(313): Closure->{closure}(Object(Slim\Psr7\Request), Object(Psr\Http\Server\RequestHandlerInterface@anonymous)) #6 /Applications/XAMPP/xamppfiles/htdocs/WorkStation/Multive/vendor/slim/slim/Slim/MiddlewareDispatcher.php(81): Psr\Http\Server\RequestHandlerInterface@anonymous->handle(Object(Slim\Psr7\Request)) #7 /Applications/XAMPP/xamppfiles/htdocs/WorkStation/Multive/vendor/slim/slim/Slim/App.php(215): Slim\MiddlewareDispatcher->handle(Object(Slim\Psr7\Request)) #8 /Applications/XAMPP/xamppfiles/htdocs/WorkStation/Multive/vendor/slim/slim/Slim/App.php(199): Slim\App->handle(Object(Slim\Psr7\Request)) #9 /Applications/XAMPP/xamppfiles/htdocs/WorkStation/Multive/index.php(145): Slim\App->run() #10 {main} diff --git a/router/route.php b/router/route.php index fc8e3c3..1cb3ad6 100644 --- a/router/route.php +++ b/router/route.php @@ -3,6 +3,7 @@ /** @noinspection StaticClosureCanBeUsedInspection */ declare(strict_types=1); +use MultiveLogger\models\UserModel; use ReallySimpleJWT\Token; use Slim\App; use Slim\Views\PhpRenderer; @@ -11,7 +12,9 @@ $app->get('/', function ($request, $response, $args) { - + $newUser = new UserModel(); + $newUser->setName(234); + //$this->get('LoggerNewAccount')->registerUser($newUser); $renderer = new PhpRenderer(__DIR__ . '/../views/'); return $renderer->render($response, "index.php", [