From 1535d8fdbe3c340b5f951aedd04126f737076f2e Mon Sep 17 00:00:00 2001 From: Dennis Eichhorn Date: Fri, 12 Feb 2016 12:16:16 +0100 Subject: [PATCH] Cleanup --- Account/AccountManager.php | 35 +++++++++- ApplicationAbstract.php | 2 +- Config/SettingsAbstract.php | 2 +- DataStorage/Database/BuilderAbstract.php | 10 ++- DataStorage/Database/DataMapperAbstract.php | 19 +++++- Datatypes/Exception/InvalidEnumName.php | 12 +++- Datatypes/Exception/InvalidEnumValue.php | 12 +++- Datatypes/SmartDateTime.php | 39 +++++++++++ Dispatcher/Dispatcher.php | 14 ++-- Log/FileLogger.php | 17 ++++- Log/README.md | 1 - Math/README.md | 1 - Module/InfoManager.php | 70 +++++++++++++------ Module/ModuleAbstract.php | 19 +++++- Module/ModuleFactory.php | 2 - Module/ModuleManager.php | 1 - Stdlib/PriorityQueue.php | 2 - System/FilePathException.php | 12 +++- System/FileSystem.php | 76 ++++----------------- Uri/InvalidUriException.php | 12 +++- Uri/UriFactory.php | 7 +- Utils/IO/Csv/CsvSettingsTrait.php | 16 ++++- 22 files changed, 262 insertions(+), 119 deletions(-) delete mode 100644 Log/README.md delete mode 100644 Math/README.md diff --git a/Account/AccountManager.php b/Account/AccountManager.php index 5d2dc9d45..58adcd736 100644 --- a/Account/AccountManager.php +++ b/Account/AccountManager.php @@ -26,7 +26,7 @@ namespace phpOMS\Account; * @link http://orange-management.com * @since 1.0.0 */ -class AccountManager +class AccountManager implements \Countable { /** @@ -83,4 +83,37 @@ class AccountManager return null; } + /** + * Remove account. + * + * @param int $id Account id + * + * @return bool + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public function remove(int $id) : bool + { + if (isset($this->accounts[$id])) { + unset($this->accounts[$id]); + + return true; + } + + return false; + } + + /** + * Get accounts count. + * + * @return int + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public function count() : int { + return count($this->accounts); + } + } diff --git a/ApplicationAbstract.php b/ApplicationAbstract.php index 6e4d3f3d1..10120960d 100644 --- a/ApplicationAbstract.php +++ b/ApplicationAbstract.php @@ -32,7 +32,7 @@ class ApplicationAbstract /** * App name. * - * @var \phpOMS\DataStorage\Database\Pool + * @var string * @since 1.0.0 */ public $appName = ''; diff --git a/Config/SettingsAbstract.php b/Config/SettingsAbstract.php index 91e7b4428..e1219ed35 100644 --- a/Config/SettingsAbstract.php +++ b/Config/SettingsAbstract.php @@ -39,7 +39,7 @@ abstract class SettingsAbstract implements OptionsInterface /** * Cache manager (pool). * - * @var \phpOMS\DataStorage\Cache\CacheManager + * @var \phpOMS\DataStorage\Cache\Pool * @since 1.0.0 */ protected $cache = null; diff --git a/DataStorage/Database/BuilderAbstract.php b/DataStorage/Database/BuilderAbstract.php index 0220856cf..728d84919 100644 --- a/DataStorage/Database/BuilderAbstract.php +++ b/DataStorage/Database/BuilderAbstract.php @@ -30,10 +30,16 @@ use phpOMS\DataStorage\Database\Connection\ConnectionAbstract; */ abstract class BuilderAbstract { + /** + * Grammar. + * + * @var GrammarAbstract + * @since 1.0.0 + */ protected $grammar = null; /** - * Database connectino. + * Database connection. * * @var ConnectionAbstract * @since 1.0.0 @@ -61,7 +67,7 @@ abstract class BuilderAbstract * * @param string $prefix Prefix * - * @return BuilderAbstract + * @return $this * * @since 1.0.0 * @author Dennis Eichhorn diff --git a/DataStorage/Database/DataMapperAbstract.php b/DataStorage/Database/DataMapperAbstract.php index 0afe55ea9..54f713a4f 100644 --- a/DataStorage/Database/DataMapperAbstract.php +++ b/DataStorage/Database/DataMapperAbstract.php @@ -226,7 +226,8 @@ abstract class DataMapperAbstract implements DataMapperInterface /** * Create object in db. * - * @param mixed $obj Object reference (gets filled with insert id) + * @param mixed $obj Object reference (gets filled with insert id) + * @param bool $relations Create all relations as well * * @return mixed * @@ -256,11 +257,13 @@ abstract class DataMapperAbstract implements DataMapperInterface /* Insert has one first */ if (isset(static::$hasOne[$pname]) && is_object($relObj = $property->getValue($obj))) { /* only insert if not already inserted */ + /** @var DataMapperAbstract $mapper */ $mapper = static::$hasOne[$pname]['mapper']; $mapper = new $mapper($this->db); $relReflectionClass = new \ReflectionClass(get_class($relObj)); - $relProperty = $relReflectionClass->getProperty($mapper::$columns[$mapper::$primaryField]['internal']); + /** @var array $columns */ + $relProperty = $relReflectionClass->getProperty($mapper::$columns[$mapper::$primaryField]['internal']); $relProperty->setAccessible(true); $primaryKey = $relProperty->getValue($relObj); $relProperty->setAccessible(false); @@ -327,6 +330,7 @@ abstract class DataMapperAbstract implements DataMapperInterface foreach ($values as $key => &$value) { // Skip if already in db/has key + /** @noinspection PhpUndefinedVariableInspection */ $relProperty = $relReflectionClass->getProperty($mapper::$columns[$mapper::$primaryField]['internal']); $relProperty->setAccessible(true); $primaryKey = $relProperty->getValue($value); @@ -552,6 +556,7 @@ abstract class DataMapperAbstract implements DataMapperInterface $reflectionProperty->setAccessible(true); } + /** @var DataMapperAbstract $mapper */ $mapper = static::$hasOne[$column]['mapper']; $mapper = new $mapper($this->db); @@ -792,6 +797,16 @@ abstract class DataMapperAbstract implements DataMapperInterface return $this->populateIterable($results); } + /** + * Get raw by primary key + * + * @param mixed $primaryKey Primary key + * + * @return array + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ public function getManyRaw($primaryKey) : array { $result = []; diff --git a/Datatypes/Exception/InvalidEnumName.php b/Datatypes/Exception/InvalidEnumName.php index b32b52619..67835071b 100644 --- a/Datatypes/Exception/InvalidEnumName.php +++ b/Datatypes/Exception/InvalidEnumName.php @@ -31,7 +31,17 @@ namespace phpOMS\Datatypes\Exception; class InvalidEnumName extends \UnexpectedValueException { - public function __construct($message, $code = 0, \Exception $previous = null) + /** + * Constructor. + * + * @param string $message Exception message + * @param int $code Exception code + * @param \Exception Previous exception + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public function __construct(string $message, int $code = 0, \Exception $previous = null) { parent::__construct('The enum name "' . $message . '" is not valid.', $code, $previous); } diff --git a/Datatypes/Exception/InvalidEnumValue.php b/Datatypes/Exception/InvalidEnumValue.php index 32c04b1d3..2ecebed96 100644 --- a/Datatypes/Exception/InvalidEnumValue.php +++ b/Datatypes/Exception/InvalidEnumValue.php @@ -31,7 +31,17 @@ namespace phpOMS\Datatypes\Exception; class InvalidEnumValue extends \UnexpectedValueException { - public function __construct($message, $code = 0, \Exception $previous = null) + /** + * Constructor. + * + * @param string $message Exception message + * @param int $code Exception code + * @param \Exception Previous exception + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public function __construct(string $message, int $code = 0, \Exception $previous = null) { parent::__construct('The enum value "' . $message . '" is not valid.', $code, $previous); } diff --git a/Datatypes/SmartDateTime.php b/Datatypes/SmartDateTime.php index 8ce9e779a..c708ed837 100644 --- a/Datatypes/SmartDateTime.php +++ b/Datatypes/SmartDateTime.php @@ -30,7 +30,20 @@ namespace phpOMS\Datatypes; */ class SmartDateTime extends \DateTime { + /** + * Default format + * + * @var string + * @since 1.0.0 + */ const FORMAT = 'Y-m-d hh:mm:ss'; + + /** + * Default timezone + * + * @var string + * @since 1.0.0 + */ const TIMEZONE = 'UTC'; /** @@ -103,4 +116,30 @@ class SmartDateTime extends \DateTime return $this; } + /** + * Get days of current month + * + * @return int + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public function getDaysOfMonth() : int + { + return cal_days_in_month(CAL_GREGORIAN, $this->format('m'), $this->format('Y')); + } + + /** + * Get first day of current month + * + * @return int + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public function getFirstDayOfMonth() : int + { + return getdate(mktime(null, null, null, $this->format('m'), 1, $this->format('Y')))['wday']; + } + } diff --git a/Dispatcher/Dispatcher.php b/Dispatcher/Dispatcher.php index 04d3da311..6540a5e47 100644 --- a/Dispatcher/Dispatcher.php +++ b/Dispatcher/Dispatcher.php @@ -71,9 +71,9 @@ class Dispatcher * Dispatch controller. * * @param string|array|\Closure $controller Controller string - * @param RequestAbstract $request Request - * @param ResponseAbstract $response Response - * @param mixed $data Data + * @param RequestAbstract $request Request + * @param ResponseAbstract $response Response + * @param mixed $data Data * * @return array * @@ -94,11 +94,13 @@ class Dispatcher $dispatch = explode(':', $controller); $this->get($dispatch[0]); - if (count($dispatch) == 3) { + if (($c = count($dispatch)) == 3) { /* Handling static functions */ $views[$type][$controller] = $dispatch[0]::$dispatch[2](); - } else { + } elseif ($c == 2) { $views[$type][$controller] = $this->controllers[$dispatch[0]]->{$dispatch[1]}($request, $response, $data); + } else { + throw new \UnexpectedValueException('Unexpected function.'); } } elseif (is_array($controller)) { foreach ($controller as $controllerSingle) { @@ -142,7 +144,7 @@ class Dispatcher * Set controller by alias. * * @param ModuleAbstract $controller Controller - * @param string $name Controller string + * @param string $name Controller string * * @return bool * diff --git a/Log/FileLogger.php b/Log/FileLogger.php index d6ad24e8f..6d765993f 100644 --- a/Log/FileLogger.php +++ b/Log/FileLogger.php @@ -611,12 +611,23 @@ class FileLogger implements LoggerInterface return $log; } - public function console(string $text, bool $verbose) + /** + * Create console log. + * + * @param string $message Log message + * @param bool $verbose Is verbose + * @param array $context Context + * + * @return array + */ + public function console(string $message, bool $verbose = true, array $context = []) { - $text = date('[Y-m-d H:i:s] ') . $text . "\r\n"; + $message = date('[Y-m-d H:i:s] ') . $message . "\r\n"; if ($verbose) { - echo $text; + echo $message; + } else { + $this->info($message, $context); } } } diff --git a/Log/README.md b/Log/README.md deleted file mode 100644 index b28451d99..000000000 --- a/Log/README.md +++ /dev/null @@ -1 +0,0 @@ -# Log # diff --git a/Math/README.md b/Math/README.md deleted file mode 100644 index 5a64d915b..000000000 --- a/Math/README.md +++ /dev/null @@ -1 +0,0 @@ -# Math # diff --git a/Module/InfoManager.php b/Module/InfoManager.php index 44b661b68..275d008ed 100644 --- a/Module/InfoManager.php +++ b/Module/InfoManager.php @@ -14,7 +14,9 @@ * @link http://orange-management.com */ namespace phpOMS\Module; + use phpOMS\System\FilePathException; +use phpOMS\Utils\ArrayUtils; use phpOMS\Validation\Validator; /** @@ -34,20 +36,20 @@ class InfoManager { /** - * File pointer. - * - * @var mixed - * @since 1.0.0 - */ - private $fp = null; - - /** - * Module path. + * File path. * * @var string * @since 1.0.0 */ - const MODULE_PATH = __DIR__ . '/../../Modules/'; + private $path = ''; + + /** + * Info data. + * + * @var array + * @since 1.0.0 + */ + private $info = []; /** * Object constructor. @@ -59,26 +61,54 @@ class InfoManager */ public function __construct(string $module) { - if (($path = realpath($oldPath = self::MODULE_PATH . $module . '/info.json')) === false || Validator::startsWith($path, self::MODULE_PATH)) { + if (($path = realpath($oldPath = ModuleAbstract::MODULE_PATH . '/' . $module . '/info.json')) === false || Validator::startsWith($path, ModuleAbstract::MODULE_PATH)) { throw new FilePathException($oldPath); } - $this->fp = fopen($oldPath, 'r'); - } - - public function update() - { - // TODO: update file (convert to json) + $this->path = $path; + $this->info = json_decode(file_get_contents($this->path), true); } /** - * Object destructor. + * Update info file * * @since 1.0.0 * @author Dennis Eichhorn */ - public function __destruct() + public function update() { - $this->fp->close(); + file_put_contents($this->path, json_encode($this->info, JSON_PRETTY_PRINT)); + } + + /** + * Set data + * + * @param string $path Value path + * @param mixed $data Scalar or array of data to set + * @param string $delim Delimiter of path + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public function set(string $path, $data, string $delim = '/') + { + if (!is_scalar($data) || !is_array($data)) { + throw new \InvalidArgumentException('Type of $data "' . gettype($data) . '" is not supported.'); + } + + ArrayUtils::setArray($path, $this->info, $data, $delim); + } + + /** + * Get info data. + * + * @return array + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public function get() : array + { + return $this->info; } } diff --git a/Module/ModuleAbstract.php b/Module/ModuleAbstract.php index 275d4a78a..cee1efdf2 100644 --- a/Module/ModuleAbstract.php +++ b/Module/ModuleAbstract.php @@ -14,6 +14,7 @@ * @link http://orange-management.com */ namespace phpOMS\Module; + use phpOMS\System\FilePathException; @@ -61,7 +62,7 @@ abstract class ModuleAbstract * @var string * @since 1.0.0 */ - const MODULE_PATH = __DIR__; + const MODULE_PATH = __DIR__ . '/../../Modules'; /** * Module version. @@ -148,8 +149,9 @@ abstract class ModuleAbstract { $lang = []; if (isset(static::$localization[$destination])) { + /** @noinspection PhpUnusedLocalVariableInspection */ foreach (static::$localization[$destination] as $file) { - if(($path = realpath($oldPath = __DIR__ . '/../../Modules/' . static::MODULE_NAME . '/Theme/' . $destination . '/Lang/' . $language . '.lang.php')) === false) { + if (($path = realpath($oldPath = __DIR__ . '/../../Modules/' . static::MODULE_NAME . '/Theme/' . $destination . '/Lang/' . $language . '.lang.php')) === false) { throw new FilePathException($oldPath); } @@ -197,4 +199,17 @@ abstract class ModuleAbstract /** @noinspection PhpUndefinedFieldInspection */ return static::$dependencies; } + + /** + * Get event id prefix. + * + * @return string + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public function getEventId() : string + { + return static::class; + } } diff --git a/Module/ModuleFactory.php b/Module/ModuleFactory.php index b382339e6..44355e08b 100644 --- a/Module/ModuleFactory.php +++ b/Module/ModuleFactory.php @@ -16,8 +16,6 @@ namespace phpOMS\Module; use phpOMS\ApplicationAbstract; -use phpOMS\Module\NullModule; - /** * ModuleFactory class. diff --git a/Module/ModuleManager.php b/Module/ModuleManager.php index 78027e0b8..6226ca460 100644 --- a/Module/ModuleManager.php +++ b/Module/ModuleManager.php @@ -22,7 +22,6 @@ use phpOMS\Message\Http\Request; use phpOMS\System\FilePathException; use phpOMS\Utils\IO\Json\InvalidJsonException; - /** * Modules class. * diff --git a/Stdlib/PriorityQueue.php b/Stdlib/PriorityQueue.php index ad65be1cd..fa829c33d 100644 --- a/Stdlib/PriorityQueue.php +++ b/Stdlib/PriorityQueue.php @@ -21,8 +21,6 @@ namespace phpOMS\Stdlib; * @category Stdlib * @package Framework * @since 1.0.0 - * - * @todo : implement JsonableInterface */ class PriorityQueue implements \Countable, \Serializable { diff --git a/System/FilePathException.php b/System/FilePathException.php index 0e2f9957a..a24204166 100644 --- a/System/FilePathException.php +++ b/System/FilePathException.php @@ -30,7 +30,17 @@ namespace phpOMS\System; */ class FilePathException extends \UnexpectedValueException { - public function __construct($message, $code = 0, \Exception $previous = null) + /** + * Constructor. + * + * @param string $message Exception message + * @param int $code Exception code + * @param \Exception Previous exception + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public function __construct(string $message, int $code = 0, \Exception $previous = null) { parent::__construct('The path "' . $message . '" is not a valid path.', $code, $previous); } diff --git a/System/FileSystem.php b/System/FileSystem.php index 8b506d5ea..1ffa85ea7 100644 --- a/System/FileSystem.php +++ b/System/FileSystem.php @@ -48,7 +48,7 @@ class FileSystem * * @param string $path Path to folder * @param bool $recursive Should sub folders be counted as well? - * @param array $ignore Ignore these sub-paths + * @param array $ignore Ignore these sub-paths * * @return int * @@ -56,7 +56,7 @@ class FileSystem * @author Dennis Eichhorn */ public static function getFileCount(string $path, bool $recursive = true, array $ignore = ['.', '..', 'cgi-bin', - '.DS_Store']) + '.DS_Store']) { $size = 0; $files = scandir($path); @@ -77,23 +77,21 @@ class FileSystem return $size; } - public function copy() - { - } - - public function rename() - { - } - - public function move() - { - } - + /** + * Delete directory and all its content. + * + * @param string $path Path to folder + * + * @return bool + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ public static function deletePath($path) : bool { $path = realpath($oldPath = $path); if ($path === false || !is_dir($path) || Validator::startsWith($path, ROOT_PATH)) { - return false; + throw new FilePathException($oldPath); } $files = scandir($path); @@ -114,52 +112,4 @@ class FileSystem return true; } - - public function touch() - { - } - - public function mkdir() - { - } - - public function exists() - { - } - - public function chmod() - { - } - - public function chown() - { - } - - public function chgrp() - { - } - - public function symlink() - { - } - - public function toRelative() - { - } - - public function toAbsolute() - { - } - - public function isRelative() - { - } - - public function isAbsolute() - { - } - - public function dump() - { - } } diff --git a/Uri/InvalidUriException.php b/Uri/InvalidUriException.php index cd2d9ea0b..117079e6f 100644 --- a/Uri/InvalidUriException.php +++ b/Uri/InvalidUriException.php @@ -28,7 +28,17 @@ namespace phpOMS\Uri; */ class InvalidUriException extends \UnexpectedValueException { - public function __construct($message, $code = 0, \Exception $previous = null) + /** + * Constructor. + * + * @param string $message Exception message + * @param int $code Exception code + * @param \Exception Previous exception + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public function __construct(string $message, int $code = 0, \Exception $previous = null) { parent::__construct('The uri "' . $message . '" is not valid.', $code, $previous); } diff --git a/Uri/UriFactory.php b/Uri/UriFactory.php index 93cabc7ca..6f52b90c2 100644 --- a/Uri/UriFactory.php +++ b/Uri/UriFactory.php @@ -15,8 +15,6 @@ */ namespace phpOMS\Uri; - - /** * UriFactory class. * @@ -93,8 +91,7 @@ class UriFactory * Build uri. * * @param string $uri Path data - * @param array $toMatch Optional special replacements - * @param UriScheme|int $scheme Scheme type + * @param array $toMatch Optional special replacements * * @return null|string * @@ -103,7 +100,7 @@ class UriFactory * @since 1.0.0 * @author Dennis Eichhorn */ - public static function build(string $uri, array $toMatch = [], int $scheme = UriScheme::HTTP) + public static function build(string $uri, array $toMatch = []) { $uri = preg_replace_callback('(\{[\/#\?@\.\$][a-zA-Z0-9]*\})', function ($match) use ($toMatch) { $match = substr($match[0], 1, strlen($match[0]) - 2); diff --git a/Utils/IO/Csv/CsvSettingsTrait.php b/Utils/IO/Csv/CsvSettingsTrait.php index 41654955a..59b01f07f 100644 --- a/Utils/IO/Csv/CsvSettingsTrait.php +++ b/Utils/IO/Csv/CsvSettingsTrait.php @@ -22,9 +22,21 @@ namespace phpOMS\Utils\IO\Csv; * @package phpOMS\Config * @since 1.0.0 */ -trait CsvSettingsTrait +class CsvSettings { - private function getFileDelimiter($file, int $checkLines = 2, array $delimiters = [',', '\t', ';', '|', ':']) : string + /** + * Get csv file delimiter. + * + * @param mixed $file File resource + * @param int $checkLines Lines to check for evaluation + * @param array $delimiters Potential delimiters + * + * @return string + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public static function getFileDelimiter($file, int $checkLines = 2, array $delimiters = [',', '\t', ';', '|', ':']) : string { $results = [];