Skip to content

Commit

Permalink
Merge pull request #26 from roufy235/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
roufy235 authored Apr 13, 2021
2 parents 40a4822 + 0a297ce commit 06d9dc5
Show file tree
Hide file tree
Showing 13 changed files with 265 additions and 30 deletions.
1 change: 1 addition & 0 deletions .idea/php.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions .idea/slimWeb.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions assets/app/README.md
Original file line number Diff line number Diff line change
@@ -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.
11 changes: 7 additions & 4 deletions bin/Commands/ProductionCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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/*.log';
foreach (glob($path) as $file) {
if (file_exists($file)) {
unlink($file);
}
}

$output->writeln('<info>Completed!</info>');
return 0;
}
Expand Down
6 changes: 4 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"],
Expand All @@ -49,7 +50,8 @@
"./helpers/session.php"
],
"psr-4": {
"MultiveCLI\\": "bin/Commands"
"MultiveCLI\\": "bin/Commands",
"MultiveLogger\\": "controllers/factory"
}
},
"scripts": {
Expand Down
102 changes: 91 additions & 11 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

64 changes: 64 additions & 0 deletions controllers/factory/LoggerFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php /** @noinspection UnknownInspectionInspection */

namespace MultiveLogger;

use Monolog\Formatter\LineFormatter;
use Monolog\Handler\RotatingFileHandler;
use Monolog\Handler\StreamHandler;
use Monolog\Logger;
use Psr\Log\LoggerInterface;

final class LoggerFactory {
private string $path;
private int $level;
private array $handler = [];
private $testLogger;

public function __construct(array $settings) {
$this->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;
}

/** @noinspection PhpUnused */
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;
}
}
36 changes: 36 additions & 0 deletions controllers/factory/LoggerNewAccount.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php /** @noinspection UnknownInspectionInspection */


namespace MultiveLogger;


use Exception;
use MultiveLogger\models\UserModel;
use Psr\Log\LoggerInterface;

class LoggerNewAccount {

private LoggerInterface $logger;

public function __construct(LoggerFactory $logger) {
$this->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;
}
}

}
18 changes: 18 additions & 0 deletions controllers/factory/models/UserModel.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php


namespace MultiveLogger\models;


class UserModel {
public int $userId;
public function __construct() {
$this->userId = 0;
}
public function getUserId(): int {
return $this->userId;
}
public function setName(int $userId): void {
$this->userId = $userId;
}
}
2 changes: 1 addition & 1 deletion gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)',
}


Expand Down
Loading

0 comments on commit 06d9dc5

Please sign in to comment.