Skip to content

Commit

Permalink
#6 change library catalog structure to 3 folders: core, components, d…
Browse files Browse the repository at this point in the history
…atabase
  • Loading branch information
xandros15 committed May 23, 2016
1 parent ba497a5 commit a876409
Show file tree
Hide file tree
Showing 29 changed files with 1,639 additions and 1,639 deletions.
310 changes: 155 additions & 155 deletions library/debugger/Core.php → library/components/debugger/Core.php
Original file line number Diff line number Diff line change
@@ -1,156 +1,156 @@
<?php

namespace library\Debugger;

use DateTime;
use DateTimeZone;
use Exception;
use library\debugger\Logger;

class Core
{
const
ERROR = 1,
WARNING = 2,
INFO = 3,
SUCCESS = 4;

/** @var string */
protected $timezoneName = 'UTC';

/** @var Core */
protected static $logger;

/** @var DateTime */
private $datetime;

/** @var string */
private $filename;

/** @var string */
private $dirname;

/** @var string */
private $ext;

private function __construct()
{

}

private function __clone()
{

}

public static function add($message, $type = self::INFO)
{
if (!self::$logger) {
throw new Exception('No configured logger');
}
}

public static function setLogger($filename, $dirname, $timezone = 'UTC')
{
if (isset(self::$logger)) {
return self::$logger;
}
$core = new Core();

$core->setDatetime($timezone)
->setDirname($dirname)
->setFilename($filename);
return self::$logger = $core;
}

protected function save($message)
{
return (file_put_contents($this->getPath(), $message, FILE_APPEND));
}

protected function getPath()
{
return $this->dirname . DIRECTORY_SEPARATOR . $this->filename . '.' . $this->ext;
}

protected function getFilename()
{
return $this->filename;
}

protected function getDirname()
{
return $this->dirname;
}

protected function getExt()
{
return $this->ext;
}

protected function flatten(array $array, $prefix)
{
$string = '';
foreach ($array as $row) {
if (is_array($row)) {
$row = $this->flatten($row);
}
if (is_string($row)) {
$string .= $prefix . trim($row) . PHP_EOL;
}
}
return ($string) ? $string : false;
}

protected function setDatetime($timezoneName = 'UTC')
{
$timezone = new DateTimeZone($timezoneName);
$datetime = new DateTime('now', $timezone);
$this->datetime = $datetime;
$this->timezoneName = $timezoneName;
return $this;
}

protected function getPrefix($type)
{
$prefix = $this->getTimestamp();

switch ($type) {
case Logger::ERROR: return $prefix .= ' (!ERROR): ';
case Logger::WARNING: return $prefix .= ' (WARNING): ';
case Logger::INFO: return $prefix .= ' (INFO): ';
case Logger::SUCCESS: return $prefix .= ' (SUCCESS): ';
}
}

protected function setFilename($file)
{
if (!is_file($file)) {
$handle = fopen($file, 'w');
fwrite($handle, '======NEW LOG FILE=====' . PHP_EOL);
fclose($handle);
} elseif (!is_writable($file)) {
throw new Exception("{$file}: write: permission denied");
}
$fileinfo = pathinfo($file);
$this->ext = $fileinfo['extension'];
$this->filename = $fileinfo['filename'];
return $this;
}

protected function setDirname($dir)
{
if (!is_dir($dir)) {
mkdir($dir, 755, true);
} elseif (!is_writable($dir)) {
throw new Exception("{$dir}: open: permission denied");
}
$this->dirname = $dir;
return $this;
}

private function getTimestamp($format = "Y-m-d H:i:s")
{
return $this->datetime->format('[' . $format . ']');
}
<?php

namespace library\Debugger;

use DateTime;
use DateTimeZone;
use Exception;
use library\debugger\Logger;

class Core
{
const
ERROR = 1,
WARNING = 2,
INFO = 3,
SUCCESS = 4;

/** @var string */
protected $timezoneName = 'UTC';

/** @var Core */
protected static $logger;

/** @var DateTime */
private $datetime;

/** @var string */
private $filename;

/** @var string */
private $dirname;

/** @var string */
private $ext;

private function __construct()
{

}

private function __clone()
{

}

public static function add($message, $type = self::INFO)
{
if (!self::$logger) {
throw new Exception('No configured logger');
}
}

public static function setLogger($filename, $dirname, $timezone = 'UTC')
{
if (isset(self::$logger)) {
return self::$logger;
}
$core = new Core();

$core->setDatetime($timezone)
->setDirname($dirname)
->setFilename($filename);
return self::$logger = $core;
}

protected function save($message)
{
return (file_put_contents($this->getPath(), $message, FILE_APPEND));
}

protected function getPath()
{
return $this->dirname . DIRECTORY_SEPARATOR . $this->filename . '.' . $this->ext;
}

protected function getFilename()
{
return $this->filename;
}

protected function getDirname()
{
return $this->dirname;
}

protected function getExt()
{
return $this->ext;
}

protected function flatten(array $array, $prefix)
{
$string = '';
foreach ($array as $row) {
if (is_array($row)) {
$row = $this->flatten($row);
}
if (is_string($row)) {
$string .= $prefix . trim($row) . PHP_EOL;
}
}
return ($string) ? $string : false;
}

protected function setDatetime($timezoneName = 'UTC')
{
$timezone = new DateTimeZone($timezoneName);
$datetime = new DateTime('now', $timezone);
$this->datetime = $datetime;
$this->timezoneName = $timezoneName;
return $this;
}

protected function getPrefix($type)
{
$prefix = $this->getTimestamp();

switch ($type) {
case Logger::ERROR: return $prefix .= ' (!ERROR): ';
case Logger::WARNING: return $prefix .= ' (WARNING): ';
case Logger::INFO: return $prefix .= ' (INFO): ';
case Logger::SUCCESS: return $prefix .= ' (SUCCESS): ';
}
}

protected function setFilename($file)
{
if (!is_file($file)) {
$handle = fopen($file, 'w');
fwrite($handle, '======NEW LOG FILE=====' . PHP_EOL);
fclose($handle);
} elseif (!is_writable($file)) {
throw new Exception("{$file}: write: permission denied");
}
$fileinfo = pathinfo($file);
$this->ext = $fileinfo['extension'];
$this->filename = $fileinfo['filename'];
return $this;
}

protected function setDirname($dir)
{
if (!is_dir($dir)) {
mkdir($dir, 755, true);
} elseif (!is_writable($dir)) {
throw new Exception("{$dir}: open: permission denied");
}
$this->dirname = $dir;
return $this;
}

private function getTimestamp($format = "Y-m-d H:i:s")
{
return $this->datetime->format('[' . $format . ']');
}
}
Original file line number Diff line number Diff line change
@@ -1,26 +1,26 @@
<?php

namespace library\Debugger;

use library\debugger\Core;
use library\botInterface\Logger as LoggerInterface;
use Exception;

class Logger extends Core implements LoggerInterface
{

public static function add($message, $type = self::INFO)
{
parent::add($message, $type);
$prefix = self::$logger->getPrefix($type);
if (is_array($message)) {
$message = self::$logger->flatten($message, $prefix);
} elseif (is_string($message)) {
$message = $prefix . trim($message) . PHP_EOL;
} else {
throw new Exception('Message must be a string or array. Is ' . gettype($message));
}
self::$logger->save($message);
return $message;
}
<?php

namespace library\Debugger;

use library\debugger\Core;
use library\botInterface\Logger as LoggerInterface;
use Exception;

class Logger extends Core implements LoggerInterface
{

public static function add($message, $type = self::INFO)
{
parent::add($message, $type);
$prefix = self::$logger->getPrefix($type);
if (is_array($message)) {
$message = self::$logger->flatten($message, $prefix);
} elseif (is_string($message)) {
$message = $prefix . trim($message) . PHP_EOL;
} else {
throw new Exception('Message must be a string or array. Is ' . gettype($message));
}
self::$logger->save($message);
return $message;
}
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Loading

0 comments on commit a876409

Please sign in to comment.