From 338883fa18bfe167b7081a4046102fbfc23345d5 Mon Sep 17 00:00:00 2001 From: Anatoliy Melnikov <5785276@gmail.com> Date: Fri, 7 Jun 2024 00:11:22 +0300 Subject: [PATCH] Implement application --- .todo-registrar.dist.php | 9 +++ bin/todo-registrar | 56 +++++++++++++++++++ composer.json | 3 + src/Application.php | 24 ++++++++ src/ApplicationFactory.php | 55 ++++++++++++++++++ src/Config.php | 54 ++++++++++++++++++ src/Enum/RegistrarType.php | 16 ++++++ src/Service/Registrar/RegistrarFactory.php | 26 +++++++++ .../Registrar/RegistrarFactoryInterface.php | 13 +++++ 9 files changed, 256 insertions(+) create mode 100644 .todo-registrar.dist.php create mode 100644 bin/todo-registrar create mode 100644 src/Application.php create mode 100644 src/ApplicationFactory.php create mode 100644 src/Config.php create mode 100644 src/Enum/RegistrarType.php create mode 100644 src/Service/Registrar/RegistrarFactory.php create mode 100644 src/Service/Registrar/RegistrarFactoryInterface.php diff --git a/.todo-registrar.dist.php b/.todo-registrar.dist.php new file mode 100644 index 0000000..3285217 --- /dev/null +++ b/.todo-registrar.dist.php @@ -0,0 +1,9 @@ +setFinder(new Finder()); diff --git a/bin/todo-registrar b/bin/todo-registrar new file mode 100644 index 0000000..a103334 --- /dev/null +++ b/bin/todo-registrar @@ -0,0 +1,56 @@ +#!/usr/bin/env php + ['c', '.todo-registrar.dist.php'], + ]; + + foreach ($defaults as $long => [$short, $default]) { + if (isset($options[$short], $options[$long])) { + throw new InvalidArgumentException(sprintf('Option %s is duplicated', $long)); + } + $values[$long] = $absolutePathMaker($options[$short] ?? $options[$long] ?? $default); + } + + return $values; +})(); + +/** @var Config $config */ +$config = require $options['config']; + +(new ApplicationFactory())->create($config)->run(); diff --git a/composer.json b/composer.json index 8aff9fc..2f32bd5 100644 --- a/composer.json +++ b/composer.json @@ -15,6 +15,9 @@ "Aeliot\\TodoRegistrar\\Test\\": "tests/" } }, + "bin": [ + "bin/todo-registrar" + ], "config": { "allow-plugins": { "composer/*": true diff --git a/src/Application.php b/src/Application.php new file mode 100644 index 0000000..e7fe3b2 --- /dev/null +++ b/src/Application.php @@ -0,0 +1,24 @@ +finder as $file) { + $this->fileProcessor->process($file); + } + } +} \ No newline at end of file diff --git a/src/ApplicationFactory.php b/src/ApplicationFactory.php new file mode 100644 index 0000000..3cb1efc --- /dev/null +++ b/src/ApplicationFactory.php @@ -0,0 +1,55 @@ +createRegistrar($config); + $commentRegistrar = $this->createCommentRegistrar($registrar); + $fileProcessor = $this->createFileProcessor($commentRegistrar); + + return new Application( + $config->getFinder(), + $fileProcessor, + ); + } + + private function createCommentRegistrar(RegistrarInterface $registrar): CommentRegistrar + { + return new CommentRegistrar( + new Detector(), + new Extractor(), + $registrar, + ); + } + + private function createFileProcessor(CommentRegistrar $commentRegistrar): FileProcessor + { + return new FileProcessor( + $commentRegistrar, + new Saver(), + new Tokenizer(), + ); + } + + private function createRegistrar(Config $config): RegistrarInterface + { + return (new RegistrarFactory())->createRegistrar( + $config->getRegistrarType(), + $config->getRegistrarConfig(), + ); + } +} \ No newline at end of file diff --git a/src/Config.php b/src/Config.php new file mode 100644 index 0000000..805818b --- /dev/null +++ b/src/Config.php @@ -0,0 +1,54 @@ + + */ + private array $registrarConfig; + private RegistrarType $registrarType; + + public function getFinder(): Finder + { + return $this->finder; + } + + public function setFinder(Finder $finder): self + { + $this->finder = $finder; + + return $this; + } + + /** + * @return array + */ + public function getRegistrarConfig(): array + { + return $this->registrarConfig; + } + + public function getRegistrarType(): RegistrarType + { + return $this->registrarType; + } + + /** + * @param array $config + */ + public function setRegistrar(RegistrarType $type, array $config): self + { + $this->registrarType = $type; + $this->registrarConfig = $config; + + return $this; + } +} \ No newline at end of file diff --git a/src/Enum/RegistrarType.php b/src/Enum/RegistrarType.php new file mode 100644 index 0000000..d389d62 --- /dev/null +++ b/src/Enum/RegistrarType.php @@ -0,0 +1,16 @@ + $config + */ + public function createRegistrar(RegistrarType $type, array $config): RegistrarInterface + { + return $this->getExactFactory($type)->create($config); + } + + private function getExactFactory(RegistrarType $type): RegistrarFactoryInterface + { + return match ($type) { + // TODO add factory of different registrars + default => throw new \DomainException(sprintf('Not supported registrar type "%s"', $type->value)), + }; + } +} \ No newline at end of file diff --git a/src/Service/Registrar/RegistrarFactoryInterface.php b/src/Service/Registrar/RegistrarFactoryInterface.php new file mode 100644 index 0000000..63b1fc8 --- /dev/null +++ b/src/Service/Registrar/RegistrarFactoryInterface.php @@ -0,0 +1,13 @@ + $config + */ + public function create(array $config): RegistrarInterface; +} \ No newline at end of file