From e30dd2f5b42473e927e755c687793ba16ab15384 Mon Sep 17 00:00:00 2001 From: Denis Ziberi Date: Thu, 11 Nov 2021 15:05:17 +0100 Subject: [PATCH 01/24] init --- .gitignore | 2 ++ App/Model/BaseModelInterface.php | 8 +++++ App/Model/Fast.php | 56 ++++++++++++++++++++++++++++++++ App/Model/Model.php | 9 +++++ composer.json | 7 ++++ index.php | 16 +++++++++ store.json | 38 ++++++++++++++++++++++ 7 files changed, 136 insertions(+) create mode 100644 .gitignore create mode 100644 App/Model/BaseModelInterface.php create mode 100644 App/Model/Fast.php create mode 100644 App/Model/Model.php create mode 100644 composer.json create mode 100644 index.php create mode 100644 store.json diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..c8153b5 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +/composer.lock +/vendor/ diff --git a/App/Model/BaseModelInterface.php b/App/Model/BaseModelInterface.php new file mode 100644 index 0000000..fa31a0a --- /dev/null +++ b/App/Model/BaseModelInterface.php @@ -0,0 +1,8 @@ +dates) && !in_array($parameter, $this->guarded)) { + return new DateTime($this->$parameter); + } elseif (in_array($parameter, $this->guarded)) { + throw new Exception('guarded property'); + } + + return $this->$parameter; + } + + + + + + + +} + + diff --git a/App/Model/Model.php b/App/Model/Model.php new file mode 100644 index 0000000..306f67f --- /dev/null +++ b/App/Model/Model.php @@ -0,0 +1,9 @@ +end->diff($fast->start); + +/*var_dump($diff->format('Y-m-d H:i:s'));*/ +echo $diff->format('%Y %m months %d %h %i %s '); +/*} catch (\Exception $e) { + echo $e->getMessage(); +}*/ + diff --git a/store.json b/store.json new file mode 100644 index 0000000..2932777 --- /dev/null +++ b/store.json @@ -0,0 +1,38 @@ +{ + "1": { + "status": "Active/Inactive", + "start": "dateformat", + "end": "dateformat", + "elapsed_time": "time", + "fast_type": "" + }, + "1": { + "status": "Active/Inactive", + "start": "dateformat", + "end": "dateformat", + "elapsed_time": "time", + "fast_type": "" + }, + "1": { + "status": "Active/Inactive", + "start": "dateformat", + "end": "dateformat", + "elapsed_time": "time", + "fast_type": "" + }, + "1": { + "status": "Active/Inactive", + "start": "dateformat", + "end": "dateformat", + "elapsed_time": "time", + "fast_type": "" + }, + "2": { + "status": "Active/Inactive", + "start": "dateformat", + "end": "dateformat", + "elapsed_time": "time", + "fast_type": "" + } + +} \ No newline at end of file From 41e202cc9cff9feb07506517db82684734731131 Mon Sep 17 00:00:00 2001 From: Denis Ziberi Date: Fri, 12 Nov 2021 12:22:53 +0100 Subject: [PATCH 02/24] Created main coman controller StoreManager --- App/Commands/CommandController.php | 67 +++++++++++++++++++ App/Commands/CreateCommand.php | 41 ++++++++++++ App/Commands/ListCommand.php | 8 +++ App/Console/FileManagerInterface.php | 12 ++++ App/Console/InputConsole.php | 16 +++++ App/Console/OutputConsole.php | 13 ++++ App/Interface/BaseCommandInterface.php | 10 +++ .../BaseModelInterface.php | 0 App/Model/Fast.php | 8 --- App/Store/StoreManager.php | 31 +++++++++ index.php | 20 +++--- store.json | 30 ++------- 12 files changed, 214 insertions(+), 42 deletions(-) create mode 100644 App/Commands/CommandController.php create mode 100644 App/Commands/CreateCommand.php create mode 100644 App/Commands/ListCommand.php create mode 100644 App/Console/FileManagerInterface.php create mode 100644 App/Console/InputConsole.php create mode 100644 App/Console/OutputConsole.php create mode 100644 App/Interface/BaseCommandInterface.php rename App/{Model => Interface}/BaseModelInterface.php (100%) create mode 100644 App/Store/StoreManager.php diff --git a/App/Commands/CommandController.php b/App/Commands/CommandController.php new file mode 100644 index 0000000..ee30f43 --- /dev/null +++ b/App/Commands/CommandController.php @@ -0,0 +1,67 @@ + CreateCommand::class, + 4 => '', + 5 => ListCommand::class + ]; + + private array $options = [ + "1" => "Create a fast", + "2" => "Create a fast", + "3" => "Create a fast", + "4" => "Exit", + ]; + + public function __construct( + protected InputConsole $input, + protected OutputConsole $output, + protected StoreManager $store + ) + { + } + + + public function run() + { + + while ($this->appRunning) { + foreach ($this->options as $key => $option) { + $this->output->write("[" . $key . "] " . $option); + } + $input = $this->input->getInput(); + if (key_exists($input, $this->commands) and $input != "4") { + $command = new $this->commands["$input"]($this->input, $this->output); + + $command->run(); + } elseif ($input == "4") { + $this->appRunning = false; + } + $this->run(); + } + + } + + public function exit() + { + $this->appRunning = false; + } + + +} \ No newline at end of file diff --git a/App/Commands/CreateCommand.php b/App/Commands/CreateCommand.php new file mode 100644 index 0000000..6fbd7f0 --- /dev/null +++ b/App/Commands/CreateCommand.php @@ -0,0 +1,41 @@ +getStartDate(); + + $this->getFastType(); + + } + + protected function getStartDate() + { + $this->output->write('Enter Start Date of Fast'); + + $userInput = $this->input->getInput(); + + $this->output->write($userInput); + } + + protected function getFastType() + { + $this->output->write('Select a fast type'); + + $userInput = $this->input->getInput(); + + $this->output->write($userInput); + } +} \ No newline at end of file diff --git a/App/Commands/ListCommand.php b/App/Commands/ListCommand.php new file mode 100644 index 0000000..0fcf2dc --- /dev/null +++ b/App/Commands/ListCommand.php @@ -0,0 +1,8 @@ +$parameter; } - - - - - - - } diff --git a/App/Store/StoreManager.php b/App/Store/StoreManager.php new file mode 100644 index 0000000..aa8183b --- /dev/null +++ b/App/Store/StoreManager.php @@ -0,0 +1,31 @@ +file); + } + + public function select($key) + { + // TODO: Implement select() method. + } + + public function write(object $fast) + { + // TODO: Implement write() method. + } +} \ No newline at end of file diff --git a/index.php b/index.php index fbacfc4..49cdce4 100644 --- a/index.php +++ b/index.php @@ -1,16 +1,20 @@ end->diff($fast->start); +$commands->run(); -/*var_dump($diff->format('Y-m-d H:i:s'));*/ -echo $diff->format('%Y %m months %d %h %i %s '); -/*} catch (\Exception $e) { - echo $e->getMessage(); -}*/ diff --git a/store.json b/store.json index 2932777..790ec35 100644 --- a/store.json +++ b/store.json @@ -1,38 +1,16 @@ -{ - "1": { +[ + { "status": "Active/Inactive", "start": "dateformat", "end": "dateformat", "elapsed_time": "time", "fast_type": "" }, - "1": { - "status": "Active/Inactive", - "start": "dateformat", - "end": "dateformat", - "elapsed_time": "time", - "fast_type": "" - }, - "1": { - "status": "Active/Inactive", - "start": "dateformat", - "end": "dateformat", - "elapsed_time": "time", - "fast_type": "" - }, - "1": { - "status": "Active/Inactive", - "start": "dateformat", - "end": "dateformat", - "elapsed_time": "time", - "fast_type": "" - }, - "2": { + { "status": "Active/Inactive", "start": "dateformat", "end": "dateformat", "elapsed_time": "time", "fast_type": "" } - -} \ No newline at end of file +] \ No newline at end of file From 49c69969122d3597bb2b77f473a84d763cf39fa8 Mon Sep 17 00:00:00 2001 From: Denis Ziberi Date: Fri, 12 Nov 2021 14:31:00 +0100 Subject: [PATCH 03/24] implemented listing for fasts --- App/Commands/CommandController.php | 15 ++++--- App/Commands/CreateCommand.php | 15 ++++--- App/Commands/ListCommand.php | 31 ++++++++++++- App/Console/OutputConsole.php | 2 +- App/Interface/BaseCommandInterface.php | 7 +++ App/Interface/BaseModelInterface.php | 8 ---- App/Interface/FastModelInterface.php | 14 ++++++ .../FileManagerInterface.php | 0 App/Model/Collection.php | 45 +++++++++++++++++++ App/Model/Fast.php | 30 ++++++------- App/Model/FastModel.php | 19 ++++++++ App/Model/Model.php | 9 ---- App/Store/StoreManager.php | 23 ++++++++-- store.json | 8 ++-- 14 files changed, 169 insertions(+), 57 deletions(-) delete mode 100644 App/Interface/BaseModelInterface.php create mode 100644 App/Interface/FastModelInterface.php rename App/{Console => Interface}/FileManagerInterface.php (100%) create mode 100644 App/Model/Collection.php create mode 100644 App/Model/FastModel.php delete mode 100644 App/Model/Model.php diff --git a/App/Commands/CommandController.php b/App/Commands/CommandController.php index ee30f43..8e1437d 100644 --- a/App/Commands/CommandController.php +++ b/App/Commands/CommandController.php @@ -5,7 +5,7 @@ use App\Console\InputConsole; use App\Console\OutputConsole; use App\Interface\BaseCommandInterface; -use App\Model\Fast; +use App\Interface\FileManagerInterface; use App\Store\StoreManager; class CommandController implements BaseCommandInterface @@ -24,15 +24,16 @@ class CommandController implements BaseCommandInterface private array $options = [ "1" => "Create a fast", - "2" => "Create a fast", - "3" => "Create a fast", + "2" => "Start a fast", "4" => "Exit", + "5" => "List all fasts", + ]; public function __construct( - protected InputConsole $input, - protected OutputConsole $output, - protected StoreManager $store + protected InputConsole $input, + protected OutputConsole $output, + protected FileManagerInterface $store ) { } @@ -47,7 +48,7 @@ public function run() } $input = $this->input->getInput(); if (key_exists($input, $this->commands) and $input != "4") { - $command = new $this->commands["$input"]($this->input, $this->output); + $command = new $this->commands["$input"]($this->input, $this->output, $this->store); $command->run(); } elseif ($input == "4") { diff --git a/App/Commands/CreateCommand.php b/App/Commands/CreateCommand.php index 6fbd7f0..a4a65a9 100644 --- a/App/Commands/CreateCommand.php +++ b/App/Commands/CreateCommand.php @@ -5,37 +5,38 @@ use App\Console\InputConsole; use App\Console\OutputConsole; use App\Interface\BaseCommandInterface; +use App\Store\StoreManager; class CreateCommand implements BaseCommandInterface { - public function __construct(protected InputConsole $input, protected OutputConsole $output) + public function __construct( + protected InputConsole $input, + protected OutputConsole $output, + protected StoreManager $store + ) { } public function run() { $this->getStartDate(); - $this->getFastType(); - } protected function getStartDate() { $this->output->write('Enter Start Date of Fast'); - $userInput = $this->input->getInput(); - $this->output->write($userInput); } protected function getFastType() { $this->output->write('Select a fast type'); - $userInput = $this->input->getInput(); - $this->output->write($userInput); } + + } \ No newline at end of file diff --git a/App/Commands/ListCommand.php b/App/Commands/ListCommand.php index 0fcf2dc..66bde72 100644 --- a/App/Commands/ListCommand.php +++ b/App/Commands/ListCommand.php @@ -2,7 +2,36 @@ namespace App\Commands; -class ListCommand +use App\Console\InputConsole; +use App\Console\OutputConsole; +use App\Interface\BaseCommandInterface; +use App\Model\Collection; +use App\Store\StoreManager; + +class ListCommand implements BaseCommandInterface { + public function __construct( + protected InputConsole $input, + protected OutputConsole $output, + protected StoreManager $store + ) + { + } + + public function run() + { + $fasts = $this->store->getAll(); + + $this->listFasts($fasts); + } + + private function listFasts(Collection $fasts) + { + foreach ($fasts as $key => $fast) { + $this->output->write("fast number:$key $fast"); + } + } + + } \ No newline at end of file diff --git a/App/Console/OutputConsole.php b/App/Console/OutputConsole.php index a677707..a071df0 100644 --- a/App/Console/OutputConsole.php +++ b/App/Console/OutputConsole.php @@ -7,7 +7,7 @@ class OutputConsole public function write($text) { - echo $text . "\n\r"; + echo sprintf("%s\n\r", $text); } } \ No newline at end of file diff --git a/App/Interface/BaseCommandInterface.php b/App/Interface/BaseCommandInterface.php index bd6003b..17ced6f 100644 --- a/App/Interface/BaseCommandInterface.php +++ b/App/Interface/BaseCommandInterface.php @@ -2,9 +2,16 @@ namespace App\Interface; + +use App\Console\InputConsole; +use App\Console\OutputConsole; +use App\Store\StoreManager; + interface BaseCommandInterface { + public function __construct(InputConsole $input, OutputConsole $output , StoreManager $store); + public function run(); } \ No newline at end of file diff --git a/App/Interface/BaseModelInterface.php b/App/Interface/BaseModelInterface.php deleted file mode 100644 index fa31a0a..0000000 --- a/App/Interface/BaseModelInterface.php +++ /dev/null @@ -1,8 +0,0 @@ -items); + } + + public + function next() + { + return next($this->items); + } + + public + function key() + { + return key($this->items); + } + + public + function valid() + { + return current($this->items) !== false; + } + + public + function rewind() + { + return reset($this->items); + } +} \ No newline at end of file diff --git a/App/Model/Fast.php b/App/Model/Fast.php index 9a0fc23..057bc31 100644 --- a/App/Model/Fast.php +++ b/App/Model/Fast.php @@ -6,7 +6,7 @@ use Exception; -class Fast extends Model +class Fast extends FastModel { protected array $dates = [ @@ -14,19 +14,6 @@ class Fast extends Model 'end' ]; - protected array $guarded = [ - "type" - ]; - - - public function __construct( - protected bool $type, - protected string|DateTime $start, - protected string|DateTime $end, - ) - { - - } /** * @param $parameter @@ -35,14 +22,23 @@ public function __construct( */ public function __get($parameter) { - if (in_array($parameter, $this->dates) && !in_array($parameter, $this->guarded)) { + if (in_array($parameter, $this->dates)) { return new DateTime($this->$parameter); - } elseif (in_array($parameter, $this->guarded)) { - throw new Exception('guarded property'); } return $this->$parameter; } + + public function __toString() + { + return " + -------------------------------------- + Status ($this->status) \n\r + Started Fasting $this->start \n\r + Fast Type $this->type + -------------------------------------- + "; + } } diff --git a/App/Model/FastModel.php b/App/Model/FastModel.php new file mode 100644 index 0000000..b505cec --- /dev/null +++ b/App/Model/FastModel.php @@ -0,0 +1,19 @@ +file); + $fastArray = []; + $storeFasts = json_decode( + file_get_contents($this->file) + , false); + + foreach ($storeFasts as $fast) { + $fastArray[] = new Fast( + $fast->status, + $fast->start, + $fast->end, + $fast->elapsed_time, + $fast->type + ); + } + return new Collection($fastArray); } public function select($key) diff --git a/store.json b/store.json index 790ec35..a8764fe 100644 --- a/store.json +++ b/store.json @@ -1,16 +1,16 @@ [ { "status": "Active/Inactive", - "start": "dateformat", - "end": "dateformat", + "start": "2022-12-05 12:12:00", + "end": "2022-12-05 12:12:00", "elapsed_time": "time", - "fast_type": "" + "type": "1" }, { "status": "Active/Inactive", "start": "dateformat", "end": "dateformat", "elapsed_time": "time", - "fast_type": "" + "type": "1" } ] \ No newline at end of file From 42735d158123f570849a6644604ee730e1024e95 Mon Sep 17 00:00:00 2001 From: Denis Ziberi Date: Tue, 16 Nov 2021 10:28:28 +0100 Subject: [PATCH 04/24] Inputvalidation and CreateFast command --- App/Commands/CommandController.php | 1 + App/Commands/CreateCommand.php | 19 +++++++++++++++--- App/Console/InputConsole.php | 8 +++++++- App/Console/InputValidator.php | 29 ++++++++++++++++++++++++++++ App/Console/OutputConsole.php | 9 ++++++++- App/Enums/FastCategory.php | 24 +++++++++++++++++++++++ App/Interface/FastModelInterface.php | 13 ++++++++----- App/Model/Fast.php | 4 +--- App/Model/FastModel.php | 12 +++++++----- App/Store/StoreManager.php | 6 ++++-- index.php | 1 + store.json | 4 ++-- 12 files changed, 108 insertions(+), 22 deletions(-) create mode 100644 App/Console/InputValidator.php create mode 100644 App/Enums/FastCategory.php diff --git a/App/Commands/CommandController.php b/App/Commands/CommandController.php index 8e1437d..55f5d88 100644 --- a/App/Commands/CommandController.php +++ b/App/Commands/CommandController.php @@ -54,6 +54,7 @@ public function run() } elseif ($input == "4") { $this->appRunning = false; } + $this->run(); } diff --git a/App/Commands/CreateCommand.php b/App/Commands/CreateCommand.php index a4a65a9..008ee12 100644 --- a/App/Commands/CreateCommand.php +++ b/App/Commands/CreateCommand.php @@ -3,19 +3,27 @@ namespace App\Commands; use App\Console\InputConsole; +use App\Console\InputValidator; use App\Console\OutputConsole; use App\Interface\BaseCommandInterface; +use App\Model\Fast; use App\Store\StoreManager; class CreateCommand implements BaseCommandInterface { + protected InputValidator $validator; + protected Fast $newFast; public function __construct( - protected InputConsole $input, + protected InputConsole $input, protected OutputConsole $output, - protected StoreManager $store + protected StoreManager $store ) { + //TODO : Inject validator + $this->validator = new InputValidator(); + $this->newFast = new Fast(); + } public function run() @@ -26,8 +34,13 @@ public function run() protected function getStartDate() { - $this->output->write('Enter Start Date of Fast'); + $this->output->write('Enter Start Date of Fast format:(Y-m-d H:i:s) => (2020-10-10 20:00:00)'); $userInput = $this->input->getInput(); + + if ($message = $this->validator->validateStartdate($userInput)) { + $this->output->write($message); + $this->getStartDate(); + } $this->output->write($userInput); } diff --git a/App/Console/InputConsole.php b/App/Console/InputConsole.php index 50032d0..7b78ffc 100644 --- a/App/Console/InputConsole.php +++ b/App/Console/InputConsole.php @@ -9,7 +9,13 @@ class InputConsole public function getInput() { - return trim(fgets(STDIN), "\n\r"); + + //TODO:refactor + + $stdin = fopen('php://stdin', 'r'); + + + return trim(fgets($stdin), "\n\r"); } diff --git a/App/Console/InputValidator.php b/App/Console/InputValidator.php new file mode 100644 index 0000000..0ce7510 --- /dev/null +++ b/App/Console/InputValidator.php @@ -0,0 +1,29 @@ +format("Y-m-d H:i:s"); + $today = new DateTime('NOW'); + + + if (!$dateInputObject || ($formattedDateInput != $input)) { + return "Please enter valid format date."; + } else if ($dateInputObject < $today) { + return 'Please set starting date greater than today.'; + } + + return false; + + } + +} \ No newline at end of file diff --git a/App/Console/OutputConsole.php b/App/Console/OutputConsole.php index a071df0..8940b98 100644 --- a/App/Console/OutputConsole.php +++ b/App/Console/OutputConsole.php @@ -2,12 +2,19 @@ namespace App\Console; +use App\Enums\FastCategory; +use App\Model\Fast; + class OutputConsole { public function write($text) { - echo sprintf("%s\n\r", $text); + if (\enum_exists(FastCategory::class)) { + $myclass = FastCategory::LONG->hours(); + echo $myclass; + } + echo print_r($text) . "\n\r"; } } \ No newline at end of file diff --git a/App/Enums/FastCategory.php b/App/Enums/FastCategory.php new file mode 100644 index 0000000..d7c5033 --- /dev/null +++ b/App/Enums/FastCategory.php @@ -0,0 +1,24 @@ + 12, + FastCategory::INTERMEDIATE => 16, + FastCategory::MEDIUM => 20, + FastCategory::LONG => 32 + }; + + } + +} \ No newline at end of file diff --git a/App/Interface/FastModelInterface.php b/App/Interface/FastModelInterface.php index b47a1cb..0d5e690 100644 --- a/App/Interface/FastModelInterface.php +++ b/App/Interface/FastModelInterface.php @@ -2,13 +2,16 @@ namespace App\Interface; +use App\Enums\FastCategory; + + interface FastModelInterface { public function __construct( - string $status, - string $start, - string $end, - string $elapsedTime, - bool $type + string $status, + string $start, + string $end, + FastCategory $type, + string $elapsedTime ); } \ No newline at end of file diff --git a/App/Model/Fast.php b/App/Model/Fast.php index 057bc31..71a5a78 100644 --- a/App/Model/Fast.php +++ b/App/Model/Fast.php @@ -8,13 +8,11 @@ class Fast extends FastModel { - protected array $dates = [ 'start', 'end' ]; - /** * @param $parameter * @return DateTime @@ -35,7 +33,7 @@ public function __toString() -------------------------------------- Status ($this->status) \n\r Started Fasting $this->start \n\r - Fast Type $this->type + Fast Type {$this->type->name} -------------------------------------- "; } diff --git a/App/Model/FastModel.php b/App/Model/FastModel.php index b505cec..b42b4e5 100644 --- a/App/Model/FastModel.php +++ b/App/Model/FastModel.php @@ -2,18 +2,20 @@ namespace App\Model; +use App\Enums\FastCategory; use App\Interface\FastModelInterface; class FastModel implements FastModelInterface { public function __construct( - public string $status, - public string $start, - public string $end, - public bool|string $type, - public string|bool $elapsedTime = "0" + public string $status, + public string $start, + public string $end, + public FastCategory $type, + public string|bool $elapsedTime = "0" ) { + } } \ No newline at end of file diff --git a/App/Store/StoreManager.php b/App/Store/StoreManager.php index dd0a7b4..6860a77 100644 --- a/App/Store/StoreManager.php +++ b/App/Store/StoreManager.php @@ -3,6 +3,7 @@ namespace App\Store; +use App\Enums\FastCategory; use App\Interface\FileManagerInterface; use App\Model\Collection; use App\Model\Fast; @@ -29,8 +30,9 @@ public function getAll(): Collection $fast->status, $fast->start, $fast->end, - $fast->elapsed_time, - $fast->type + FastCategory::LONG, + $fast->elapsed_time + ); } return new Collection($fastArray); diff --git a/index.php b/index.php index 49cdce4..f4edac0 100644 --- a/index.php +++ b/index.php @@ -18,3 +18,4 @@ $commands->run(); + diff --git a/store.json b/store.json index a8764fe..64a1725 100644 --- a/store.json +++ b/store.json @@ -4,13 +4,13 @@ "start": "2022-12-05 12:12:00", "end": "2022-12-05 12:12:00", "elapsed_time": "time", - "type": "1" + "type": "SHORT" }, { "status": "Active/Inactive", "start": "dateformat", "end": "dateformat", "elapsed_time": "time", - "type": "1" + "type": "LONG" } ] \ No newline at end of file From ead243da92c6da56de1bf597bf8e6e8de8e0752f Mon Sep 17 00:00:00 2001 From: Denis Ziberi Date: Tue, 16 Nov 2021 21:52:25 +0100 Subject: [PATCH 05/24] update --- App/Commands/CommandController.php | 110 +++++++++++++++++++++------ App/Commands/CreateCommand.php | 23 +++++- App/Commands/UpdateCommand.php | 21 +++++ App/Console/InputValidator.php | 3 +- App/Console/OutputConsole.php | 9 +-- App/Enums/BasicEnum.php | 49 ++++++++++++ App/Enums/FastCategory.php | 24 ------ App/Enums/FastType.php | 17 +++++ App/Enums/Status.php | 16 ++++ App/Interface/FastModelInterface.php | 13 ++-- App/Model/Collection.php | 10 +++ App/Model/Fast.php | 39 ++++++++-- App/Model/FastModel.php | 21 ----- App/Store/StoreManager.php | 32 ++++++-- index.php | 4 +- store.json | 18 ++--- 16 files changed, 301 insertions(+), 108 deletions(-) create mode 100644 App/Commands/UpdateCommand.php create mode 100644 App/Enums/BasicEnum.php delete mode 100644 App/Enums/FastCategory.php create mode 100644 App/Enums/FastType.php create mode 100644 App/Enums/Status.php delete mode 100644 App/Model/FastModel.php diff --git a/App/Commands/CommandController.php b/App/Commands/CommandController.php index 55f5d88..35a28e3 100644 --- a/App/Commands/CommandController.php +++ b/App/Commands/CommandController.php @@ -4,29 +4,46 @@ use App\Console\InputConsole; use App\Console\OutputConsole; +use App\Enums\Status; use App\Interface\BaseCommandInterface; use App\Interface\FileManagerInterface; -use App\Store\StoreManager; +use App\Model\Collection; + class CommandController implements BaseCommandInterface { public bool $appRunning = true; - /** - * @var array|string[]|BaseCommandInterface[]d - */ - private array $commands = [ - 1 => CreateCommand::class, - 4 => '', - 5 => ListCommand::class - ]; - - private array $options = [ - "1" => "Create a fast", - "2" => "Start a fast", - "4" => "Exit", - "5" => "List all fasts", + protected bool $activeFasts = false; + + protected bool $canCreate = false; + + protected bool $noCondition = true; + + + private array $menu = [ + [ + 'option' => 'Create Fast', + 'command' => CreateCommand::class, + 'condition' => 'canCreate' + ], + [ + 'option' => 'Exit', + 'command' => "", + 'condition' => 'noCondition' + ], + [ + 'option' => 'Update an active fast', + 'command' => UpdateCommand::class, + 'condition' => 'activeFasts', + 'operand' => '> 0' + ], + [ + 'option' => 'List all fasts', + 'command' => ListCommand::class, + 'condition' => 'noCondition' + ], ]; @@ -43,16 +60,14 @@ public function run() { while ($this->appRunning) { - foreach ($this->options as $key => $option) { - $this->output->write("[" . $key . "] " . $option); - } + $this->updateActiveFast(); + $this->updateCanCreate(); + $this->printMenu(); $input = $this->input->getInput(); - if (key_exists($input, $this->commands) and $input != "4") { - $command = new $this->commands["$input"]($this->input, $this->output, $this->store); + if (key_exists($input, $this->menu)) { + $command = new $this->menu[$input]['command']($this->input, $this->output, $this->store); $command->run(); - } elseif ($input == "4") { - $this->appRunning = false; } $this->run(); @@ -65,5 +80,56 @@ public function exit() $this->appRunning = false; } + public function printMenu() + { + $counter = 0; + foreach ($this->menu as $key => $bundle) { + + $condition = $bundle['condition']; + + if ($this->{$condition}) { + + $this->output->write("[" . $key . "] " . $bundle['option']); + + + } + + } + } + + private function updateActiveFast() + { + /** + * @var $fasts Collection + */ + $fasts = $this->store->getAll(); + $fasts->each(callback: function ($key, $fast) { + + if ($fast->status == Status::ACTIVE) { + + $this->activeFasts = true; + + } + }); + + } + + private function updateCanCreate() + { + /** + * @var $fasts Collection + */ + $fasts = $this->store->getAll(); + $fasts->each(function ($key, $fast) { + if ($fast->status == Status::ACTIVE) { + + $this->canCreate = false; + return; + } + $this->canCreate = true; + + }); + } + } \ No newline at end of file diff --git a/App/Commands/CreateCommand.php b/App/Commands/CreateCommand.php index 008ee12..9a9bd94 100644 --- a/App/Commands/CreateCommand.php +++ b/App/Commands/CreateCommand.php @@ -5,16 +5,18 @@ use App\Console\InputConsole; use App\Console\InputValidator; use App\Console\OutputConsole; +use App\Enums\FastType; use App\Interface\BaseCommandInterface; use App\Model\Fast; use App\Store\StoreManager; +use JetBrains\PhpStorm\Pure; class CreateCommand implements BaseCommandInterface { protected InputValidator $validator; protected Fast $newFast; - public function __construct( + #[Pure] public function __construct( protected InputConsole $input, protected OutputConsole $output, protected StoreManager $store @@ -41,14 +43,29 @@ protected function getStartDate() $this->output->write($message); $this->getStartDate(); } - $this->output->write($userInput); + + + $this->newFast->set([ + 'start' => $userInput + ]); + } protected function getFastType() { $this->output->write('Select a fast type'); + $this->printFastTypes(); $userInput = $this->input->getInput(); - $this->output->write($userInput); + $this->newFast->set([ + 'type' => FastType::fromValue($userInput) + ]); + } + + protected function printFastTypes() + { + foreach (FastType::getAll() as $const => $value) { + $this->output->write($const . "($value" . 'h)'); + } } diff --git a/App/Commands/UpdateCommand.php b/App/Commands/UpdateCommand.php new file mode 100644 index 0000000..fcaafb5 --- /dev/null +++ b/App/Commands/UpdateCommand.php @@ -0,0 +1,21 @@ +format("Y-m-d H:i:s"); $today = new DateTime('NOW'); - if (!$dateInputObject || ($formattedDateInput != $input)) { + if (!$dateInputObject || ($dateInputObject->format("Y-m-d H:i:s") != $input)) { return "Please enter valid format date."; } else if ($dateInputObject < $today) { return 'Please set starting date greater than today.'; diff --git a/App/Console/OutputConsole.php b/App/Console/OutputConsole.php index 8940b98..9f93014 100644 --- a/App/Console/OutputConsole.php +++ b/App/Console/OutputConsole.php @@ -2,7 +2,7 @@ namespace App\Console; -use App\Enums\FastCategory; + use App\Model\Fast; class OutputConsole @@ -10,11 +10,8 @@ class OutputConsole public function write($text) { - if (\enum_exists(FastCategory::class)) { - $myclass = FastCategory::LONG->hours(); - echo $myclass; - } - echo print_r($text) . "\n\r"; + + echo $text . "\n\r"; } } \ No newline at end of file diff --git a/App/Enums/BasicEnum.php b/App/Enums/BasicEnum.php new file mode 100644 index 0000000..d24ab24 --- /dev/null +++ b/App/Enums/BasicEnum.php @@ -0,0 +1,49 @@ +getConstants(); + } + return self::$constCacheArray[$calledClass]; + } + + /** + * @param mixed $value + * @return false|int|string + */ + public static function fromValue(mixed $value): bool|int|string + { + $constants = self::getConstants(); + + return array_search($value, $constants); + } + + /** + * @param string $value + * @return bool + */ + public static function isValidType(string $value): bool + { + $constants = self::getConstants(); + return array_key_exists($value, $constants); + } + + public static function getAll() + { + return self::getConstants(); + } +} \ No newline at end of file diff --git a/App/Enums/FastCategory.php b/App/Enums/FastCategory.php deleted file mode 100644 index d7c5033..0000000 --- a/App/Enums/FastCategory.php +++ /dev/null @@ -1,24 +0,0 @@ - 12, - FastCategory::INTERMEDIATE => 16, - FastCategory::MEDIUM => 20, - FastCategory::LONG => 32 - }; - - } - -} \ No newline at end of file diff --git a/App/Enums/FastType.php b/App/Enums/FastType.php new file mode 100644 index 0000000..34b5607 --- /dev/null +++ b/App/Enums/FastType.php @@ -0,0 +1,17 @@ +items); } + + public function each(callable $callback) + { + foreach ($this as $key => $value) { + if ($callback($key, $value) === null) { + return; + } + } + return $this; + } } \ No newline at end of file diff --git a/App/Model/Fast.php b/App/Model/Fast.php index 71a5a78..b36f3ec 100644 --- a/App/Model/Fast.php +++ b/App/Model/Fast.php @@ -2,41 +2,70 @@ namespace App\Model; +use App\Enums\FastType; +use App\Enums\Status; +use App\Interface\FastModelInterface; use DateTime; use Exception; -class Fast extends FastModel +class Fast implements FastModelInterface { protected array $dates = [ 'start', 'end' ]; + public function __construct( + protected string $start = '', + protected int $status = 0, + protected string $end = '', + protected int $type = 0, + protected string $elapsedTime = '') + { + } + /** * @param $parameter - * @return DateTime + * @return DateTime|mixed * @throws Exception */ public function __get($parameter) { if (in_array($parameter, $this->dates)) { - return new DateTime($this->$parameter); + return new DateTime($this->{$parameter}); } return $this->$parameter; } + public function set(array $params) + { + foreach ($params as $param => $value) { + $this->$param = $value; + } + } + public function __toString() { + $type = FastType::fromValue($this->type); + $status = Status::fromValue($this->status); + + return " -------------------------------------- - Status ($this->status) \n\r + Status ($status) \n\r Started Fasting $this->start \n\r - Fast Type {$this->type->name} + Fast Type $type({$this->type}h) \n\r + Elapsed Time $this->elapsedTime; -------------------------------------- "; } + + public function setElapsedTime($time) + { + $this->elapsedTime = $time; + } } diff --git a/App/Model/FastModel.php b/App/Model/FastModel.php deleted file mode 100644 index b42b4e5..0000000 --- a/App/Model/FastModel.php +++ /dev/null @@ -1,21 +0,0 @@ -file) , false); foreach ($storeFasts as $fast) { - $fastArray[] = new Fast( - $fast->status, - $fast->start, - $fast->end, - FastCategory::LONG, - $fast->elapsed_time + $newFast = new Fast( + start: $fast->start, + status: $fast->status, + end: $fast->end, + type: $fast->type ); + if ($fast->status !== Status::ACTIVE) { + $newFast->setElapsedTime($today + ->diff($newFast->start) + ->format('%Y years %m months %d days %H h %i min %s sec')); + + } else { + $newFast->setElapsedTime($fast->elapsed_time); + } + $fastArray[] = $newFast; + +// $fastArray[] = new Fast( +// start: $fast->start, +// status: $fast->status, +// end: $fast->end, +// type: $fast->type, +// elapsedTime: $fast->elapsed_time); } return new Collection($fastArray); } diff --git a/index.php b/index.php index f4edac0..0995805 100644 --- a/index.php +++ b/index.php @@ -3,12 +3,14 @@ use App\Commands\CommandController; use App\Console\InputConsole; use App\Console\OutputConsole; +use App\Model\Fast; use App\Store\StoreManager; require_once './vendor/autoload.php'; +$obj = new Fast('2020-10-10 10:00:00', 0, '2020-10-11 10:00:00', 12, ''); + -//$fast = new \App\Model\Fast(1, '2021-10-11 10:22:00', date('Y-m-d H:m:s')); $commands = new CommandController( new InputConsole(), diff --git a/store.json b/store.json index 64a1725..7dc994b 100644 --- a/store.json +++ b/store.json @@ -1,16 +1,16 @@ [ { - "status": "Active/Inactive", - "start": "2022-12-05 12:12:00", + "status": 0, + "start": "2021-10-05 12:12:00", "end": "2022-12-05 12:12:00", - "elapsed_time": "time", - "type": "SHORT" + "elapsed_time": "00 years 1 months 11 days 06 h 41 min 47 sec", + "type": 20 }, { - "status": "Active/Inactive", - "start": "dateformat", - "end": "dateformat", - "elapsed_time": "time", - "type": "LONG" + "status": 0, + "start": "2021-10-05 12:12:00", + "end": "2021-10-05 12:12:00", + "elapsed_time": "00 years 1 months 11 days 06 h 41 min 47 sec", + "type": 12 } ] \ No newline at end of file From fc41b8a59a4526d40fe12552b0912d6abc6dfe0b Mon Sep 17 00:00:00 2001 From: Denis Ziberi Date: Wed, 17 Nov 2021 14:17:43 +0100 Subject: [PATCH 06/24] CommandController update updateCanCreate if store is empty add true --- App/Commands/CommandController.php | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/App/Commands/CommandController.php b/App/Commands/CommandController.php index 35a28e3..8569490 100644 --- a/App/Commands/CommandController.php +++ b/App/Commands/CommandController.php @@ -120,6 +120,10 @@ private function updateCanCreate() * @var $fasts Collection */ $fasts = $this->store->getAll(); + if (!$fasts) { + $this->canCreate = true; + return; + } $fasts->each(function ($key, $fast) { if ($fast->status == Status::ACTIVE) { @@ -129,6 +133,7 @@ private function updateCanCreate() $this->canCreate = true; }); + } From 12bece5dae1846eaced0f3b345b988b23db2d352 Mon Sep 17 00:00:00 2001 From: Denis Ziberi Date: Wed, 17 Nov 2021 14:18:51 +0100 Subject: [PATCH 07/24] updated Create command --- App/Commands/CreateCommand.php | 50 ++++++++++++++++++++++++++++++---- 1 file changed, 44 insertions(+), 6 deletions(-) diff --git a/App/Commands/CreateCommand.php b/App/Commands/CreateCommand.php index 9a9bd94..47b3086 100644 --- a/App/Commands/CreateCommand.php +++ b/App/Commands/CreateCommand.php @@ -15,6 +15,7 @@ class CreateCommand implements BaseCommandInterface { protected InputValidator $validator; protected Fast $newFast; + protected array $fastTypes; #[Pure] public function __construct( protected InputConsole $input, @@ -25,6 +26,7 @@ class CreateCommand implements BaseCommandInterface //TODO : Inject validator $this->validator = new InputValidator(); $this->newFast = new Fast(); + $this->setFastTypes(); } @@ -32,6 +34,8 @@ public function run() { $this->getStartDate(); $this->getFastType(); + $this->setFastEndDate(); + $this->saveFast(); } protected function getStartDate() @@ -43,12 +47,9 @@ protected function getStartDate() $this->output->write($message); $this->getStartDate(); } - - $this->newFast->set([ 'start' => $userInput ]); - } protected function getFastType() @@ -56,17 +57,54 @@ protected function getFastType() $this->output->write('Select a fast type'); $this->printFastTypes(); $userInput = $this->input->getInput(); + if (!key_exists($userInput, $this->fastTypes)) { + $this->output->write('Please choose from existing types.'); + $this->getFastType(); + } $this->newFast->set([ - 'type' => FastType::fromValue($userInput) + 'type' => $this->fastTypes[$userInput]['value'] ]); + return; } - protected function printFastTypes() + protected function setFastTypes() { foreach (FastType::getAll() as $const => $value) { - $this->output->write($const . "($value" . 'h)'); + $this->fastTypes[] = [ + 'const' => $const, + 'value' => $value + ]; } } + protected function printFastTypes() + { + foreach ($this->fastTypes as $key => $value) { + $this->output->write("[$key] " . $value['const'] . " ({$value['value']}" . 'h)'); + } + } + + protected function setFastEndDate() + { + $hours = 'PT' . $this->newFast->type . 'H'; + $fastEndDate = $this->newFast + ->start + ->add(new \DateInterval("$hours")) + ->format('Y-m-d H:i:s'); + $this->newFast->set([ + 'end' => $fastEndDate + ]); + + + } + + protected function saveFast() + { + $storeFasts = $this->store->getAll()->toArray(); + + $storeFasts[] = $this->newFast; + + $this->store->write($storeFasts); + } } \ No newline at end of file From 4481a4073bf8e01227086079a0952f08ec7664e8 Mon Sep 17 00:00:00 2001 From: Denis Ziberi Date: Wed, 17 Nov 2021 14:30:20 +0100 Subject: [PATCH 08/24] listFasts changed Fast print function added --- App/Commands/ListCommand.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/App/Commands/ListCommand.php b/App/Commands/ListCommand.php index 66bde72..196aa2e 100644 --- a/App/Commands/ListCommand.php +++ b/App/Commands/ListCommand.php @@ -29,7 +29,7 @@ public function run() private function listFasts(Collection $fasts) { foreach ($fasts as $key => $fast) { - $this->output->write("fast number:$key $fast"); + $this->output->write("fast number:$key {$fast->print()}"); } } From 4fb396cb80a871f8bab25bf8d19520e2ba22e427 Mon Sep 17 00:00:00 2001 From: Denis Ziberi Date: Wed, 17 Nov 2021 14:31:14 +0100 Subject: [PATCH 09/24] FileManagerInterface write method accepts array --- App/Interface/FileManagerInterface.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/App/Interface/FileManagerInterface.php b/App/Interface/FileManagerInterface.php index 93694a4..1e5097d 100644 --- a/App/Interface/FileManagerInterface.php +++ b/App/Interface/FileManagerInterface.php @@ -8,5 +8,5 @@ public function getAll(); public function select($key); - public function write(object $fast); + public function write(array $fasts); } \ No newline at end of file From 2c62839ddf52a6684fdcd31dffb98c7219273e12 Mon Sep 17 00:00:00 2001 From: Denis Ziberi Date: Wed, 17 Nov 2021 14:32:19 +0100 Subject: [PATCH 10/24] Collection class added a toArray method --- App/Model/Collection.php | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/App/Model/Collection.php b/App/Model/Collection.php index 9c36207..1a69f71 100644 --- a/App/Model/Collection.php +++ b/App/Model/Collection.php @@ -52,4 +52,9 @@ public function each(callable $callback) } return $this; } + + public function toArray() + { + return $this->items; + } } \ No newline at end of file From 6609a2c96770a1101eb64207c0318a845511c262 Mon Sep 17 00:00:00 2001 From: Denis Ziberi Date: Wed, 17 Nov 2021 14:44:26 +0100 Subject: [PATCH 11/24] Fast model added jsonSerialize method and print method --- App/Model/Fast.php | 27 ++++++++++++++++++++++----- 1 file changed, 22 insertions(+), 5 deletions(-) diff --git a/App/Model/Fast.php b/App/Model/Fast.php index b36f3ec..c0dddb3 100644 --- a/App/Model/Fast.php +++ b/App/Model/Fast.php @@ -7,9 +7,10 @@ use App\Interface\FastModelInterface; use DateTime; use Exception; +use JsonSerializable; -class Fast implements FastModelInterface +class Fast implements FastModelInterface, JsonSerializable { protected array $dates = [ 'start', @@ -27,10 +28,10 @@ public function __construct( /** * @param $parameter - * @return DateTime|mixed + * @return DateTime|int|string * @throws Exception */ - public function __get($parameter) + public function __get($parameter): DateTime|int|string { if (in_array($parameter, $this->dates)) { return new DateTime($this->{$parameter}); @@ -46,7 +47,11 @@ public function set(array $params) } } - public function __toString() + + /** + * @return string + */ + public function print(): string { $type = FastType::fromValue($this->type); $status = Status::fromValue($this->status); @@ -55,7 +60,8 @@ public function __toString() return " -------------------------------------- Status ($status) \n\r - Started Fasting $this->start \n\r + Started Fasting $this->start \n\r + End date $this->end \n\r Fast Type $type({$this->type}h) \n\r Elapsed Time $this->elapsedTime; -------------------------------------- @@ -66,6 +72,17 @@ public function setElapsedTime($time) { $this->elapsedTime = $time; } + + public function jsonSerialize() + { + return [ + "status" => $this->status, + "start" => $this->start, + "end" => $this->end, + "type" => $this->type, + "elapsed_time" => $this->elapsedTime + ]; + } } From 32cbb08c6d7eb2094f6eec039c9def43fda9d183 Mon Sep 17 00:00:00 2001 From: Denis Ziberi Date: Wed, 17 Nov 2021 14:45:43 +0100 Subject: [PATCH 12/24] getAll method updated if store is empty return empty collection write method implemented --- App/Store/StoreManager.php | 18 ++++++------------ 1 file changed, 6 insertions(+), 12 deletions(-) diff --git a/App/Store/StoreManager.php b/App/Store/StoreManager.php index 8354f8d..ee18209 100644 --- a/App/Store/StoreManager.php +++ b/App/Store/StoreManager.php @@ -16,7 +16,7 @@ class StoreManager implements FileManagerInterface /** - * @return Collection + * @return void */ public function getAll(): Collection { @@ -25,9 +25,10 @@ public function getAll(): Collection $storeFasts = json_decode( file_get_contents($this->file) , false); - + if (!$storeFasts) { + return new Collection([]); + } foreach ($storeFasts as $fast) { - $newFast = new Fast( start: $fast->start, status: $fast->status, @@ -43,13 +44,6 @@ public function getAll(): Collection $newFast->setElapsedTime($fast->elapsed_time); } $fastArray[] = $newFast; - -// $fastArray[] = new Fast( -// start: $fast->start, -// status: $fast->status, -// end: $fast->end, -// type: $fast->type, -// elapsedTime: $fast->elapsed_time); } return new Collection($fastArray); } @@ -59,8 +53,8 @@ public function select($key) // TODO: Implement select() method. } - public function write(object $fast) + public function write($fasts) { - // TODO: Implement write() method. + file_put_contents($this->file, json_encode($fasts)); } } \ No newline at end of file From eacd34dbf31ed7341de3108b5532adf3ed0d3c8c Mon Sep 17 00:00:00 2001 From: Denis Ziberi Date: Wed, 17 Nov 2021 14:46:56 +0100 Subject: [PATCH 13/24] updated files --- index.php | 7 ++----- store.example.json | 9 +++++++++ store.json | 37 +++++++++++++++++++++++++++++-------- 3 files changed, 40 insertions(+), 13 deletions(-) create mode 100644 store.example.json diff --git a/index.php b/index.php index 0995805..3515abe 100644 --- a/index.php +++ b/index.php @@ -8,14 +8,11 @@ require_once './vendor/autoload.php'; -$obj = new Fast('2020-10-10 10:00:00', 0, '2020-10-11 10:00:00', 12, ''); - - - $commands = new CommandController( new InputConsole(), new OutputConsole(), - new StoreManager()); + new StoreManager() +); $commands->run(); diff --git a/store.example.json b/store.example.json new file mode 100644 index 0000000..ebc1367 --- /dev/null +++ b/store.example.json @@ -0,0 +1,9 @@ +[ + { + "status": 0, + "start": "2021-11-22 10:00:00", + "end": "2021-11-22 10:00:00", + "type": 16, + "elapsed_time": "0 years 0 months 0 days 0 hours 0 minutes 0 seconds" + } +] \ No newline at end of file diff --git a/store.json b/store.json index 7dc994b..63d5382 100644 --- a/store.json +++ b/store.json @@ -1,16 +1,37 @@ [ { "status": 0, - "start": "2021-10-05 12:12:00", - "end": "2022-12-05 12:12:00", - "elapsed_time": "00 years 1 months 11 days 06 h 41 min 47 sec", - "type": 20 + "start": "2021-11-22 10:00:00", + "end": "2021-11-22 10:00:00", + "type": 16, + "elapsed_time": "00 years 0 months 4 days 19 h 54 min 37 sec" }, { "status": 0, - "start": "2021-10-05 12:12:00", - "end": "2021-10-05 12:12:00", - "elapsed_time": "00 years 1 months 11 days 06 h 41 min 47 sec", - "type": 12 + "start": "2021-11-22 10:00:00", + "end": "2021-11-22 10:00:00", + "type": 16, + "elapsed_time": "00 years 0 months 4 days 19 h 54 min 37 sec" + }, + { + "status": 0, + "start": "2021-11-22 10:00:00", + "end": "2021-11-22 10:00:00", + "type": 16, + "elapsed_time": "00 years 0 months 4 days 19 h 54 min 37 sec" + }, + { + "status": 0, + "start": "2021-11-18 20:00:00", + "end": "2021-11-19 16:00:00", + "type": 20, + "elapsed_time": "00 years 0 months 1 days 05 h 54 min 37 sec" + }, + { + "status": 0, + "start": "2021-11-18 10:00:00", + "end": "2021-11-19 06:00:00", + "type": 20, + "elapsed_time": "" } ] \ No newline at end of file From 24db1a3c9e46f7392c61973f4b807c220146fc1f Mon Sep 17 00:00:00 2001 From: Denis Ziberi Date: Wed, 17 Nov 2021 17:07:27 +0100 Subject: [PATCH 14/24] small functionality changes on printing menu --- App/Commands/CommandController.php | 44 ++++++++---------------------- App/Commands/CreateCommand.php | 2 -- App/Commands/ExitCommand.php | 24 ++++++++++++++++ App/Model/Collection.php | 4 +-- App/Model/Fast.php | 2 +- App/Store/StoreManager.php | 20 +++++++++++++- index.php | 3 +- store.json | 22 ++++++++++++--- 8 files changed, 78 insertions(+), 43 deletions(-) create mode 100644 App/Commands/ExitCommand.php diff --git a/App/Commands/CommandController.php b/App/Commands/CommandController.php index 8569490..cba9fdc 100644 --- a/App/Commands/CommandController.php +++ b/App/Commands/CommandController.php @@ -28,11 +28,6 @@ class CommandController implements BaseCommandInterface 'command' => CreateCommand::class, 'condition' => 'canCreate' ], - [ - 'option' => 'Exit', - 'command' => "", - 'condition' => 'noCondition' - ], [ 'option' => 'Update an active fast', 'command' => UpdateCommand::class, @@ -44,6 +39,11 @@ class CommandController implements BaseCommandInterface 'command' => ListCommand::class, 'condition' => 'noCondition' ], + [ + 'option' => 'Exit', + 'command' => ExitCommand::class, + 'condition' => 'noCondition' + ], ]; @@ -64,13 +64,13 @@ public function run() $this->updateCanCreate(); $this->printMenu(); $input = $this->input->getInput(); - if (key_exists($input, $this->menu)) { - $command = new $this->menu[$input]['command']($this->input, $this->output, $this->store); - - $command->run(); + if (!key_exists($input, $this->menu)) { + $this->run(); } + $command = new $this->menu[$input]['command']($this->input, $this->output, $this->store); + + $command->run(); - $this->run(); } } @@ -82,18 +82,11 @@ public function exit() public function printMenu() { - $counter = 0; foreach ($this->menu as $key => $bundle) { - $condition = $bundle['condition']; - if ($this->{$condition}) { - $this->output->write("[" . $key . "] " . $bundle['option']); - - } - } } @@ -116,24 +109,11 @@ private function updateActiveFast() private function updateCanCreate() { - /** - * @var $fasts Collection - */ - $fasts = $this->store->getAll(); - if (!$fasts) { + if (!$this->store->hasActiveFasts()) { $this->canCreate = true; return; } - $fasts->each(function ($key, $fast) { - if ($fast->status == Status::ACTIVE) { - - $this->canCreate = false; - return; - } - $this->canCreate = true; - - }); - + $this->canCreate = false; } diff --git a/App/Commands/CreateCommand.php b/App/Commands/CreateCommand.php index 47b3086..5df16fd 100644 --- a/App/Commands/CreateCommand.php +++ b/App/Commands/CreateCommand.php @@ -95,8 +95,6 @@ protected function setFastEndDate() $this->newFast->set([ 'end' => $fastEndDate ]); - - } protected function saveFast() diff --git a/App/Commands/ExitCommand.php b/App/Commands/ExitCommand.php new file mode 100644 index 0000000..a05cd39 --- /dev/null +++ b/App/Commands/ExitCommand.php @@ -0,0 +1,24 @@ + $value) { - if ($callback($key, $value) === null) { - return; + if ($callback($key, $value) === false) { + return false; } } return $this; diff --git a/App/Model/Fast.php b/App/Model/Fast.php index c0dddb3..0f2c506 100644 --- a/App/Model/Fast.php +++ b/App/Model/Fast.php @@ -19,7 +19,7 @@ class Fast implements FastModelInterface, JsonSerializable public function __construct( protected string $start = '', - protected int $status = 0, + protected int $status = 1, protected string $end = '', protected int $type = 0, protected string $elapsedTime = '') diff --git a/App/Store/StoreManager.php b/App/Store/StoreManager.php index ee18209..cd1f154 100644 --- a/App/Store/StoreManager.php +++ b/App/Store/StoreManager.php @@ -35,7 +35,7 @@ public function getAll(): Collection end: $fast->end, type: $fast->type ); - if ($fast->status !== Status::ACTIVE) { + if ($fast->status === Status::ACTIVE && $newFast->start < $today) { $newFast->setElapsedTime($today ->diff($newFast->start) ->format('%Y years %m months %d days %H h %i min %s sec')); @@ -48,6 +48,24 @@ public function getAll(): Collection return new Collection($fastArray); } + public function hasActiveFasts(): bool + { + $hasActive = false; + $fasts = $this->getAll(); + + if (!$fasts->toArray()) { + $hasActive = false; + } + + $fasts->each(function ($key, $fast) use (&$hasActive) { + if ($fast->status == Status::ACTIVE) { + $hasActive = true; + } + }); + + return $hasActive; + } + public function select($key) { // TODO: Implement select() method. diff --git a/index.php b/index.php index 3515abe..330f736 100644 --- a/index.php +++ b/index.php @@ -1,3 +1,5 @@ + + run(); diff --git a/store.json b/store.json index 63d5382..ef3ead4 100644 --- a/store.json +++ b/store.json @@ -4,34 +4,48 @@ "start": "2021-11-22 10:00:00", "end": "2021-11-22 10:00:00", "type": 16, - "elapsed_time": "00 years 0 months 4 days 19 h 54 min 37 sec" + "elapsed_time": "00 years 0 months 4 days 18 h 8 min 21 sec" }, { "status": 0, "start": "2021-11-22 10:00:00", "end": "2021-11-22 10:00:00", "type": 16, - "elapsed_time": "00 years 0 months 4 days 19 h 54 min 37 sec" + "elapsed_time": "00 years 0 months 4 days 18 h 8 min 21 sec" }, { "status": 0, "start": "2021-11-22 10:00:00", "end": "2021-11-22 10:00:00", "type": 16, - "elapsed_time": "00 years 0 months 4 days 19 h 54 min 37 sec" + "elapsed_time": "00 years 0 months 4 days 18 h 8 min 21 sec" }, { "status": 0, "start": "2021-11-18 20:00:00", "end": "2021-11-19 16:00:00", "type": 20, - "elapsed_time": "00 years 0 months 1 days 05 h 54 min 37 sec" + "elapsed_time": "00 years 0 months 1 days 04 h 8 min 21 sec" }, { "status": 0, "start": "2021-11-18 10:00:00", "end": "2021-11-19 06:00:00", "type": 20, + "elapsed_time": "00 years 0 months 0 days 18 h 8 min 21 sec" + }, + { + "status": 0, + "start": "2020-01-1", + "end": "2020-01-01 12:00:00", + "type": 12, + "elapsed_time": "" + }, + { + "status": 1, + "start": "2021-11-18 10:00:00", + "end": "2021-11-18 22:00:00", + "type": 12, "elapsed_time": "" } ] \ No newline at end of file From 0056a766b1fdcbe80787c7007d8bd472ea9ecab9 Mon Sep 17 00:00:00 2001 From: Denis Ziberi Date: Thu, 18 Nov 2021 17:42:09 +0100 Subject: [PATCH 15/24] too much to commit --- .gitignore | 2 + App/Commands/CheckCommand.php | 19 +++++ App/Commands/CommandController.php | 51 ++++++----- App/Commands/CreateCommand.php | 96 +-------------------- App/Commands/EndCommand.php | 52 ++++++++++++ App/Commands/UpdateCommand.php | 16 ++-- App/Console/InputConsole.php | 14 +--- App/Console/InputValidator.php | 9 +- App/Console/OutputConsole.php | 6 +- App/Enums/BasicEnum.php | 11 ++- App/Interface/BaseCommandInterface.php | 8 +- App/Interface/FastModelInterface.php | 7 ++ App/Interface/FileManagerInterface.php | 2 - App/Model/Fast.php | 24 ++++-- App/Model/FastEditor.php | 112 +++++++++++++++++++++++++ App/Store/StoreManager.php | 51 +++++++++-- README.md | 1 + index.php => fast | 3 +- store.json | 52 +----------- 19 files changed, 319 insertions(+), 217 deletions(-) create mode 100644 App/Commands/CheckCommand.php create mode 100644 App/Commands/EndCommand.php create mode 100644 App/Model/FastEditor.php rename index.php => fast (94%) diff --git a/.gitignore b/.gitignore index c8153b5..b67f666 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,4 @@ /composer.lock /vendor/ +.idea + diff --git a/App/Commands/CheckCommand.php b/App/Commands/CheckCommand.php new file mode 100644 index 0000000..b4425ac --- /dev/null +++ b/App/Commands/CheckCommand.php @@ -0,0 +1,19 @@ +store->getActiveFast()) { + $this->output->write('You have no active fast please create one.'); + return; + } + $this->output->write($activeFast->print()); + } +} \ No newline at end of file diff --git a/App/Commands/CommandController.php b/App/Commands/CommandController.php index cba9fdc..6c96384 100644 --- a/App/Commands/CommandController.php +++ b/App/Commands/CommandController.php @@ -21,8 +21,17 @@ class CommandController implements BaseCommandInterface protected bool $noCondition = true; + /** + * @var array|string[][] + */ + private array $menu = [ + [ + 'option' => 'Check the fast status', + 'command' => CheckCommand::class, + 'condition' => 'noCondition' + ], [ 'option' => 'Create Fast', 'command' => CreateCommand::class, @@ -32,7 +41,13 @@ class CommandController implements BaseCommandInterface 'option' => 'Update an active fast', 'command' => UpdateCommand::class, 'condition' => 'activeFasts', - 'operand' => '> 0' + + ], + [ + 'option' => 'End an active fast', + 'command' => EndCommand::class, + 'condition' => 'activeFasts', + ], [ 'option' => 'List all fasts', @@ -58,26 +73,16 @@ public function __construct( public function run() { - while ($this->appRunning) { $this->updateActiveFast(); $this->updateCanCreate(); $this->printMenu(); $input = $this->input->getInput(); - if (!key_exists($input, $this->menu)) { - $this->run(); + if (key_exists($input, $this->menu)) { + $command = new $this->menu[$input]['command']($this->input, $this->output, $this->store); + $command->run(); } - $command = new $this->menu[$input]['command']($this->input, $this->output, $this->store); - - $command->run(); - } - - } - - public function exit() - { - $this->appRunning = false; } public function printMenu() @@ -92,19 +97,11 @@ public function printMenu() private function updateActiveFast() { - /** - * @var $fasts Collection - */ - $fasts = $this->store->getAll(); - $fasts->each(callback: function ($key, $fast) { - - if ($fast->status == Status::ACTIVE) { - - $this->activeFasts = true; - - } - }); - + if ($this->store->hasActiveFasts()) { + $this->activeFasts = true; + return; + } + $this->activeFasts = false; } private function updateCanCreate() diff --git a/App/Commands/CreateCommand.php b/App/Commands/CreateCommand.php index 5df16fd..4d8c50e 100644 --- a/App/Commands/CreateCommand.php +++ b/App/Commands/CreateCommand.php @@ -2,34 +2,12 @@ namespace App\Commands; -use App\Console\InputConsole; -use App\Console\InputValidator; -use App\Console\OutputConsole; -use App\Enums\FastType; +use App\Model\FastEditor; use App\Interface\BaseCommandInterface; -use App\Model\Fast; -use App\Store\StoreManager; -use JetBrains\PhpStorm\Pure; -class CreateCommand implements BaseCommandInterface -{ - protected InputValidator $validator; - protected Fast $newFast; - protected array $fastTypes; - - #[Pure] public function __construct( - protected InputConsole $input, - protected OutputConsole $output, - protected StoreManager $store - ) - { - //TODO : Inject validator - $this->validator = new InputValidator(); - $this->newFast = new Fast(); - $this->setFastTypes(); - - } +class CreateCommand extends FastEditor implements BaseCommandInterface +{ public function run() { $this->getStartDate(); @@ -37,72 +15,4 @@ public function run() $this->setFastEndDate(); $this->saveFast(); } - - protected function getStartDate() - { - $this->output->write('Enter Start Date of Fast format:(Y-m-d H:i:s) => (2020-10-10 20:00:00)'); - $userInput = $this->input->getInput(); - - if ($message = $this->validator->validateStartdate($userInput)) { - $this->output->write($message); - $this->getStartDate(); - } - $this->newFast->set([ - 'start' => $userInput - ]); - } - - protected function getFastType() - { - $this->output->write('Select a fast type'); - $this->printFastTypes(); - $userInput = $this->input->getInput(); - if (!key_exists($userInput, $this->fastTypes)) { - $this->output->write('Please choose from existing types.'); - $this->getFastType(); - } - $this->newFast->set([ - 'type' => $this->fastTypes[$userInput]['value'] - ]); - return; - } - - protected function setFastTypes() - { - foreach (FastType::getAll() as $const => $value) { - $this->fastTypes[] = [ - 'const' => $const, - 'value' => $value - ]; - } - } - - protected function printFastTypes() - { - foreach ($this->fastTypes as $key => $value) { - $this->output->write("[$key] " . $value['const'] . " ({$value['value']}" . 'h)'); - } - } - - protected function setFastEndDate() - { - $hours = 'PT' . $this->newFast->type . 'H'; - $fastEndDate = $this->newFast - ->start - ->add(new \DateInterval("$hours")) - ->format('Y-m-d H:i:s'); - - $this->newFast->set([ - 'end' => $fastEndDate - ]); - } - - protected function saveFast() - { - $storeFasts = $this->store->getAll()->toArray(); - - $storeFasts[] = $this->newFast; - - $this->store->write($storeFasts); - } } \ No newline at end of file diff --git a/App/Commands/EndCommand.php b/App/Commands/EndCommand.php new file mode 100644 index 0000000..1ef0d2a --- /dev/null +++ b/App/Commands/EndCommand.php @@ -0,0 +1,52 @@ + true, + "N" => false + ]; + + public function run() + { + $today = new DateTime('NOW'); + if ($activeFast = $this->store->getActiveFast()) { + $this->output->write('Are you sure tou want to end current active fast.'); + $this->printMenu(); + $userInput = strtoupper($this->input->getInput()); + while (!key_exists($userInput, $this->menu)) { + $userInput = $this->input->getInput(); + } + if (!$userInput) return; + $this->store->deleteActiveFast(); + $activeFast->set([ + 'status' => Status::INACTIVE, + ]); + if ($activeFast->start < $today) { + $activeFast->set([ + 'elapsedTime' => $today->diff($activeFast->start) + ->format('%Y years %m months %d days %H h %i min %s sec') + ]); + } + $this->save($activeFast); + } + } + + protected function printMenu() + { + foreach ($this->menu as $key => $value) { + $this->output->write("[$key]"); + } + } + + + +} \ No newline at end of file diff --git a/App/Commands/UpdateCommand.php b/App/Commands/UpdateCommand.php index fcaafb5..30f0a70 100644 --- a/App/Commands/UpdateCommand.php +++ b/App/Commands/UpdateCommand.php @@ -5,17 +5,21 @@ use App\Console\InputConsole; use App\Console\OutputConsole; use App\Interface\BaseCommandInterface; +use App\Model\FastEditor; use App\Store\StoreManager; -class UpdateCommand implements BaseCommandInterface +class UpdateCommand extends FastEditor implements BaseCommandInterface { - public function __construct(InputConsole $input, OutputConsole $output, StoreManager $store) - { - } - public function run() { - // TODO: Implement run() method. + if ($activeFast = $this->store->getActiveFast()) { + $this->output->write("This is your active fast {$activeFast->print()} Please reset start date and fast type:"); + $this->getStartDate(); + $this->getFastType(); + $this->setFastEndDate(); + $this->store->deleteActiveFast(); + $this->saveFast(); + } } } \ No newline at end of file diff --git a/App/Console/InputConsole.php b/App/Console/InputConsole.php index 7b78ffc..2b4f562 100644 --- a/App/Console/InputConsole.php +++ b/App/Console/InputConsole.php @@ -6,17 +6,11 @@ class InputConsole { - + /** + * @return string + */ public function getInput() { - - //TODO:refactor - - $stdin = fopen('php://stdin', 'r'); - - - return trim(fgets($stdin), "\n\r"); + return trim(fgets(STDIN)); } - - } \ No newline at end of file diff --git a/App/Console/InputValidator.php b/App/Console/InputValidator.php index fbf8c86..9b26f55 100644 --- a/App/Console/InputValidator.php +++ b/App/Console/InputValidator.php @@ -8,21 +8,20 @@ class InputValidator { + /** + * @param $input + * @return false|string + */ public function validateStartdate($input) { - $dateInputObject = DateTime::createFromFormat("Y-m-d H:i:s", $input); $today = new DateTime('NOW'); - if (!$dateInputObject || ($dateInputObject->format("Y-m-d H:i:s") != $input)) { return "Please enter valid format date."; } else if ($dateInputObject < $today) { return 'Please set starting date greater than today.'; } - return false; - } - } \ No newline at end of file diff --git a/App/Console/OutputConsole.php b/App/Console/OutputConsole.php index 9f93014..aa8c764 100644 --- a/App/Console/OutputConsole.php +++ b/App/Console/OutputConsole.php @@ -8,10 +8,14 @@ class OutputConsole { + /** + * Prints string into the console + * @param $text + */ public function write($text) { - echo $text . "\n\r"; } + } \ No newline at end of file diff --git a/App/Enums/BasicEnum.php b/App/Enums/BasicEnum.php index d24ab24..5aa7925 100644 --- a/App/Enums/BasicEnum.php +++ b/App/Enums/BasicEnum.php @@ -8,6 +8,10 @@ abstract class BasicEnum { private static array|null $constCacheArray = NULL; + /** + * @return array|mixed + * @throws \ReflectionException + */ private static function getConstants() { if (self::$constCacheArray == NULL) { @@ -23,7 +27,8 @@ private static function getConstants() /** * @param mixed $value - * @return false|int|string + * @return bool|int|string + * @throws \ReflectionException */ public static function fromValue(mixed $value): bool|int|string { @@ -42,6 +47,10 @@ public static function isValidType(string $value): bool return array_key_exists($value, $constants); } + /** + * @return array|mixed + * @throws \ReflectionException + */ public static function getAll() { return self::getConstants(); diff --git a/App/Interface/BaseCommandInterface.php b/App/Interface/BaseCommandInterface.php index 17ced6f..a926fcc 100644 --- a/App/Interface/BaseCommandInterface.php +++ b/App/Interface/BaseCommandInterface.php @@ -9,8 +9,12 @@ interface BaseCommandInterface { - - public function __construct(InputConsole $input, OutputConsole $output , StoreManager $store); + /** + * @param InputConsole $input + * @param OutputConsole $output + * @param StoreManager $store + */ + public function __construct(InputConsole $input, OutputConsole $output, StoreManager $store); public function run(); diff --git a/App/Interface/FastModelInterface.php b/App/Interface/FastModelInterface.php index d99c8b1..59a4385 100644 --- a/App/Interface/FastModelInterface.php +++ b/App/Interface/FastModelInterface.php @@ -5,6 +5,13 @@ interface FastModelInterface { + /** + * @param string $start + * @param int $status + * @param string $end + * @param int $type + * @param string $elapsedTime + */ public function __construct( string $start, diff --git a/App/Interface/FileManagerInterface.php b/App/Interface/FileManagerInterface.php index 1e5097d..265e1d6 100644 --- a/App/Interface/FileManagerInterface.php +++ b/App/Interface/FileManagerInterface.php @@ -5,8 +5,6 @@ interface FileManagerInterface { public function getAll(); - public function select($key); - public function write(array $fasts); } \ No newline at end of file diff --git a/App/Model/Fast.php b/App/Model/Fast.php index 0f2c506..2935829 100644 --- a/App/Model/Fast.php +++ b/App/Model/Fast.php @@ -7,6 +7,7 @@ use App\Interface\FastModelInterface; use DateTime; use Exception; +use JetBrains\PhpStorm\ArrayShape; use JsonSerializable; @@ -58,22 +59,29 @@ public function print(): string return " - -------------------------------------- - Status ($status) \n\r - Started Fasting $this->start \n\r - End date $this->end \n\r - Fast Type $type({$this->type}h) \n\r - Elapsed Time $this->elapsedTime; - -------------------------------------- +-------------------------------------- +Status ($status) \n\r +Started Fasting $this->start \n\r +End date $this->end \n\r +Fast Type $type({$this->type}h) \n\r +Elapsed Time $this->elapsedTime; \n\r +-------------------------------------- "; } + /** + * @param $time + */ public function setElapsedTime($time) { $this->elapsedTime = $time; } - public function jsonSerialize() + /** + * @return array + */ + #[ArrayShape(["status" => "int", "start" => "string", "end" => "string", "type" => "int", "elapsed_time" => "string"])] + public function jsonSerialize(): array { return [ "status" => $this->status, diff --git a/App/Model/FastEditor.php b/App/Model/FastEditor.php new file mode 100644 index 0000000..1b32ab4 --- /dev/null +++ b/App/Model/FastEditor.php @@ -0,0 +1,112 @@ +validator = new InputValidator(); + $this->newFast = new Fast(); + $this->setFastTypes(); + + } + + protected function getStartDate() + { + $this->output->write('Enter Start Date of Fast format:(Y-m-d H:i:s) => (2020-10-10 20:00:00)'); + $userInput = $this->input->getInput(); + + while ($message = $this->validator->validateStartdate($userInput)) { + $this->output->write($message); + $userInput = $this->input->getInput(); + } + $this->newFast->set([ + 'start' => $userInput + ]); + } + + protected function getFastType() + { + $this->output->write('Select a fast type'); + $this->printFastTypes(); + $userInput = $this->input->getInput(); + + while (!key_exists($userInput, $this->fastTypes)) { + $this->output->write('Please choose from existing types.'); + + $userInput = $this->input->getInput(); + } + $this->newFast->set([ + 'type' => $this->fastTypes[$userInput]['value'] + ]); + } + + protected function setFastTypes() + { + foreach (FastType::getAll() as $const => $value) { + $this->fastTypes[] = [ + 'const' => $const, + 'value' => $value + ]; + } + } + + protected function printFastTypes() + { + foreach ($this->fastTypes as $key => $value) { + $this->output->write("[$key] " . $value['const'] . " ({$value['value']}" . 'h)'); + } + } + + protected function setFastEndDate() + { + $hours = 'PT' . $this->newFast->type . 'H'; + $fastEndDate = $this->newFast + ->start + ->add(new \DateInterval("$hours")) + ->format('Y-m-d H:i:s'); + + $this->newFast->set([ + 'end' => $fastEndDate + ]); + } + + + protected function saveFast() + { + $storeFasts = $this->store->getAll()->toArray(); + + $storeFasts[] = $this->newFast; + + $this->store->write($storeFasts); + } + + /** + * @param Fast $fast + */ + protected function save(Fast $fast) + { + $storeFasts = $this->store->getAll()->toArray(); + + $storeFasts[] = $fast; + + $this->store->write($storeFasts); + } +} \ No newline at end of file diff --git a/App/Store/StoreManager.php b/App/Store/StoreManager.php index cd1f154..3be5f1e 100644 --- a/App/Store/StoreManager.php +++ b/App/Store/StoreManager.php @@ -2,10 +2,7 @@ namespace App\Store; -use App\Enums\Status; -use App\Interface\FileManagerInterface; -use App\Model\Collection; -use App\Model\Fast; +use App\{Enums\Status, Interface\FileManagerInterface, Model\Collection, Model\Fast}; use DateTime; @@ -14,9 +11,8 @@ class StoreManager implements FileManagerInterface protected string $file = "./store.json"; - /** - * @return void + * @return Collection */ public function getAll(): Collection { @@ -48,31 +44,68 @@ public function getAll(): Collection return new Collection($fastArray); } + /** + * @return bool + */ public function hasActiveFasts(): bool { $hasActive = false; $fasts = $this->getAll(); if (!$fasts->toArray()) { - $hasActive = false; + return $hasActive; } - $fasts->each(function ($key, $fast) use (&$hasActive) { if ($fast->status == Status::ACTIVE) { $hasActive = true; } }); - return $hasActive; } + /** + * @param $key + */ public function select($key) { // TODO: Implement select() method. } + /** + * @return false|Fast + */ + public function getActiveFast(): bool|Fast + { + if (!$this->hasActiveFasts()) return false; + + $fasts = $this->getAll(); + $activeFast = false; + + $fasts->each(function ($key, $fast) use (&$activeFast) { + if ($fast->status == Status::ACTIVE) { + $activeFast = $fast; + } + }); + return $activeFast; + } + + public function write($fasts) { file_put_contents($this->file, json_encode($fasts)); } + + public function deleteActiveFast() + { + $fasts = $this->getAll(); + $fastsWithoutActiveFasts = []; + + $fasts->each(function ($key, $fast) use (&$fastsWithoutActiveFasts) { + if ($fast->status == Status::INACTIVE) { + $fastsWithoutActiveFasts[] = $fast; + } + }); + + $this->write($fastsWithoutActiveFasts); + } } \ No newline at end of file diff --git a/README.md b/README.md index 03bb225..f7c2473 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,3 @@ # Zero-consoleApp + PHP console app based on Zero diff --git a/index.php b/fast similarity index 94% rename from index.php rename to fast index 330f736..bc3a183 100644 --- a/index.php +++ b/fast @@ -1,5 +1,4 @@ - - +#!/usr/bin/env php Date: Fri, 19 Nov 2021 15:28:44 +0100 Subject: [PATCH 16/24] final commit before merge to master --- .../BaseCommandController.php} | 75 ++++++++++++++----- App/Commands/CheckCommand.php | 7 +- App/Commands/CommandController.php | 58 ++++++++------ App/Commands/CreateCommand.php | 3 +- App/Commands/EndCommand.php | 28 ++----- App/Commands/ExitCommand.php | 26 +++++-- App/Commands/ListCommand.php | 30 +++++--- App/Commands/UpdateCommand.php | 8 +- App/Console/InputConsole.php | 3 +- App/Console/InputValidator.php | 5 +- App/Console/OutputConsole.php | 30 +++++++- App/Enums/BasicEnum.php | 20 +++-- App/Enums/Status.php | 2 - App/Interface/BaseCommandInterface.php | 12 ++- App/Model/Collection.php | 9 +++ App/Model/Fast.php | 35 ++++----- App/Store/StoreManager.php | 28 +++++-- fast | 6 +- store.json | 2 +- 19 files changed, 255 insertions(+), 132 deletions(-) rename App/{Model/FastEditor.php => Commands/BaseCommandController.php} (51%) diff --git a/App/Model/FastEditor.php b/App/Commands/BaseCommandController.php similarity index 51% rename from App/Model/FastEditor.php rename to App/Commands/BaseCommandController.php index 1b32ab4..5d5b442 100644 --- a/App/Model/FastEditor.php +++ b/App/Commands/BaseCommandController.php @@ -1,40 +1,46 @@ true, + "N" => false + ]; public function __construct( - protected InputConsole $input, - protected OutputConsole $output, - protected StoreManager $store + protected InputConsole $input, + protected OutputConsole $output, + protected StoreManager $store, + protected InputValidator $validator, + protected Fast $newFast ) { - //TODO : Inject validator - $this->validator = new InputValidator(); - $this->newFast = new Fast(); $this->setFastTypes(); - } + /** + * Sets fast starting date from user input. + * @throws Exception + */ protected function getStartDate() { - $this->output->write('Enter Start Date of Fast format:(Y-m-d H:i:s) => (2020-10-10 20:00:00)'); + $this->output->writeYellow('Enter Start Date of Fast format:(Y-m-d H:i:s) => (2020-10-10 20:00:00)'); $userInput = $this->input->getInput(); - while ($message = $this->validator->validateStartdate($userInput)) { - $this->output->write($message); + while ($message = $this->validator->validateStartDate($userInput)) { + $this->output->writeError($message); $userInput = $this->input->getInput(); } $this->newFast->set([ @@ -42,14 +48,18 @@ protected function getStartDate() ]); } + /** + * Prints fast types in console and sets fast type from user input. + * @throws Exception + */ protected function getFastType() { - $this->output->write('Select a fast type'); + $this->output->writeYellow('Select a fast type'); $this->printFastTypes(); $userInput = $this->input->getInput(); while (!key_exists($userInput, $this->fastTypes)) { - $this->output->write('Please choose from existing types.'); + $this->output->writeError('Please choose from existing types.'); $userInput = $this->input->getInput(); } @@ -58,6 +68,9 @@ protected function getFastType() ]); } + /** + * Sets $fastTypes property as array from FastType enum class + */ protected function setFastTypes() { foreach (FastType::getAll() as $const => $value) { @@ -68,13 +81,19 @@ protected function setFastTypes() } } + /** + * Prints Fast types from fastTypes property. + */ protected function printFastTypes() { foreach ($this->fastTypes as $key => $value) { - $this->output->write("[$key] " . $value['const'] . " ({$value['value']}" . 'h)'); + $this->output->writeYellow("[$key] " . $value['const'] . " ({$value['value']}" . 'h)'); } } + /** + * Sets the newly created fast end Date + */ protected function setFastEndDate() { $hours = 'PT' . $this->newFast->type . 'H'; @@ -88,7 +107,9 @@ protected function setFastEndDate() ]); } - + /** + * Saves the newly created fast into the store file. + */ protected function saveFast() { $storeFasts = $this->store->getAll()->toArray(); @@ -99,14 +120,28 @@ protected function saveFast() } /** + * Saves a single Fast object passed as a parameter in the store file. * @param Fast $fast + * @throws Exception */ protected function save(Fast $fast) { $storeFasts = $this->store->getAll()->toArray(); - $storeFasts[] = $fast; - $this->store->write($storeFasts); } + + protected function askForConfirmation(string $question): string + { + $this->output->writeYellow($question); + $this->printConfirmationMenu(); + return strtoupper($this->input->getInput()); + } + + private function printConfirmationMenu() + { + foreach ($this->confirmationOptions as $key => $value) { + $this->output->writeYellow("[$key]"); + } + } } \ No newline at end of file diff --git a/App/Commands/CheckCommand.php b/App/Commands/CheckCommand.php index b4425ac..7258260 100644 --- a/App/Commands/CheckCommand.php +++ b/App/Commands/CheckCommand.php @@ -3,17 +3,16 @@ namespace App\Commands; use App\Interface\BaseCommandInterface; -use App\Model\FastEditor; -class CheckCommand extends FastEditor implements BaseCommandInterface +class CheckCommand extends BaseCommandController implements BaseCommandInterface { public function run() { if (!$activeFast = $this->store->getActiveFast()) { - $this->output->write('You have no active fast please create one.'); + $this->output->writeError('You have no active fast please create one.'); return; } - $this->output->write($activeFast->print()); + $this->output->writeGreen($activeFast->print()); } } \ No newline at end of file diff --git a/App/Commands/CommandController.php b/App/Commands/CommandController.php index 6c96384..d8f2346 100644 --- a/App/Commands/CommandController.php +++ b/App/Commands/CommandController.php @@ -2,12 +2,9 @@ namespace App\Commands; -use App\Console\InputConsole; -use App\Console\OutputConsole; -use App\Enums\Status; -use App\Interface\BaseCommandInterface; -use App\Interface\FileManagerInterface; -use App\Model\Collection; +use App\Console\{InputConsole, InputValidator, OutputConsole}; +use App\Interface\{BaseCommandInterface , FileManagerInterface}; +use App\Model\Fast; class CommandController implements BaseCommandInterface @@ -21,10 +18,7 @@ class CommandController implements BaseCommandInterface protected bool $noCondition = true; - /** - * @var array|string[][] - */ - + protected array $availableCommands = []; private array $menu = [ [ @@ -65,7 +59,9 @@ class CommandController implements BaseCommandInterface public function __construct( protected InputConsole $input, protected OutputConsole $output, - protected FileManagerInterface $store + protected FileManagerInterface $store, + protected InputValidator $validator, + protected Fast $newFast, ) { } @@ -74,28 +70,47 @@ public function __construct( public function run() { while ($this->appRunning) { - $this->updateActiveFast(); - $this->updateCanCreate(); - $this->printMenu(); + $this->updateActiveFastParameter(); + $this->updateCanCreateParameter(); + $this->adjustMenuAndPrint(); $input = $this->input->getInput(); if (key_exists($input, $this->menu)) { - $command = new $this->menu[$input]['command']($this->input, $this->output, $this->store); + /** + * @var $command BaseCommandInterface + */ + $command = new $this->availableCommands[$input]( + $this->input, + $this->output, + $this->store, + $this->validator, + $this->newFast, + ); $command->run(); + } else { + $this->output->writeYellow('Please select a specific command.'); } } } - public function printMenu() + public function adjustMenuAndPrint() { + $counter = 0; foreach ($this->menu as $key => $bundle) { + ++$counter; $condition = $bundle['condition']; if ($this->{$condition}) { - $this->output->write("[" . $key . "] " . $bundle['option']); + $this->output->write("[" . $counter . "] " . $bundle['option']); + $this->availableCommands[$counter] = $bundle['command']; + } else { + $counter--; } } } - private function updateActiveFast() + /** + * @throws \Exception + */ + private function updateActiveFastParameter() { if ($this->store->hasActiveFasts()) { $this->activeFasts = true; @@ -104,7 +119,10 @@ private function updateActiveFast() $this->activeFasts = false; } - private function updateCanCreate() + /** + * @throws \Exception + */ + private function updateCanCreateParameter() { if (!$this->store->hasActiveFasts()) { $this->canCreate = true; @@ -112,6 +130,4 @@ private function updateCanCreate() } $this->canCreate = false; } - - } \ No newline at end of file diff --git a/App/Commands/CreateCommand.php b/App/Commands/CreateCommand.php index 4d8c50e..30ef7d5 100644 --- a/App/Commands/CreateCommand.php +++ b/App/Commands/CreateCommand.php @@ -2,11 +2,10 @@ namespace App\Commands; -use App\Model\FastEditor; use App\Interface\BaseCommandInterface; -class CreateCommand extends FastEditor implements BaseCommandInterface +class CreateCommand extends BaseCommandController implements BaseCommandInterface { public function run() { diff --git a/App/Commands/EndCommand.php b/App/Commands/EndCommand.php index 1ef0d2a..649712e 100644 --- a/App/Commands/EndCommand.php +++ b/App/Commands/EndCommand.php @@ -4,28 +4,22 @@ use App\Enums\Status; use App\Interface\BaseCommandInterface; -use App\Model\Fast; -use App\Model\FastEditor; use DateTime; -class EndCommand extends FastEditor implements BaseCommandInterface +class EndCommand extends BaseCommandController implements BaseCommandInterface { - protected array $menu = [ - "Y" => true, - "N" => false - ]; + public function run() { $today = new DateTime('NOW'); if ($activeFast = $this->store->getActiveFast()) { - $this->output->write('Are you sure tou want to end current active fast.'); - $this->printMenu(); - $userInput = strtoupper($this->input->getInput()); - while (!key_exists($userInput, $this->menu)) { + $userInput = $this->askForConfirmation("Are you sure you want to end current active fast?"); + while (!key_exists($userInput, $this->confirmationOptions)) { $userInput = $this->input->getInput(); } - if (!$userInput) return; + if (!$this->confirmationOptions[$userInput]) return; + // Once we have confirmation delete the current active fast from file. $this->store->deleteActiveFast(); $activeFast->set([ 'status' => Status::INACTIVE, @@ -36,17 +30,9 @@ public function run() ->format('%Y years %m months %d days %H h %i min %s sec') ]); } + //Save the updated fast into file. $this->save($activeFast); } } - protected function printMenu() - { - foreach ($this->menu as $key => $value) { - $this->output->write("[$key]"); - } - } - - - } \ No newline at end of file diff --git a/App/Commands/ExitCommand.php b/App/Commands/ExitCommand.php index a05cd39..07a5848 100644 --- a/App/Commands/ExitCommand.php +++ b/App/Commands/ExitCommand.php @@ -2,23 +2,33 @@ namespace App\Commands; -use App\Console\InputConsole; -use App\Console\OutputConsole; -use App\Interface\BaseCommandInterface; -use App\Store\StoreManager; +use App\{Console\InputConsole, + Console\InputValidator, + Console\OutputConsole, + Interface\BaseCommandInterface, + Store\StoreManager, + Model\Fast +}; -class ExitCommand implements BaseCommandInterface + +class ExitCommand extends BaseCommandController implements BaseCommandInterface { public function __construct( - protected InputConsole $input, - protected OutputConsole $output, - protected StoreManager $store) + protected InputConsole $input, + protected OutputConsole $output, + protected StoreManager $store, + protected InputValidator $validator, + protected Fast $newFast + ) { } public function run() { + $userInput = $this->askForConfirmation("Are you sure you want to exit the app?"); + if (!$this->confirmationOptions[$userInput]) return; + $this->output->writeGreen('See you soon.. Goodbye....'); exit; } } \ No newline at end of file diff --git a/App/Commands/ListCommand.php b/App/Commands/ListCommand.php index 196aa2e..dd5fb37 100644 --- a/App/Commands/ListCommand.php +++ b/App/Commands/ListCommand.php @@ -2,19 +2,21 @@ namespace App\Commands; -use App\Console\InputConsole; -use App\Console\OutputConsole; +use App\Console\{InputConsole , InputValidator , OutputConsole}; +use App\Enums\Status; use App\Interface\BaseCommandInterface; -use App\Model\Collection; +use App\Model\{Collection , Fast}; use App\Store\StoreManager; class ListCommand implements BaseCommandInterface { public function __construct( - protected InputConsole $input, - protected OutputConsole $output, - protected StoreManager $store + protected InputConsole $input, + protected OutputConsole $output, + protected StoreManager $store, + protected InputValidator $validator, + protected Fast $newFast ) { } @@ -22,16 +24,20 @@ public function __construct( public function run() { $fasts = $this->store->getAll(); - $this->listFasts($fasts); } + /** + * Accepts a collection and prints out in console. + * @param Collection $fasts + */ private function listFasts(Collection $fasts) { - foreach ($fasts as $key => $fast) { - $this->output->write("fast number:$key {$fast->print()}"); - } + $fasts->each(function ($key, $fast) { + if ($fast->status !== Status::ACTIVE) { + $this->output->writeError("Fast number:$key {$fast->print()}"); + } + $this->output->writeGreen("Fast number:$key {$fast->print()}"); + }); } - - } \ No newline at end of file diff --git a/App/Commands/UpdateCommand.php b/App/Commands/UpdateCommand.php index 30f0a70..f129563 100644 --- a/App/Commands/UpdateCommand.php +++ b/App/Commands/UpdateCommand.php @@ -2,18 +2,16 @@ namespace App\Commands; -use App\Console\InputConsole; -use App\Console\OutputConsole; use App\Interface\BaseCommandInterface; -use App\Model\FastEditor; -use App\Store\StoreManager; -class UpdateCommand extends FastEditor implements BaseCommandInterface +class UpdateCommand extends BaseCommandController implements BaseCommandInterface { public function run() { if ($activeFast = $this->store->getActiveFast()) { + $userInput = $this->askForConfirmation("Are you sure you want to update your current active fast?"); + if (!$this->confirmationOptions[$userInput]) return; $this->output->write("This is your active fast {$activeFast->print()} Please reset start date and fast type:"); $this->getStartDate(); $this->getFastType(); diff --git a/App/Console/InputConsole.php b/App/Console/InputConsole.php index 2b4f562..a245295 100644 --- a/App/Console/InputConsole.php +++ b/App/Console/InputConsole.php @@ -7,9 +7,10 @@ class InputConsole { /** + * Returns trimmed input from console * @return string */ - public function getInput() + public function getInput(): string { return trim(fgets(STDIN)); } diff --git a/App/Console/InputValidator.php b/App/Console/InputValidator.php index 9b26f55..71750a4 100644 --- a/App/Console/InputValidator.php +++ b/App/Console/InputValidator.php @@ -9,10 +9,11 @@ class InputValidator { /** - * @param $input + * Validates passed string if it is in (Y-m-d H:i:s) date format + * @param string $input * @return false|string */ - public function validateStartdate($input) + public function validateStartDate(string $input): bool|string { $dateInputObject = DateTime::createFromFormat("Y-m-d H:i:s", $input); $today = new DateTime('NOW'); diff --git a/App/Console/OutputConsole.php b/App/Console/OutputConsole.php index aa8c764..ef2aa72 100644 --- a/App/Console/OutputConsole.php +++ b/App/Console/OutputConsole.php @@ -9,7 +9,8 @@ class OutputConsole { /** - * Prints string into the console + * + * Echos passed string into console as white text * @param $text */ public function write($text) @@ -17,5 +18,32 @@ public function write($text) echo $text . "\n\r"; } + /** + * Echos passed string into console as yellow text + * @param $text + */ + public function writeYellow($text) + { + echo "\e[33;1m" . $text . "\e[0m" . "\n\r"; + } + + /** + * Echos passed string into console as green text + * @param $text + */ + public function writeGreen($text) + { + echo "\e[32;1m" . $text . "\e[0m" . "\n\r"; + } + + /** + * Echos passed string into console with red background text + * @param $text + */ + public function writeError($text) + { + echo "\e[41;1m" . $text . "\e[0m" . "\n\r"; + } + } \ No newline at end of file diff --git a/App/Enums/BasicEnum.php b/App/Enums/BasicEnum.php index 5aa7925..a98aa35 100644 --- a/App/Enums/BasicEnum.php +++ b/App/Enums/BasicEnum.php @@ -3,16 +3,17 @@ namespace App\Enums; use ReflectionClass; +use ReflectionException; abstract class BasicEnum { private static array|null $constCacheArray = NULL; /** - * @return array|mixed - * @throws \ReflectionException + * @return array + * @throws ReflectionException */ - private static function getConstants() + private static function getConstants(): array { if (self::$constCacheArray == NULL) { self::$constCacheArray = []; @@ -28,7 +29,7 @@ private static function getConstants() /** * @param mixed $value * @return bool|int|string - * @throws \ReflectionException + * @throws ReflectionException */ public static function fromValue(mixed $value): bool|int|string { @@ -38,20 +39,23 @@ public static function fromValue(mixed $value): bool|int|string } /** + * Checks if the passed value exist as a constant in the called enum class. * @param string $value * @return bool + * @throws ReflectionException */ - public static function isValidType(string $value): bool + public static function isValid(string $value): bool { $constants = self::getConstants(); return array_key_exists($value, $constants); } /** - * @return array|mixed - * @throws \ReflectionException + * Return all constants of the called enum class + * @return array + * @throws ReflectionException */ - public static function getAll() + public static function getAll(): array { return self::getConstants(); } diff --git a/App/Enums/Status.php b/App/Enums/Status.php index daf61d9..981e9bb 100644 --- a/App/Enums/Status.php +++ b/App/Enums/Status.php @@ -4,9 +4,7 @@ class Status extends BasicEnum { - const ACTIVE = 1; - const INACTIVE = 0; protected function __construct() diff --git a/App/Interface/BaseCommandInterface.php b/App/Interface/BaseCommandInterface.php index a926fcc..03144b6 100644 --- a/App/Interface/BaseCommandInterface.php +++ b/App/Interface/BaseCommandInterface.php @@ -4,7 +4,9 @@ use App\Console\InputConsole; +use App\Console\InputValidator; use App\Console\OutputConsole; +use App\Model\Fast; use App\Store\StoreManager; interface BaseCommandInterface @@ -13,8 +15,16 @@ interface BaseCommandInterface * @param InputConsole $input * @param OutputConsole $output * @param StoreManager $store + * @param InputValidator $validator + * @param Fast $newFast */ - public function __construct(InputConsole $input, OutputConsole $output, StoreManager $store); + public function __construct( + InputConsole $input, + OutputConsole $output, + StoreManager $store, + InputValidator $validator, + Fast $newFast + ); public function run(); diff --git a/App/Model/Collection.php b/App/Model/Collection.php index e8b181b..3907326 100644 --- a/App/Model/Collection.php +++ b/App/Model/Collection.php @@ -43,6 +43,11 @@ function rewind() return reset($this->items); } + /** + * Iterates the collection over a callback function + * @param callable $callback + * @return $this|false + */ public function each(callable $callback) { foreach ($this as $key => $value) { @@ -53,6 +58,10 @@ public function each(callable $callback) return $this; } + /** + * Returns collection as an array + * @return array + */ public function toArray() { return $this->items; diff --git a/App/Model/Fast.php b/App/Model/Fast.php index 2935829..4e6907f 100644 --- a/App/Model/Fast.php +++ b/App/Model/Fast.php @@ -18,6 +18,14 @@ class Fast implements FastModelInterface, JsonSerializable 'end' ]; + protected array $fillable = [ + 'start', + 'status', + 'end', + 'type', + 'elapsedTime' + ]; + public function __construct( protected string $start = '', protected int $status = 1, @@ -41,40 +49,33 @@ public function __get($parameter): DateTime|int|string return $this->$parameter; } + /** + * Global params setter for fillable properties of Fast class. + * @param array $params + * @throws Exception + */ public function set(array $params) { foreach ($params as $param => $value) { + if (!in_array($param, $this->fillable)) throw new Exception("Cannot set property {$param} of " . get_class($this)); $this->$param = $value; } } - /** * @return string + * @throws \ReflectionException */ public function print(): string { - $type = FastType::fromValue($this->type); - $status = Status::fromValue($this->status); - - return " -------------------------------------- -Status ($status) \n\r +Status (" . Status::fromValue($this->status) . ") \n\r Started Fasting $this->start \n\r End date $this->end \n\r -Fast Type $type({$this->type}h) \n\r +Fast Type " . FastType::fromValue($this->type) . "({$this->type}h) \n\r Elapsed Time $this->elapsedTime; \n\r --------------------------------------- - "; - } - - /** - * @param $time - */ - public function setElapsedTime($time) - { - $this->elapsedTime = $time; +-------------------------------------- "; } /** diff --git a/App/Store/StoreManager.php b/App/Store/StoreManager.php index 3be5f1e..6c8c521 100644 --- a/App/Store/StoreManager.php +++ b/App/Store/StoreManager.php @@ -4,6 +4,7 @@ use App\{Enums\Status, Interface\FileManagerInterface, Model\Collection, Model\Fast}; use DateTime; +use Exception; class StoreManager implements FileManagerInterface @@ -12,7 +13,9 @@ class StoreManager implements FileManagerInterface protected string $file = "./store.json"; /** + * Fetches all fasts from store.json if any and returns a collection * @return Collection + * @throws Exception */ public function getAll(): Collection { @@ -32,12 +35,18 @@ public function getAll(): Collection type: $fast->type ); if ($fast->status === Status::ACTIVE && $newFast->start < $today) { - $newFast->setElapsedTime($today - ->diff($newFast->start) - ->format('%Y years %m months %d days %H h %i min %s sec')); + $newFast->set( + [ + 'elapsedTime' => $today + ->diff($newFast->start) + ->format('%Y years %m months %d days %H h %i min %s sec') + ] + ); } else { - $newFast->setElapsedTime($fast->elapsed_time); + $newFast->set([ + 'elapsedTime' => $fast->elapsed_time + ]); } $fastArray[] = $newFast; } @@ -45,7 +54,9 @@ public function getAll(): Collection } /** + * Checks if there are any active fasts in file. * @return bool + * @throws Exception */ public function hasActiveFasts(): bool { @@ -72,6 +83,7 @@ public function select($key) } /** + * Returns an active fast as Fast or falls on fail * @return false|Fast */ public function getActiveFast(): bool|Fast @@ -89,12 +101,18 @@ public function getActiveFast(): bool|Fast return $activeFast; } - + /** + * Writes the past parameter into the store file. + * @param $fasts + */ public function write($fasts) { file_put_contents($this->file, json_encode($fasts)); } + /** + * Delete an Active fast from store file + */ public function deleteActiveFast() { $fasts = $this->getAll(); diff --git a/fast b/fast index bc3a183..c79c20a 100644 --- a/fast +++ b/fast @@ -3,16 +3,20 @@ use App\Commands\CommandController; use App\Console\InputConsole; +use App\Console\InputValidator; use App\Console\OutputConsole; use App\Model\Fast; use App\Store\StoreManager; require_once './vendor/autoload.php'; + $commands = new CommandController( new InputConsole(), new OutputConsole(), - new StoreManager() + new StoreManager(), + new InputValidator(), + new Fast() ); $commands->run(); diff --git a/store.json b/store.json index aff4144..6bb0f60 100644 --- a/store.json +++ b/store.json @@ -1 +1 @@ -[{"status":0,"start":"2021-11-19 10:00:00","end":"2021-11-20 06:00:00","type":20,"elapsed_time":""},{"status":0,"start":"2021-11-18 16:50:00","end":"2021-11-19 08:50:00","type":16,"elapsed_time":"00 years 0 months 0 days 00 h 25 min 4 sec"},{"status":1,"start":"2021-11-18 17:40:00","end":"2021-11-19 13:40:00","type":20,"elapsed_time":""}] \ No newline at end of file +[{"status":0,"start":"2022-10-11 10:00:00","end":"2022-10-11 22:00:00","type":12,"elapsed_time":""}] \ No newline at end of file From 6a8f9ebfd21454bc78bb88e0703b7fa17a5d9913 Mon Sep 17 00:00:00 2001 From: Denis Ziberi Date: Fri, 19 Nov 2021 15:29:36 +0100 Subject: [PATCH 17/24] sad --- composer.lock | 18 + vendor/autoload.php | 7 + vendor/composer/ClassLoader.php | 479 ++++++++++++++++++++++++ vendor/composer/InstalledVersions.php | 299 +++++++++++++++ vendor/composer/LICENSE | 21 ++ vendor/composer/autoload_classmap.php | 10 + vendor/composer/autoload_namespaces.php | 9 + vendor/composer/autoload_psr4.php | 10 + vendor/composer/autoload_real.php | 55 +++ vendor/composer/autoload_static.php | 36 ++ vendor/composer/installed.json | 5 + vendor/composer/installed.php | 24 ++ 12 files changed, 973 insertions(+) create mode 100644 composer.lock create mode 100644 vendor/autoload.php create mode 100644 vendor/composer/ClassLoader.php create mode 100644 vendor/composer/InstalledVersions.php create mode 100644 vendor/composer/LICENSE create mode 100644 vendor/composer/autoload_classmap.php create mode 100644 vendor/composer/autoload_namespaces.php create mode 100644 vendor/composer/autoload_psr4.php create mode 100644 vendor/composer/autoload_real.php create mode 100644 vendor/composer/autoload_static.php create mode 100644 vendor/composer/installed.json create mode 100644 vendor/composer/installed.php diff --git a/composer.lock b/composer.lock new file mode 100644 index 0000000..2d09942 --- /dev/null +++ b/composer.lock @@ -0,0 +1,18 @@ +{ + "_readme": [ + "This file locks the dependencies of your project to a known state", + "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", + "This file is @generated automatically" + ], + "content-hash": "d751713988987e9331980363e24189ce", + "packages": [], + "packages-dev": [], + "aliases": [], + "minimum-stability": "stable", + "stability-flags": [], + "prefer-stable": false, + "prefer-lowest": false, + "platform": [], + "platform-dev": [], + "plugin-api-version": "2.0.0" +} diff --git a/vendor/autoload.php b/vendor/autoload.php new file mode 100644 index 0000000..727d0e3 --- /dev/null +++ b/vendor/autoload.php @@ -0,0 +1,7 @@ + + * Jordi Boggiano + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Composer\Autoload; + +/** + * ClassLoader implements a PSR-0, PSR-4 and classmap class loader. + * + * $loader = new \Composer\Autoload\ClassLoader(); + * + * // register classes with namespaces + * $loader->add('Symfony\Component', __DIR__.'/component'); + * $loader->add('Symfony', __DIR__.'/framework'); + * + * // activate the autoloader + * $loader->register(); + * + * // to enable searching the include path (eg. for PEAR packages) + * $loader->setUseIncludePath(true); + * + * In this example, if you try to use a class in the Symfony\Component + * namespace or one of its children (Symfony\Component\Console for instance), + * the autoloader will first look for the class under the component/ + * directory, and it will then fallback to the framework/ directory if not + * found before giving up. + * + * This class is loosely based on the Symfony UniversalClassLoader. + * + * @author Fabien Potencier + * @author Jordi Boggiano + * @see https://www.php-fig.org/psr/psr-0/ + * @see https://www.php-fig.org/psr/psr-4/ + */ +class ClassLoader +{ + private $vendorDir; + + // PSR-4 + private $prefixLengthsPsr4 = array(); + private $prefixDirsPsr4 = array(); + private $fallbackDirsPsr4 = array(); + + // PSR-0 + private $prefixesPsr0 = array(); + private $fallbackDirsPsr0 = array(); + + private $useIncludePath = false; + private $classMap = array(); + private $classMapAuthoritative = false; + private $missingClasses = array(); + private $apcuPrefix; + + private static $registeredLoaders = array(); + + public function __construct($vendorDir = null) + { + $this->vendorDir = $vendorDir; + } + + public function getPrefixes() + { + if (!empty($this->prefixesPsr0)) { + return call_user_func_array('array_merge', array_values($this->prefixesPsr0)); + } + + return array(); + } + + public function getPrefixesPsr4() + { + return $this->prefixDirsPsr4; + } + + public function getFallbackDirs() + { + return $this->fallbackDirsPsr0; + } + + public function getFallbackDirsPsr4() + { + return $this->fallbackDirsPsr4; + } + + public function getClassMap() + { + return $this->classMap; + } + + /** + * @param array $classMap Class to filename map + */ + public function addClassMap(array $classMap) + { + if ($this->classMap) { + $this->classMap = array_merge($this->classMap, $classMap); + } else { + $this->classMap = $classMap; + } + } + + /** + * Registers a set of PSR-0 directories for a given prefix, either + * appending or prepending to the ones previously set for this prefix. + * + * @param string $prefix The prefix + * @param array|string $paths The PSR-0 root directories + * @param bool $prepend Whether to prepend the directories + */ + public function add($prefix, $paths, $prepend = false) + { + if (!$prefix) { + if ($prepend) { + $this->fallbackDirsPsr0 = array_merge( + (array) $paths, + $this->fallbackDirsPsr0 + ); + } else { + $this->fallbackDirsPsr0 = array_merge( + $this->fallbackDirsPsr0, + (array) $paths + ); + } + + return; + } + + $first = $prefix[0]; + if (!isset($this->prefixesPsr0[$first][$prefix])) { + $this->prefixesPsr0[$first][$prefix] = (array) $paths; + + return; + } + if ($prepend) { + $this->prefixesPsr0[$first][$prefix] = array_merge( + (array) $paths, + $this->prefixesPsr0[$first][$prefix] + ); + } else { + $this->prefixesPsr0[$first][$prefix] = array_merge( + $this->prefixesPsr0[$first][$prefix], + (array) $paths + ); + } + } + + /** + * Registers a set of PSR-4 directories for a given namespace, either + * appending or prepending to the ones previously set for this namespace. + * + * @param string $prefix The prefix/namespace, with trailing '\\' + * @param array|string $paths The PSR-4 base directories + * @param bool $prepend Whether to prepend the directories + * + * @throws \InvalidArgumentException + */ + public function addPsr4($prefix, $paths, $prepend = false) + { + if (!$prefix) { + // Register directories for the root namespace. + if ($prepend) { + $this->fallbackDirsPsr4 = array_merge( + (array) $paths, + $this->fallbackDirsPsr4 + ); + } else { + $this->fallbackDirsPsr4 = array_merge( + $this->fallbackDirsPsr4, + (array) $paths + ); + } + } elseif (!isset($this->prefixDirsPsr4[$prefix])) { + // Register directories for a new namespace. + $length = strlen($prefix); + if ('\\' !== $prefix[$length - 1]) { + throw new \InvalidArgumentException("A non-empty PSR-4 prefix must end with a namespace separator."); + } + $this->prefixLengthsPsr4[$prefix[0]][$prefix] = $length; + $this->prefixDirsPsr4[$prefix] = (array) $paths; + } elseif ($prepend) { + // Prepend directories for an already registered namespace. + $this->prefixDirsPsr4[$prefix] = array_merge( + (array) $paths, + $this->prefixDirsPsr4[$prefix] + ); + } else { + // Append directories for an already registered namespace. + $this->prefixDirsPsr4[$prefix] = array_merge( + $this->prefixDirsPsr4[$prefix], + (array) $paths + ); + } + } + + /** + * Registers a set of PSR-0 directories for a given prefix, + * replacing any others previously set for this prefix. + * + * @param string $prefix The prefix + * @param array|string $paths The PSR-0 base directories + */ + public function set($prefix, $paths) + { + if (!$prefix) { + $this->fallbackDirsPsr0 = (array) $paths; + } else { + $this->prefixesPsr0[$prefix[0]][$prefix] = (array) $paths; + } + } + + /** + * Registers a set of PSR-4 directories for a given namespace, + * replacing any others previously set for this namespace. + * + * @param string $prefix The prefix/namespace, with trailing '\\' + * @param array|string $paths The PSR-4 base directories + * + * @throws \InvalidArgumentException + */ + public function setPsr4($prefix, $paths) + { + if (!$prefix) { + $this->fallbackDirsPsr4 = (array) $paths; + } else { + $length = strlen($prefix); + if ('\\' !== $prefix[$length - 1]) { + throw new \InvalidArgumentException("A non-empty PSR-4 prefix must end with a namespace separator."); + } + $this->prefixLengthsPsr4[$prefix[0]][$prefix] = $length; + $this->prefixDirsPsr4[$prefix] = (array) $paths; + } + } + + /** + * Turns on searching the include path for class files. + * + * @param bool $useIncludePath + */ + public function setUseIncludePath($useIncludePath) + { + $this->useIncludePath = $useIncludePath; + } + + /** + * Can be used to check if the autoloader uses the include path to check + * for classes. + * + * @return bool + */ + public function getUseIncludePath() + { + return $this->useIncludePath; + } + + /** + * Turns off searching the prefix and fallback directories for classes + * that have not been registered with the class map. + * + * @param bool $classMapAuthoritative + */ + public function setClassMapAuthoritative($classMapAuthoritative) + { + $this->classMapAuthoritative = $classMapAuthoritative; + } + + /** + * Should class lookup fail if not found in the current class map? + * + * @return bool + */ + public function isClassMapAuthoritative() + { + return $this->classMapAuthoritative; + } + + /** + * APCu prefix to use to cache found/not-found classes, if the extension is enabled. + * + * @param string|null $apcuPrefix + */ + public function setApcuPrefix($apcuPrefix) + { + $this->apcuPrefix = function_exists('apcu_fetch') && filter_var(ini_get('apc.enabled'), FILTER_VALIDATE_BOOLEAN) ? $apcuPrefix : null; + } + + /** + * The APCu prefix in use, or null if APCu caching is not enabled. + * + * @return string|null + */ + public function getApcuPrefix() + { + return $this->apcuPrefix; + } + + /** + * Registers this instance as an autoloader. + * + * @param bool $prepend Whether to prepend the autoloader or not + */ + public function register($prepend = false) + { + spl_autoload_register(array($this, 'loadClass'), true, $prepend); + + if (null === $this->vendorDir) { + return; + } + + if ($prepend) { + self::$registeredLoaders = array($this->vendorDir => $this) + self::$registeredLoaders; + } else { + unset(self::$registeredLoaders[$this->vendorDir]); + self::$registeredLoaders[$this->vendorDir] = $this; + } + } + + /** + * Unregisters this instance as an autoloader. + */ + public function unregister() + { + spl_autoload_unregister(array($this, 'loadClass')); + + if (null !== $this->vendorDir) { + unset(self::$registeredLoaders[$this->vendorDir]); + } + } + + /** + * Loads the given class or interface. + * + * @param string $class The name of the class + * @return bool|null True if loaded, null otherwise + */ + public function loadClass($class) + { + if ($file = $this->findFile($class)) { + includeFile($file); + + return true; + } + } + + /** + * Finds the path to the file where the class is defined. + * + * @param string $class The name of the class + * + * @return string|false The path if found, false otherwise + */ + public function findFile($class) + { + // class map lookup + if (isset($this->classMap[$class])) { + return $this->classMap[$class]; + } + if ($this->classMapAuthoritative || isset($this->missingClasses[$class])) { + return false; + } + if (null !== $this->apcuPrefix) { + $file = apcu_fetch($this->apcuPrefix.$class, $hit); + if ($hit) { + return $file; + } + } + + $file = $this->findFileWithExtension($class, '.php'); + + // Search for Hack files if we are running on HHVM + if (false === $file && defined('HHVM_VERSION')) { + $file = $this->findFileWithExtension($class, '.hh'); + } + + if (null !== $this->apcuPrefix) { + apcu_add($this->apcuPrefix.$class, $file); + } + + if (false === $file) { + // Remember that this class does not exist. + $this->missingClasses[$class] = true; + } + + return $file; + } + + /** + * Returns the currently registered loaders indexed by their corresponding vendor directories. + * + * @return self[] + */ + public static function getRegisteredLoaders() + { + return self::$registeredLoaders; + } + + private function findFileWithExtension($class, $ext) + { + // PSR-4 lookup + $logicalPathPsr4 = strtr($class, '\\', DIRECTORY_SEPARATOR) . $ext; + + $first = $class[0]; + if (isset($this->prefixLengthsPsr4[$first])) { + $subPath = $class; + while (false !== $lastPos = strrpos($subPath, '\\')) { + $subPath = substr($subPath, 0, $lastPos); + $search = $subPath . '\\'; + if (isset($this->prefixDirsPsr4[$search])) { + $pathEnd = DIRECTORY_SEPARATOR . substr($logicalPathPsr4, $lastPos + 1); + foreach ($this->prefixDirsPsr4[$search] as $dir) { + if (file_exists($file = $dir . $pathEnd)) { + return $file; + } + } + } + } + } + + // PSR-4 fallback dirs + foreach ($this->fallbackDirsPsr4 as $dir) { + if (file_exists($file = $dir . DIRECTORY_SEPARATOR . $logicalPathPsr4)) { + return $file; + } + } + + // PSR-0 lookup + if (false !== $pos = strrpos($class, '\\')) { + // namespaced class name + $logicalPathPsr0 = substr($logicalPathPsr4, 0, $pos + 1) + . strtr(substr($logicalPathPsr4, $pos + 1), '_', DIRECTORY_SEPARATOR); + } else { + // PEAR-like class name + $logicalPathPsr0 = strtr($class, '_', DIRECTORY_SEPARATOR) . $ext; + } + + if (isset($this->prefixesPsr0[$first])) { + foreach ($this->prefixesPsr0[$first] as $prefix => $dirs) { + if (0 === strpos($class, $prefix)) { + foreach ($dirs as $dir) { + if (file_exists($file = $dir . DIRECTORY_SEPARATOR . $logicalPathPsr0)) { + return $file; + } + } + } + } + } + + // PSR-0 fallback dirs + foreach ($this->fallbackDirsPsr0 as $dir) { + if (file_exists($file = $dir . DIRECTORY_SEPARATOR . $logicalPathPsr0)) { + return $file; + } + } + + // PSR-0 include paths. + if ($this->useIncludePath && $file = stream_resolve_include_path($logicalPathPsr0)) { + return $file; + } + + return false; + } +} + +/** + * Scope isolated include. + * + * Prevents access to $this/self from included files. + */ +function includeFile($file) +{ + include $file; +} diff --git a/vendor/composer/InstalledVersions.php b/vendor/composer/InstalledVersions.php new file mode 100644 index 0000000..3c69c55 --- /dev/null +++ b/vendor/composer/InstalledVersions.php @@ -0,0 +1,299 @@ + + array ( + 'pretty_version' => 'dev-develop', + 'version' => 'dev-develop', + 'aliases' => + array ( + ), + 'reference' => '42735d158123f570849a6644604ee730e1024e95', + 'name' => '__root__', + ), + 'versions' => + array ( + '__root__' => + array ( + 'pretty_version' => 'dev-develop', + 'version' => 'dev-develop', + 'aliases' => + array ( + ), + 'reference' => '42735d158123f570849a6644604ee730e1024e95', + ), + ), +); +private static $canGetVendors; +private static $installedByVendor = array(); + + + + + + + +public static function getInstalledPackages() +{ +$packages = array(); +foreach (self::getInstalled() as $installed) { +$packages[] = array_keys($installed['versions']); +} + +if (1 === \count($packages)) { +return $packages[0]; +} + +return array_keys(array_flip(\call_user_func_array('array_merge', $packages))); +} + + + + + + + + + +public static function isInstalled($packageName) +{ +foreach (self::getInstalled() as $installed) { +if (isset($installed['versions'][$packageName])) { +return true; +} +} + +return false; +} + + + + + + + + + + + + + + +public static function satisfies(VersionParser $parser, $packageName, $constraint) +{ +$constraint = $parser->parseConstraints($constraint); +$provided = $parser->parseConstraints(self::getVersionRanges($packageName)); + +return $provided->matches($constraint); +} + + + + + + + + + + +public static function getVersionRanges($packageName) +{ +foreach (self::getInstalled() as $installed) { +if (!isset($installed['versions'][$packageName])) { +continue; +} + +$ranges = array(); +if (isset($installed['versions'][$packageName]['pretty_version'])) { +$ranges[] = $installed['versions'][$packageName]['pretty_version']; +} +if (array_key_exists('aliases', $installed['versions'][$packageName])) { +$ranges = array_merge($ranges, $installed['versions'][$packageName]['aliases']); +} +if (array_key_exists('replaced', $installed['versions'][$packageName])) { +$ranges = array_merge($ranges, $installed['versions'][$packageName]['replaced']); +} +if (array_key_exists('provided', $installed['versions'][$packageName])) { +$ranges = array_merge($ranges, $installed['versions'][$packageName]['provided']); +} + +return implode(' || ', $ranges); +} + +throw new \OutOfBoundsException('Package "' . $packageName . '" is not installed'); +} + + + + + +public static function getVersion($packageName) +{ +foreach (self::getInstalled() as $installed) { +if (!isset($installed['versions'][$packageName])) { +continue; +} + +if (!isset($installed['versions'][$packageName]['version'])) { +return null; +} + +return $installed['versions'][$packageName]['version']; +} + +throw new \OutOfBoundsException('Package "' . $packageName . '" is not installed'); +} + + + + + +public static function getPrettyVersion($packageName) +{ +foreach (self::getInstalled() as $installed) { +if (!isset($installed['versions'][$packageName])) { +continue; +} + +if (!isset($installed['versions'][$packageName]['pretty_version'])) { +return null; +} + +return $installed['versions'][$packageName]['pretty_version']; +} + +throw new \OutOfBoundsException('Package "' . $packageName . '" is not installed'); +} + + + + + +public static function getReference($packageName) +{ +foreach (self::getInstalled() as $installed) { +if (!isset($installed['versions'][$packageName])) { +continue; +} + +if (!isset($installed['versions'][$packageName]['reference'])) { +return null; +} + +return $installed['versions'][$packageName]['reference']; +} + +throw new \OutOfBoundsException('Package "' . $packageName . '" is not installed'); +} + + + + + +public static function getRootPackage() +{ +$installed = self::getInstalled(); + +return $installed[0]['root']; +} + + + + + + + + +public static function getRawData() +{ +@trigger_error('getRawData only returns the first dataset loaded, which may not be what you expect. Use getAllRawData() instead which returns all datasets for all autoloaders present in the process.', E_USER_DEPRECATED); + +return self::$installed; +} + + + + + + + +public static function getAllRawData() +{ +return self::getInstalled(); +} + + + + + + + + + + + + + + + + + + + +public static function reload($data) +{ +self::$installed = $data; +self::$installedByVendor = array(); +} + + + + + +private static function getInstalled() +{ +if (null === self::$canGetVendors) { +self::$canGetVendors = method_exists('Composer\Autoload\ClassLoader', 'getRegisteredLoaders'); +} + +$installed = array(); + +if (self::$canGetVendors) { +foreach (ClassLoader::getRegisteredLoaders() as $vendorDir => $loader) { +if (isset(self::$installedByVendor[$vendorDir])) { +$installed[] = self::$installedByVendor[$vendorDir]; +} elseif (is_file($vendorDir.'/composer/installed.php')) { +$installed[] = self::$installedByVendor[$vendorDir] = require $vendorDir.'/composer/installed.php'; +} +} +} + +$installed[] = self::$installed; + +return $installed; +} +} diff --git a/vendor/composer/LICENSE b/vendor/composer/LICENSE new file mode 100644 index 0000000..f27399a --- /dev/null +++ b/vendor/composer/LICENSE @@ -0,0 +1,21 @@ + +Copyright (c) Nils Adermann, Jordi Boggiano + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is furnished +to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. + diff --git a/vendor/composer/autoload_classmap.php b/vendor/composer/autoload_classmap.php new file mode 100644 index 0000000..b26f1b1 --- /dev/null +++ b/vendor/composer/autoload_classmap.php @@ -0,0 +1,10 @@ + $vendorDir . '/composer/InstalledVersions.php', +); diff --git a/vendor/composer/autoload_namespaces.php b/vendor/composer/autoload_namespaces.php new file mode 100644 index 0000000..b7fc012 --- /dev/null +++ b/vendor/composer/autoload_namespaces.php @@ -0,0 +1,9 @@ + array($baseDir . '/App'), +); diff --git a/vendor/composer/autoload_real.php b/vendor/composer/autoload_real.php new file mode 100644 index 0000000..a7a115d --- /dev/null +++ b/vendor/composer/autoload_real.php @@ -0,0 +1,55 @@ += 50600 && !defined('HHVM_VERSION') && (!function_exists('zend_loader_file_encoded') || !zend_loader_file_encoded()); + if ($useStaticLoader) { + require __DIR__ . '/autoload_static.php'; + + call_user_func(\Composer\Autoload\ComposerStaticInit53a0023f3f1dabac92e13eef04255492::getInitializer($loader)); + } else { + $map = require __DIR__ . '/autoload_namespaces.php'; + foreach ($map as $namespace => $path) { + $loader->set($namespace, $path); + } + + $map = require __DIR__ . '/autoload_psr4.php'; + foreach ($map as $namespace => $path) { + $loader->setPsr4($namespace, $path); + } + + $classMap = require __DIR__ . '/autoload_classmap.php'; + if ($classMap) { + $loader->addClassMap($classMap); + } + } + + $loader->register(true); + + return $loader; + } +} diff --git a/vendor/composer/autoload_static.php b/vendor/composer/autoload_static.php new file mode 100644 index 0000000..226c1f7 --- /dev/null +++ b/vendor/composer/autoload_static.php @@ -0,0 +1,36 @@ + + array ( + 'App\\' => 4, + ), + ); + + public static $prefixDirsPsr4 = array ( + 'App\\' => + array ( + 0 => __DIR__ . '/../..' . '/App', + ), + ); + + public static $classMap = array ( + 'Composer\\InstalledVersions' => __DIR__ . '/..' . '/composer/InstalledVersions.php', + ); + + public static function getInitializer(ClassLoader $loader) + { + return \Closure::bind(function () use ($loader) { + $loader->prefixLengthsPsr4 = ComposerStaticInit53a0023f3f1dabac92e13eef04255492::$prefixLengthsPsr4; + $loader->prefixDirsPsr4 = ComposerStaticInit53a0023f3f1dabac92e13eef04255492::$prefixDirsPsr4; + $loader->classMap = ComposerStaticInit53a0023f3f1dabac92e13eef04255492::$classMap; + + }, null, ClassLoader::class); + } +} diff --git a/vendor/composer/installed.json b/vendor/composer/installed.json new file mode 100644 index 0000000..87fda74 --- /dev/null +++ b/vendor/composer/installed.json @@ -0,0 +1,5 @@ +{ + "packages": [], + "dev": true, + "dev-package-names": [] +} diff --git a/vendor/composer/installed.php b/vendor/composer/installed.php new file mode 100644 index 0000000..9bb15e7 --- /dev/null +++ b/vendor/composer/installed.php @@ -0,0 +1,24 @@ + + array ( + 'pretty_version' => 'dev-develop', + 'version' => 'dev-develop', + 'aliases' => + array ( + ), + 'reference' => '42735d158123f570849a6644604ee730e1024e95', + 'name' => '__root__', + ), + 'versions' => + array ( + '__root__' => + array ( + 'pretty_version' => 'dev-develop', + 'version' => 'dev-develop', + 'aliases' => + array ( + ), + 'reference' => '42735d158123f570849a6644604ee730e1024e95', + ), + ), +); From 88a327b69f037bde5bfcb987e3727bfc5033581d Mon Sep 17 00:00:00 2001 From: Denis Ziberi Date: Fri, 19 Nov 2021 15:40:54 +0100 Subject: [PATCH 18/24] edited redme file with app instructions --- README.md | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index f7c2473..2e8c9fd 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,16 @@ # Zero-consoleApp -PHP console app based on Zero +Simple console app for tracking fasts written in PHP. + +# Requirements + +1. PHP ^8.0 +2. Composer package see installation -> Composer + +# Instalation + +1. Clone or download from repository. +2. Get into the folder directory cd ./Zero-consoleApp +3. Run composer install +4. Run php fast +5. Enjoy ;) \ No newline at end of file From ac385a5c653d2c511a52b447c0aeb323ad6041d6 Mon Sep 17 00:00:00 2001 From: Denis Ziberi Date: Fri, 19 Nov 2021 15:42:29 +0100 Subject: [PATCH 19/24] edit redme --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 2e8c9fd..83aa014 100644 --- a/README.md +++ b/README.md @@ -5,7 +5,7 @@ Simple console app for tracking fasts written in PHP. # Requirements 1. PHP ^8.0 -2. Composer package see installation -> Composer +2. Composer package see installation -> Composer # Instalation From 4b8a69dd3971809c91d0a8d0c9f1e6fd5748c070 Mon Sep 17 00:00:00 2001 From: ziberiDev <92785026+ziberiDev@users.noreply.github.com> Date: Fri, 19 Nov 2021 15:47:02 +0100 Subject: [PATCH 20/24] Delete vendor directory --- vendor/autoload.php | 7 - vendor/composer/ClassLoader.php | 479 ------------------------ vendor/composer/InstalledVersions.php | 299 --------------- vendor/composer/LICENSE | 21 -- vendor/composer/autoload_classmap.php | 10 - vendor/composer/autoload_namespaces.php | 9 - vendor/composer/autoload_psr4.php | 10 - vendor/composer/autoload_real.php | 55 --- vendor/composer/autoload_static.php | 36 -- vendor/composer/installed.json | 5 - vendor/composer/installed.php | 24 -- 11 files changed, 955 deletions(-) delete mode 100644 vendor/autoload.php delete mode 100644 vendor/composer/ClassLoader.php delete mode 100644 vendor/composer/InstalledVersions.php delete mode 100644 vendor/composer/LICENSE delete mode 100644 vendor/composer/autoload_classmap.php delete mode 100644 vendor/composer/autoload_namespaces.php delete mode 100644 vendor/composer/autoload_psr4.php delete mode 100644 vendor/composer/autoload_real.php delete mode 100644 vendor/composer/autoload_static.php delete mode 100644 vendor/composer/installed.json delete mode 100644 vendor/composer/installed.php diff --git a/vendor/autoload.php b/vendor/autoload.php deleted file mode 100644 index 727d0e3..0000000 --- a/vendor/autoload.php +++ /dev/null @@ -1,7 +0,0 @@ - - * Jordi Boggiano - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -namespace Composer\Autoload; - -/** - * ClassLoader implements a PSR-0, PSR-4 and classmap class loader. - * - * $loader = new \Composer\Autoload\ClassLoader(); - * - * // register classes with namespaces - * $loader->add('Symfony\Component', __DIR__.'/component'); - * $loader->add('Symfony', __DIR__.'/framework'); - * - * // activate the autoloader - * $loader->register(); - * - * // to enable searching the include path (eg. for PEAR packages) - * $loader->setUseIncludePath(true); - * - * In this example, if you try to use a class in the Symfony\Component - * namespace or one of its children (Symfony\Component\Console for instance), - * the autoloader will first look for the class under the component/ - * directory, and it will then fallback to the framework/ directory if not - * found before giving up. - * - * This class is loosely based on the Symfony UniversalClassLoader. - * - * @author Fabien Potencier - * @author Jordi Boggiano - * @see https://www.php-fig.org/psr/psr-0/ - * @see https://www.php-fig.org/psr/psr-4/ - */ -class ClassLoader -{ - private $vendorDir; - - // PSR-4 - private $prefixLengthsPsr4 = array(); - private $prefixDirsPsr4 = array(); - private $fallbackDirsPsr4 = array(); - - // PSR-0 - private $prefixesPsr0 = array(); - private $fallbackDirsPsr0 = array(); - - private $useIncludePath = false; - private $classMap = array(); - private $classMapAuthoritative = false; - private $missingClasses = array(); - private $apcuPrefix; - - private static $registeredLoaders = array(); - - public function __construct($vendorDir = null) - { - $this->vendorDir = $vendorDir; - } - - public function getPrefixes() - { - if (!empty($this->prefixesPsr0)) { - return call_user_func_array('array_merge', array_values($this->prefixesPsr0)); - } - - return array(); - } - - public function getPrefixesPsr4() - { - return $this->prefixDirsPsr4; - } - - public function getFallbackDirs() - { - return $this->fallbackDirsPsr0; - } - - public function getFallbackDirsPsr4() - { - return $this->fallbackDirsPsr4; - } - - public function getClassMap() - { - return $this->classMap; - } - - /** - * @param array $classMap Class to filename map - */ - public function addClassMap(array $classMap) - { - if ($this->classMap) { - $this->classMap = array_merge($this->classMap, $classMap); - } else { - $this->classMap = $classMap; - } - } - - /** - * Registers a set of PSR-0 directories for a given prefix, either - * appending or prepending to the ones previously set for this prefix. - * - * @param string $prefix The prefix - * @param array|string $paths The PSR-0 root directories - * @param bool $prepend Whether to prepend the directories - */ - public function add($prefix, $paths, $prepend = false) - { - if (!$prefix) { - if ($prepend) { - $this->fallbackDirsPsr0 = array_merge( - (array) $paths, - $this->fallbackDirsPsr0 - ); - } else { - $this->fallbackDirsPsr0 = array_merge( - $this->fallbackDirsPsr0, - (array) $paths - ); - } - - return; - } - - $first = $prefix[0]; - if (!isset($this->prefixesPsr0[$first][$prefix])) { - $this->prefixesPsr0[$first][$prefix] = (array) $paths; - - return; - } - if ($prepend) { - $this->prefixesPsr0[$first][$prefix] = array_merge( - (array) $paths, - $this->prefixesPsr0[$first][$prefix] - ); - } else { - $this->prefixesPsr0[$first][$prefix] = array_merge( - $this->prefixesPsr0[$first][$prefix], - (array) $paths - ); - } - } - - /** - * Registers a set of PSR-4 directories for a given namespace, either - * appending or prepending to the ones previously set for this namespace. - * - * @param string $prefix The prefix/namespace, with trailing '\\' - * @param array|string $paths The PSR-4 base directories - * @param bool $prepend Whether to prepend the directories - * - * @throws \InvalidArgumentException - */ - public function addPsr4($prefix, $paths, $prepend = false) - { - if (!$prefix) { - // Register directories for the root namespace. - if ($prepend) { - $this->fallbackDirsPsr4 = array_merge( - (array) $paths, - $this->fallbackDirsPsr4 - ); - } else { - $this->fallbackDirsPsr4 = array_merge( - $this->fallbackDirsPsr4, - (array) $paths - ); - } - } elseif (!isset($this->prefixDirsPsr4[$prefix])) { - // Register directories for a new namespace. - $length = strlen($prefix); - if ('\\' !== $prefix[$length - 1]) { - throw new \InvalidArgumentException("A non-empty PSR-4 prefix must end with a namespace separator."); - } - $this->prefixLengthsPsr4[$prefix[0]][$prefix] = $length; - $this->prefixDirsPsr4[$prefix] = (array) $paths; - } elseif ($prepend) { - // Prepend directories for an already registered namespace. - $this->prefixDirsPsr4[$prefix] = array_merge( - (array) $paths, - $this->prefixDirsPsr4[$prefix] - ); - } else { - // Append directories for an already registered namespace. - $this->prefixDirsPsr4[$prefix] = array_merge( - $this->prefixDirsPsr4[$prefix], - (array) $paths - ); - } - } - - /** - * Registers a set of PSR-0 directories for a given prefix, - * replacing any others previously set for this prefix. - * - * @param string $prefix The prefix - * @param array|string $paths The PSR-0 base directories - */ - public function set($prefix, $paths) - { - if (!$prefix) { - $this->fallbackDirsPsr0 = (array) $paths; - } else { - $this->prefixesPsr0[$prefix[0]][$prefix] = (array) $paths; - } - } - - /** - * Registers a set of PSR-4 directories for a given namespace, - * replacing any others previously set for this namespace. - * - * @param string $prefix The prefix/namespace, with trailing '\\' - * @param array|string $paths The PSR-4 base directories - * - * @throws \InvalidArgumentException - */ - public function setPsr4($prefix, $paths) - { - if (!$prefix) { - $this->fallbackDirsPsr4 = (array) $paths; - } else { - $length = strlen($prefix); - if ('\\' !== $prefix[$length - 1]) { - throw new \InvalidArgumentException("A non-empty PSR-4 prefix must end with a namespace separator."); - } - $this->prefixLengthsPsr4[$prefix[0]][$prefix] = $length; - $this->prefixDirsPsr4[$prefix] = (array) $paths; - } - } - - /** - * Turns on searching the include path for class files. - * - * @param bool $useIncludePath - */ - public function setUseIncludePath($useIncludePath) - { - $this->useIncludePath = $useIncludePath; - } - - /** - * Can be used to check if the autoloader uses the include path to check - * for classes. - * - * @return bool - */ - public function getUseIncludePath() - { - return $this->useIncludePath; - } - - /** - * Turns off searching the prefix and fallback directories for classes - * that have not been registered with the class map. - * - * @param bool $classMapAuthoritative - */ - public function setClassMapAuthoritative($classMapAuthoritative) - { - $this->classMapAuthoritative = $classMapAuthoritative; - } - - /** - * Should class lookup fail if not found in the current class map? - * - * @return bool - */ - public function isClassMapAuthoritative() - { - return $this->classMapAuthoritative; - } - - /** - * APCu prefix to use to cache found/not-found classes, if the extension is enabled. - * - * @param string|null $apcuPrefix - */ - public function setApcuPrefix($apcuPrefix) - { - $this->apcuPrefix = function_exists('apcu_fetch') && filter_var(ini_get('apc.enabled'), FILTER_VALIDATE_BOOLEAN) ? $apcuPrefix : null; - } - - /** - * The APCu prefix in use, or null if APCu caching is not enabled. - * - * @return string|null - */ - public function getApcuPrefix() - { - return $this->apcuPrefix; - } - - /** - * Registers this instance as an autoloader. - * - * @param bool $prepend Whether to prepend the autoloader or not - */ - public function register($prepend = false) - { - spl_autoload_register(array($this, 'loadClass'), true, $prepend); - - if (null === $this->vendorDir) { - return; - } - - if ($prepend) { - self::$registeredLoaders = array($this->vendorDir => $this) + self::$registeredLoaders; - } else { - unset(self::$registeredLoaders[$this->vendorDir]); - self::$registeredLoaders[$this->vendorDir] = $this; - } - } - - /** - * Unregisters this instance as an autoloader. - */ - public function unregister() - { - spl_autoload_unregister(array($this, 'loadClass')); - - if (null !== $this->vendorDir) { - unset(self::$registeredLoaders[$this->vendorDir]); - } - } - - /** - * Loads the given class or interface. - * - * @param string $class The name of the class - * @return bool|null True if loaded, null otherwise - */ - public function loadClass($class) - { - if ($file = $this->findFile($class)) { - includeFile($file); - - return true; - } - } - - /** - * Finds the path to the file where the class is defined. - * - * @param string $class The name of the class - * - * @return string|false The path if found, false otherwise - */ - public function findFile($class) - { - // class map lookup - if (isset($this->classMap[$class])) { - return $this->classMap[$class]; - } - if ($this->classMapAuthoritative || isset($this->missingClasses[$class])) { - return false; - } - if (null !== $this->apcuPrefix) { - $file = apcu_fetch($this->apcuPrefix.$class, $hit); - if ($hit) { - return $file; - } - } - - $file = $this->findFileWithExtension($class, '.php'); - - // Search for Hack files if we are running on HHVM - if (false === $file && defined('HHVM_VERSION')) { - $file = $this->findFileWithExtension($class, '.hh'); - } - - if (null !== $this->apcuPrefix) { - apcu_add($this->apcuPrefix.$class, $file); - } - - if (false === $file) { - // Remember that this class does not exist. - $this->missingClasses[$class] = true; - } - - return $file; - } - - /** - * Returns the currently registered loaders indexed by their corresponding vendor directories. - * - * @return self[] - */ - public static function getRegisteredLoaders() - { - return self::$registeredLoaders; - } - - private function findFileWithExtension($class, $ext) - { - // PSR-4 lookup - $logicalPathPsr4 = strtr($class, '\\', DIRECTORY_SEPARATOR) . $ext; - - $first = $class[0]; - if (isset($this->prefixLengthsPsr4[$first])) { - $subPath = $class; - while (false !== $lastPos = strrpos($subPath, '\\')) { - $subPath = substr($subPath, 0, $lastPos); - $search = $subPath . '\\'; - if (isset($this->prefixDirsPsr4[$search])) { - $pathEnd = DIRECTORY_SEPARATOR . substr($logicalPathPsr4, $lastPos + 1); - foreach ($this->prefixDirsPsr4[$search] as $dir) { - if (file_exists($file = $dir . $pathEnd)) { - return $file; - } - } - } - } - } - - // PSR-4 fallback dirs - foreach ($this->fallbackDirsPsr4 as $dir) { - if (file_exists($file = $dir . DIRECTORY_SEPARATOR . $logicalPathPsr4)) { - return $file; - } - } - - // PSR-0 lookup - if (false !== $pos = strrpos($class, '\\')) { - // namespaced class name - $logicalPathPsr0 = substr($logicalPathPsr4, 0, $pos + 1) - . strtr(substr($logicalPathPsr4, $pos + 1), '_', DIRECTORY_SEPARATOR); - } else { - // PEAR-like class name - $logicalPathPsr0 = strtr($class, '_', DIRECTORY_SEPARATOR) . $ext; - } - - if (isset($this->prefixesPsr0[$first])) { - foreach ($this->prefixesPsr0[$first] as $prefix => $dirs) { - if (0 === strpos($class, $prefix)) { - foreach ($dirs as $dir) { - if (file_exists($file = $dir . DIRECTORY_SEPARATOR . $logicalPathPsr0)) { - return $file; - } - } - } - } - } - - // PSR-0 fallback dirs - foreach ($this->fallbackDirsPsr0 as $dir) { - if (file_exists($file = $dir . DIRECTORY_SEPARATOR . $logicalPathPsr0)) { - return $file; - } - } - - // PSR-0 include paths. - if ($this->useIncludePath && $file = stream_resolve_include_path($logicalPathPsr0)) { - return $file; - } - - return false; - } -} - -/** - * Scope isolated include. - * - * Prevents access to $this/self from included files. - */ -function includeFile($file) -{ - include $file; -} diff --git a/vendor/composer/InstalledVersions.php b/vendor/composer/InstalledVersions.php deleted file mode 100644 index 3c69c55..0000000 --- a/vendor/composer/InstalledVersions.php +++ /dev/null @@ -1,299 +0,0 @@ - - array ( - 'pretty_version' => 'dev-develop', - 'version' => 'dev-develop', - 'aliases' => - array ( - ), - 'reference' => '42735d158123f570849a6644604ee730e1024e95', - 'name' => '__root__', - ), - 'versions' => - array ( - '__root__' => - array ( - 'pretty_version' => 'dev-develop', - 'version' => 'dev-develop', - 'aliases' => - array ( - ), - 'reference' => '42735d158123f570849a6644604ee730e1024e95', - ), - ), -); -private static $canGetVendors; -private static $installedByVendor = array(); - - - - - - - -public static function getInstalledPackages() -{ -$packages = array(); -foreach (self::getInstalled() as $installed) { -$packages[] = array_keys($installed['versions']); -} - -if (1 === \count($packages)) { -return $packages[0]; -} - -return array_keys(array_flip(\call_user_func_array('array_merge', $packages))); -} - - - - - - - - - -public static function isInstalled($packageName) -{ -foreach (self::getInstalled() as $installed) { -if (isset($installed['versions'][$packageName])) { -return true; -} -} - -return false; -} - - - - - - - - - - - - - - -public static function satisfies(VersionParser $parser, $packageName, $constraint) -{ -$constraint = $parser->parseConstraints($constraint); -$provided = $parser->parseConstraints(self::getVersionRanges($packageName)); - -return $provided->matches($constraint); -} - - - - - - - - - - -public static function getVersionRanges($packageName) -{ -foreach (self::getInstalled() as $installed) { -if (!isset($installed['versions'][$packageName])) { -continue; -} - -$ranges = array(); -if (isset($installed['versions'][$packageName]['pretty_version'])) { -$ranges[] = $installed['versions'][$packageName]['pretty_version']; -} -if (array_key_exists('aliases', $installed['versions'][$packageName])) { -$ranges = array_merge($ranges, $installed['versions'][$packageName]['aliases']); -} -if (array_key_exists('replaced', $installed['versions'][$packageName])) { -$ranges = array_merge($ranges, $installed['versions'][$packageName]['replaced']); -} -if (array_key_exists('provided', $installed['versions'][$packageName])) { -$ranges = array_merge($ranges, $installed['versions'][$packageName]['provided']); -} - -return implode(' || ', $ranges); -} - -throw new \OutOfBoundsException('Package "' . $packageName . '" is not installed'); -} - - - - - -public static function getVersion($packageName) -{ -foreach (self::getInstalled() as $installed) { -if (!isset($installed['versions'][$packageName])) { -continue; -} - -if (!isset($installed['versions'][$packageName]['version'])) { -return null; -} - -return $installed['versions'][$packageName]['version']; -} - -throw new \OutOfBoundsException('Package "' . $packageName . '" is not installed'); -} - - - - - -public static function getPrettyVersion($packageName) -{ -foreach (self::getInstalled() as $installed) { -if (!isset($installed['versions'][$packageName])) { -continue; -} - -if (!isset($installed['versions'][$packageName]['pretty_version'])) { -return null; -} - -return $installed['versions'][$packageName]['pretty_version']; -} - -throw new \OutOfBoundsException('Package "' . $packageName . '" is not installed'); -} - - - - - -public static function getReference($packageName) -{ -foreach (self::getInstalled() as $installed) { -if (!isset($installed['versions'][$packageName])) { -continue; -} - -if (!isset($installed['versions'][$packageName]['reference'])) { -return null; -} - -return $installed['versions'][$packageName]['reference']; -} - -throw new \OutOfBoundsException('Package "' . $packageName . '" is not installed'); -} - - - - - -public static function getRootPackage() -{ -$installed = self::getInstalled(); - -return $installed[0]['root']; -} - - - - - - - - -public static function getRawData() -{ -@trigger_error('getRawData only returns the first dataset loaded, which may not be what you expect. Use getAllRawData() instead which returns all datasets for all autoloaders present in the process.', E_USER_DEPRECATED); - -return self::$installed; -} - - - - - - - -public static function getAllRawData() -{ -return self::getInstalled(); -} - - - - - - - - - - - - - - - - - - - -public static function reload($data) -{ -self::$installed = $data; -self::$installedByVendor = array(); -} - - - - - -private static function getInstalled() -{ -if (null === self::$canGetVendors) { -self::$canGetVendors = method_exists('Composer\Autoload\ClassLoader', 'getRegisteredLoaders'); -} - -$installed = array(); - -if (self::$canGetVendors) { -foreach (ClassLoader::getRegisteredLoaders() as $vendorDir => $loader) { -if (isset(self::$installedByVendor[$vendorDir])) { -$installed[] = self::$installedByVendor[$vendorDir]; -} elseif (is_file($vendorDir.'/composer/installed.php')) { -$installed[] = self::$installedByVendor[$vendorDir] = require $vendorDir.'/composer/installed.php'; -} -} -} - -$installed[] = self::$installed; - -return $installed; -} -} diff --git a/vendor/composer/LICENSE b/vendor/composer/LICENSE deleted file mode 100644 index f27399a..0000000 --- a/vendor/composer/LICENSE +++ /dev/null @@ -1,21 +0,0 @@ - -Copyright (c) Nils Adermann, Jordi Boggiano - -Permission is hereby granted, free of charge, to any person obtaining a copy -of this software and associated documentation files (the "Software"), to deal -in the Software without restriction, including without limitation the rights -to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -copies of the Software, and to permit persons to whom the Software is furnished -to do so, subject to the following conditions: - -The above copyright notice and this permission notice shall be included in all -copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN -THE SOFTWARE. - diff --git a/vendor/composer/autoload_classmap.php b/vendor/composer/autoload_classmap.php deleted file mode 100644 index b26f1b1..0000000 --- a/vendor/composer/autoload_classmap.php +++ /dev/null @@ -1,10 +0,0 @@ - $vendorDir . '/composer/InstalledVersions.php', -); diff --git a/vendor/composer/autoload_namespaces.php b/vendor/composer/autoload_namespaces.php deleted file mode 100644 index b7fc012..0000000 --- a/vendor/composer/autoload_namespaces.php +++ /dev/null @@ -1,9 +0,0 @@ - array($baseDir . '/App'), -); diff --git a/vendor/composer/autoload_real.php b/vendor/composer/autoload_real.php deleted file mode 100644 index a7a115d..0000000 --- a/vendor/composer/autoload_real.php +++ /dev/null @@ -1,55 +0,0 @@ -= 50600 && !defined('HHVM_VERSION') && (!function_exists('zend_loader_file_encoded') || !zend_loader_file_encoded()); - if ($useStaticLoader) { - require __DIR__ . '/autoload_static.php'; - - call_user_func(\Composer\Autoload\ComposerStaticInit53a0023f3f1dabac92e13eef04255492::getInitializer($loader)); - } else { - $map = require __DIR__ . '/autoload_namespaces.php'; - foreach ($map as $namespace => $path) { - $loader->set($namespace, $path); - } - - $map = require __DIR__ . '/autoload_psr4.php'; - foreach ($map as $namespace => $path) { - $loader->setPsr4($namespace, $path); - } - - $classMap = require __DIR__ . '/autoload_classmap.php'; - if ($classMap) { - $loader->addClassMap($classMap); - } - } - - $loader->register(true); - - return $loader; - } -} diff --git a/vendor/composer/autoload_static.php b/vendor/composer/autoload_static.php deleted file mode 100644 index 226c1f7..0000000 --- a/vendor/composer/autoload_static.php +++ /dev/null @@ -1,36 +0,0 @@ - - array ( - 'App\\' => 4, - ), - ); - - public static $prefixDirsPsr4 = array ( - 'App\\' => - array ( - 0 => __DIR__ . '/../..' . '/App', - ), - ); - - public static $classMap = array ( - 'Composer\\InstalledVersions' => __DIR__ . '/..' . '/composer/InstalledVersions.php', - ); - - public static function getInitializer(ClassLoader $loader) - { - return \Closure::bind(function () use ($loader) { - $loader->prefixLengthsPsr4 = ComposerStaticInit53a0023f3f1dabac92e13eef04255492::$prefixLengthsPsr4; - $loader->prefixDirsPsr4 = ComposerStaticInit53a0023f3f1dabac92e13eef04255492::$prefixDirsPsr4; - $loader->classMap = ComposerStaticInit53a0023f3f1dabac92e13eef04255492::$classMap; - - }, null, ClassLoader::class); - } -} diff --git a/vendor/composer/installed.json b/vendor/composer/installed.json deleted file mode 100644 index 87fda74..0000000 --- a/vendor/composer/installed.json +++ /dev/null @@ -1,5 +0,0 @@ -{ - "packages": [], - "dev": true, - "dev-package-names": [] -} diff --git a/vendor/composer/installed.php b/vendor/composer/installed.php deleted file mode 100644 index 9bb15e7..0000000 --- a/vendor/composer/installed.php +++ /dev/null @@ -1,24 +0,0 @@ - - array ( - 'pretty_version' => 'dev-develop', - 'version' => 'dev-develop', - 'aliases' => - array ( - ), - 'reference' => '42735d158123f570849a6644604ee730e1024e95', - 'name' => '__root__', - ), - 'versions' => - array ( - '__root__' => - array ( - 'pretty_version' => 'dev-develop', - 'version' => 'dev-develop', - 'aliases' => - array ( - ), - 'reference' => '42735d158123f570849a6644604ee730e1024e95', - ), - ), -); From 9a3c83c05aed9cdf7cf323b11115d88b58035b95 Mon Sep 17 00:00:00 2001 From: Denis Ziberi Date: Fri, 19 Nov 2021 15:52:21 +0100 Subject: [PATCH 21/24] modified gitignore --- .gitignore | 2 +- composer.lock | 18 ------------------ 2 files changed, 1 insertion(+), 19 deletions(-) delete mode 100644 composer.lock diff --git a/.gitignore b/.gitignore index b67f666..bd4c47e 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,4 @@ -/composer.lock +composer.lock /vendor/ .idea diff --git a/composer.lock b/composer.lock deleted file mode 100644 index 2d09942..0000000 --- a/composer.lock +++ /dev/null @@ -1,18 +0,0 @@ -{ - "_readme": [ - "This file locks the dependencies of your project to a known state", - "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", - "This file is @generated automatically" - ], - "content-hash": "d751713988987e9331980363e24189ce", - "packages": [], - "packages-dev": [], - "aliases": [], - "minimum-stability": "stable", - "stability-flags": [], - "prefer-stable": false, - "prefer-lowest": false, - "platform": [], - "platform-dev": [], - "plugin-api-version": "2.0.0" -} From 412f0dbb1c937662621bd40f748bb1de68e625f4 Mon Sep 17 00:00:00 2001 From: Denis Ziberi Date: Tue, 23 Nov 2021 16:34:52 +0100 Subject: [PATCH 22/24] resolved comments from pull request --- .env | 1 + .gitignore | 2 +- App/Commands/BaseCommandController.php | 20 ++++++------ App/Commands/CheckCommand.php | 5 ++- App/Commands/CommandController.php | 10 ++---- App/Commands/EndCommand.php | 3 -- App/Commands/ExitCommand.php | 11 ++----- App/Commands/ListCommand.php | 14 ++++---- App/Console/OutputConsole.php | 45 +++++++------------------- App/Enums/FastType.php | 13 +++----- App/Enums/Status.php | 5 --- App/Helpers/Json.php | 29 +++++++++++++++++ App/Interface/BaseCommandInterface.php | 2 -- App/Interface/FastModelInterface.php | 1 - App/Model/Collection.php | 24 ++++---------- App/Model/Fast.php | 4 +-- App/Store/StoreManager.php | 28 ++++++++++++---- composer.json | 5 ++- fast | 3 +- store.example.json | 9 ------ store.json | 1 - 21 files changed, 107 insertions(+), 128 deletions(-) create mode 100644 .env create mode 100644 App/Helpers/Json.php delete mode 100644 store.example.json delete mode 100644 store.json diff --git a/.env b/.env new file mode 100644 index 0000000..e90b489 --- /dev/null +++ b/.env @@ -0,0 +1 @@ +APP_STORAGE=./store.json \ No newline at end of file diff --git a/.gitignore b/.gitignore index bd4c47e..f454177 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,4 @@ composer.lock /vendor/ .idea - +store.json \ No newline at end of file diff --git a/App/Commands/BaseCommandController.php b/App/Commands/BaseCommandController.php index 5d5b442..535d950 100644 --- a/App/Commands/BaseCommandController.php +++ b/App/Commands/BaseCommandController.php @@ -25,8 +25,7 @@ public function __construct( protected StoreManager $store, protected InputValidator $validator, protected Fast $newFast - ) - { + ){ $this->setFastTypes(); } @@ -36,11 +35,11 @@ public function __construct( */ protected function getStartDate() { - $this->output->writeYellow('Enter Start Date of Fast format:(Y-m-d H:i:s) => (2020-10-10 20:00:00)'); + $this->output->write('Enter Start Date of Fast format:(Y-m-d H:i:s) => (2020-10-10 20:00:00)', 'yellow'); $userInput = $this->input->getInput(); while ($message = $this->validator->validateStartDate($userInput)) { - $this->output->writeError($message); + $this->output->write($message, 'red'); $userInput = $this->input->getInput(); } $this->newFast->set([ @@ -54,13 +53,12 @@ protected function getStartDate() */ protected function getFastType() { - $this->output->writeYellow('Select a fast type'); + $this->output->write('Select a fast type', 'yellow'); $this->printFastTypes(); $userInput = $this->input->getInput(); - while (!key_exists($userInput, $this->fastTypes)) { - $this->output->writeError('Please choose from existing types.'); - + while (!isset($this->fastTypes[$userInput])) { + $this->output->write('Please choose from existing types.', 'red'); $userInput = $this->input->getInput(); } $this->newFast->set([ @@ -87,7 +85,7 @@ protected function setFastTypes() protected function printFastTypes() { foreach ($this->fastTypes as $key => $value) { - $this->output->writeYellow("[$key] " . $value['const'] . " ({$value['value']}" . 'h)'); + $this->output->write("[$key] " . $value['const'] . " ({$value['value']}" . 'h)', 'yellow'); } } @@ -133,7 +131,7 @@ protected function save(Fast $fast) protected function askForConfirmation(string $question): string { - $this->output->writeYellow($question); + $this->output->write($question, 'yellow'); $this->printConfirmationMenu(); return strtoupper($this->input->getInput()); } @@ -141,7 +139,7 @@ protected function askForConfirmation(string $question): string private function printConfirmationMenu() { foreach ($this->confirmationOptions as $key => $value) { - $this->output->writeYellow("[$key]"); + $this->output->write("[$key]", 'yellow'); } } } \ No newline at end of file diff --git a/App/Commands/CheckCommand.php b/App/Commands/CheckCommand.php index 7258260..4684a75 100644 --- a/App/Commands/CheckCommand.php +++ b/App/Commands/CheckCommand.php @@ -6,13 +6,12 @@ class CheckCommand extends BaseCommandController implements BaseCommandInterface { - public function run() { if (!$activeFast = $this->store->getActiveFast()) { - $this->output->writeError('You have no active fast please create one.'); + $this->output->write('You have no active fast please create one.', 'red'); return; } - $this->output->writeGreen($activeFast->print()); + $this->output->write($activeFast->print(), 'green'); } } \ No newline at end of file diff --git a/App/Commands/CommandController.php b/App/Commands/CommandController.php index d8f2346..2610968 100644 --- a/App/Commands/CommandController.php +++ b/App/Commands/CommandController.php @@ -3,10 +3,9 @@ namespace App\Commands; use App\Console\{InputConsole, InputValidator, OutputConsole}; -use App\Interface\{BaseCommandInterface , FileManagerInterface}; +use App\Interface\{BaseCommandInterface, FileManagerInterface}; use App\Model\Fast; - class CommandController implements BaseCommandInterface { @@ -62,10 +61,7 @@ public function __construct( protected FileManagerInterface $store, protected InputValidator $validator, protected Fast $newFast, - ) - { - } - + ){} public function run() { @@ -87,7 +83,7 @@ public function run() ); $command->run(); } else { - $this->output->writeYellow('Please select a specific command.'); + $this->output->write('Please select a specific command.', 'yellow'); } } } diff --git a/App/Commands/EndCommand.php b/App/Commands/EndCommand.php index 649712e..08039d6 100644 --- a/App/Commands/EndCommand.php +++ b/App/Commands/EndCommand.php @@ -8,8 +8,6 @@ class EndCommand extends BaseCommandController implements BaseCommandInterface { - - public function run() { $today = new DateTime('NOW'); @@ -34,5 +32,4 @@ public function run() $this->save($activeFast); } } - } \ No newline at end of file diff --git a/App/Commands/ExitCommand.php b/App/Commands/ExitCommand.php index 07a5848..4e88de5 100644 --- a/App/Commands/ExitCommand.php +++ b/App/Commands/ExitCommand.php @@ -7,28 +7,23 @@ Console\OutputConsole, Interface\BaseCommandInterface, Store\StoreManager, - Model\Fast -}; - + Model\Fast}; class ExitCommand extends BaseCommandController implements BaseCommandInterface { - public function __construct( protected InputConsole $input, protected OutputConsole $output, protected StoreManager $store, protected InputValidator $validator, protected Fast $newFast - ) - { - } + ){} public function run() { $userInput = $this->askForConfirmation("Are you sure you want to exit the app?"); if (!$this->confirmationOptions[$userInput]) return; - $this->output->writeGreen('See you soon.. Goodbye....'); + $this->output->write('See you soon.. Goodbye....', 'green'); exit; } } \ No newline at end of file diff --git a/App/Commands/ListCommand.php b/App/Commands/ListCommand.php index dd5fb37..d2341d6 100644 --- a/App/Commands/ListCommand.php +++ b/App/Commands/ListCommand.php @@ -2,10 +2,10 @@ namespace App\Commands; -use App\Console\{InputConsole , InputValidator , OutputConsole}; +use App\Console\{InputConsole, InputValidator, OutputConsole}; use App\Enums\Status; use App\Interface\BaseCommandInterface; -use App\Model\{Collection , Fast}; +use App\Model\{Collection, Fast}; use App\Store\StoreManager; class ListCommand implements BaseCommandInterface @@ -17,9 +17,7 @@ public function __construct( protected StoreManager $store, protected InputValidator $validator, protected Fast $newFast - ) - { - } + ){} public function run() { @@ -35,9 +33,11 @@ private function listFasts(Collection $fasts) { $fasts->each(function ($key, $fast) { if ($fast->status !== Status::ACTIVE) { - $this->output->writeError("Fast number:$key {$fast->print()}"); + $this->output->write("Fast number:$key {$fast->print()}", 'red'); + + }else{ + $this->output->write("Fast number:$key {$fast->print()}", 'green'); } - $this->output->writeGreen("Fast number:$key {$fast->print()}"); }); } } \ No newline at end of file diff --git a/App/Console/OutputConsole.php b/App/Console/OutputConsole.php index ef2aa72..f7abf4b 100644 --- a/App/Console/OutputConsole.php +++ b/App/Console/OutputConsole.php @@ -7,43 +7,22 @@ class OutputConsole { + protected array $colors = [ + 'white' => "[1;37m", + 'yellow' => "[33;1m", + 'green' => "[32;1m", + 'red' => "[41;1m" + ]; /** - * - * Echos passed string into console as white text + * Echos passed string into console as text * @param $text + * @param string $color + * color options (white{default} , yellow , green , red{outputs white text with red background}) */ - public function write($text) + public function write($text, string $color = 'white') { - echo $text . "\n\r"; + $outputColor = $this->colors["$color"] ?? "[1;37m"; + echo "\e" . $outputColor . $text . "\e[0m" . "\n\r"; } - - /** - * Echos passed string into console as yellow text - * @param $text - */ - public function writeYellow($text) - { - echo "\e[33;1m" . $text . "\e[0m" . "\n\r"; - } - - /** - * Echos passed string into console as green text - * @param $text - */ - public function writeGreen($text) - { - echo "\e[32;1m" . $text . "\e[0m" . "\n\r"; - } - - /** - * Echos passed string into console with red background text - * @param $text - */ - public function writeError($text) - { - echo "\e[41;1m" . $text . "\e[0m" . "\n\r"; - } - - } \ No newline at end of file diff --git a/App/Enums/FastType.php b/App/Enums/FastType.php index 34b5607..ef7799b 100644 --- a/App/Enums/FastType.php +++ b/App/Enums/FastType.php @@ -5,13 +5,8 @@ abstract class FastType extends BasicEnum { - const SHORT = 12; - const INTERMEDIATE = 16; - const MEDIUM = 20; - const LONG = 32; - - protected function __construct() - { - } - + const SHORT = 12; + const INTERMEDIATE = 16; + const MEDIUM = 20; + const LONG = 32; } \ No newline at end of file diff --git a/App/Enums/Status.php b/App/Enums/Status.php index 981e9bb..ee8efd2 100644 --- a/App/Enums/Status.php +++ b/App/Enums/Status.php @@ -6,9 +6,4 @@ class Status extends BasicEnum { const ACTIVE = 1; const INACTIVE = 0; - - protected function __construct() - { - } - } \ No newline at end of file diff --git a/App/Helpers/Json.php b/App/Helpers/Json.php new file mode 100644 index 0000000..3a65cfe --- /dev/null +++ b/App/Helpers/Json.php @@ -0,0 +1,29 @@ +items); } - public - function next() + public function next() { return next($this->items); } - public - function key() + public function key() { return key($this->items); } - public - function valid() + public function valid() { return current($this->items) !== false; } - public - function rewind() + public function rewind() { return reset($this->items); } @@ -48,7 +38,7 @@ function rewind() * @param callable $callback * @return $this|false */ - public function each(callable $callback) + public function each(callable $callback): bool|static { foreach ($this as $key => $value) { if ($callback($key, $value) === false) { diff --git a/App/Model/Fast.php b/App/Model/Fast.php index 4e6907f..9fdb166 100644 --- a/App/Model/Fast.php +++ b/App/Model/Fast.php @@ -31,8 +31,8 @@ public function __construct( protected int $status = 1, protected string $end = '', protected int $type = 0, - protected string $elapsedTime = '') - { + protected string $elapsedTime = '' + ){ } /** diff --git a/App/Store/StoreManager.php b/App/Store/StoreManager.php index 6c8c521..c574bed 100644 --- a/App/Store/StoreManager.php +++ b/App/Store/StoreManager.php @@ -2,7 +2,7 @@ namespace App\Store; -use App\{Enums\Status, Interface\FileManagerInterface, Model\Collection, Model\Fast}; +use App\{Enums\Status, Helpers\Json, Interface\FileManagerInterface, Model\Collection, Model\Fast}; use DateTime; use Exception; @@ -10,7 +10,21 @@ class StoreManager implements FileManagerInterface { - protected string $file = "./store.json"; + protected string $file; + + public function __construct() + { + if (!file_exists($_ENV['APP_STORAGE'])) { + try { + fopen($_ENV['APP_STORAGE'], 'w'); + } catch (\Throwable $e) { + + } + + } + $this->file = $_ENV['APP_STORAGE']; + + } /** * Fetches all fasts from store.json if any and returns a collection @@ -21,8 +35,8 @@ public function getAll(): Collection { $today = new DateTime('NOW'); $fastArray = []; - $storeFasts = json_decode( - file_get_contents($this->file) + $storeFasts = Json::decode( + file_get_contents("$this->file") , false); if (!$storeFasts) { return new Collection([]); @@ -83,8 +97,8 @@ public function select($key) } /** - * Returns an active fast as Fast or falls on fail - * @return false|Fast + * @return bool|Fast + * @throws Exception */ public function getActiveFast(): bool|Fast { @@ -107,7 +121,7 @@ public function getActiveFast(): bool|Fast */ public function write($fasts) { - file_put_contents($this->file, json_encode($fasts)); + file_put_contents($this->file, Json::encode($fasts)); } /** diff --git a/composer.json b/composer.json index bf03ae2..c6f49db 100644 --- a/composer.json +++ b/composer.json @@ -3,5 +3,8 @@ "psr-4": { "App\\": "./App" } + }, + "require": { + "vlucas/phpdotenv": "^5.4" } -} \ No newline at end of file +} diff --git a/fast b/fast index c79c20a..7cebdbd 100644 --- a/fast +++ b/fast @@ -9,7 +9,8 @@ use App\Model\Fast; use App\Store\StoreManager; require_once './vendor/autoload.php'; - +$dotenv = Dotenv\Dotenv::createImmutable(__DIR__, '.env'); +$dotenv->load(); $commands = new CommandController( new InputConsole(), diff --git a/store.example.json b/store.example.json deleted file mode 100644 index ebc1367..0000000 --- a/store.example.json +++ /dev/null @@ -1,9 +0,0 @@ -[ - { - "status": 0, - "start": "2021-11-22 10:00:00", - "end": "2021-11-22 10:00:00", - "type": 16, - "elapsed_time": "0 years 0 months 0 days 0 hours 0 minutes 0 seconds" - } -] \ No newline at end of file diff --git a/store.json b/store.json deleted file mode 100644 index 6bb0f60..0000000 --- a/store.json +++ /dev/null @@ -1 +0,0 @@ -[{"status":0,"start":"2022-10-11 10:00:00","end":"2022-10-11 22:00:00","type":12,"elapsed_time":""}] \ No newline at end of file From 6659062266aeb8fc3494814aefe39f9418d49953 Mon Sep 17 00:00:00 2001 From: Denis Ziberi Date: Fri, 23 Sep 2022 20:59:30 +0200 Subject: [PATCH 23/24] fixed issues --- App/Model/Collection.php | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/App/Model/Collection.php b/App/Model/Collection.php index dd6f3f8..3576732 100644 --- a/App/Model/Collection.php +++ b/App/Model/Collection.php @@ -8,29 +8,29 @@ class Collection implements Iterator { public function __construct(protected array $items){} - public function current() + public function current() : mixed { return current($this->items); } - public function next() + public function next() :void { - return next($this->items); + next($this->items); } - public function key() + public function key() : mixed { return key($this->items); } - public function valid() + public function valid() : bool { return current($this->items) !== false; } - public function rewind() + public function rewind() : void { - return reset($this->items); + reset($this->items); } /** From 3a4101a9ea255139e47c05d592bab6f7ac0464c2 Mon Sep 17 00:00:00 2001 From: Denis Ziberi Date: Fri, 23 Sep 2022 21:05:14 +0200 Subject: [PATCH 24/24] update file manager --- App/Interface/FileManagerInterface.php | 13 +++++++++++-- App/Store/StoreManager.php | 7 ------- 2 files changed, 11 insertions(+), 9 deletions(-) diff --git a/App/Interface/FileManagerInterface.php b/App/Interface/FileManagerInterface.php index 265e1d6..ee6ab0a 100644 --- a/App/Interface/FileManagerInterface.php +++ b/App/Interface/FileManagerInterface.php @@ -2,9 +2,18 @@ namespace App\Interface; +use App\Model\Collection; +use App\Model\Fast; + interface FileManagerInterface { - public function getAll(); - public function select($key); + public function getAll() :Collection; + public function write(array $fasts); + + public function hasActiveFasts(): bool; + + public function getActiveFast(): bool|Fast; + + public function deleteActiveFast(); } \ No newline at end of file diff --git a/App/Store/StoreManager.php b/App/Store/StoreManager.php index c574bed..aa1a473 100644 --- a/App/Store/StoreManager.php +++ b/App/Store/StoreManager.php @@ -88,13 +88,6 @@ public function hasActiveFasts(): bool return $hasActive; } - /** - * @param $key - */ - public function select($key) - { - // TODO: Implement select() method. - } /** * @return bool|Fast