|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Dot\Maker; |
| 6 | + |
| 7 | +use function sprintf; |
| 8 | + |
| 9 | +use const PHP_EOL; |
| 10 | + |
| 11 | +class Message |
| 12 | +{ |
| 13 | + public const ADD_CONFIG_PROVIDER_TO_CONFIG = 1; |
| 14 | + public const ADD_CORE_CONFIG_PROVIDER_TO_CONFIG = 2; |
| 15 | + public const ADD_MODULE_TO_COMPOSER = 3; |
| 16 | + public const ADD_CORE_MODULE_TO_COMPOSER = 4; |
| 17 | + public const ADD_COMMAND_TO_CONFIG = 5; |
| 18 | + public const ADD_MIDDLEWARE_TO_PIPELINE = 6; |
| 19 | + public const DUMP_COMPOSER_AUTOLOADER = 7; |
| 20 | + public const GENERATE_MIGRATION = 8; |
| 21 | + |
| 22 | + public function __construct( |
| 23 | + private string $message = '', |
| 24 | + private int $priority = 10, |
| 25 | + ) { |
| 26 | + } |
| 27 | + |
| 28 | + public function __toString(): string |
| 29 | + { |
| 30 | + return $this->message; |
| 31 | + } |
| 32 | + |
| 33 | + public function append(string $message): self |
| 34 | + { |
| 35 | + $this->message .= $message; |
| 36 | + |
| 37 | + return $this; |
| 38 | + } |
| 39 | + |
| 40 | + public function appendLine(string $message): self |
| 41 | + { |
| 42 | + $this->message .= PHP_EOL . $message; |
| 43 | + |
| 44 | + return $this; |
| 45 | + } |
| 46 | + |
| 47 | + public static function addCommandToConfig(string $fqcn): self |
| 48 | + { |
| 49 | + return (new self('add to ', self::ADD_COMMAND_TO_CONFIG)) |
| 50 | + ->append( |
| 51 | + ColorEnum::colorize('config/autoload/cli.global.php', ColorEnum::ForegroundBrightWhite) |
| 52 | + ) |
| 53 | + ->append(' under ') |
| 54 | + ->append( |
| 55 | + ColorEnum::colorize('dot_cli', ColorEnum::ForegroundBrightWhite) |
| 56 | + ) |
| 57 | + ->append('.') |
| 58 | + ->append( |
| 59 | + ColorEnum::colorize('commands', ColorEnum::ForegroundBrightWhite) |
| 60 | + ) |
| 61 | + ->append(':') |
| 62 | + ->appendLine( |
| 63 | + ColorEnum::colorize( |
| 64 | + sprintf(' %s::getDefaultName() => %s::class,', $fqcn, $fqcn), |
| 65 | + ColorEnum::ForegroundBrightYellow |
| 66 | + ) |
| 67 | + ); |
| 68 | + } |
| 69 | + |
| 70 | + public static function addConfigProviderToConfig(string $fqcn): self |
| 71 | + { |
| 72 | + return (new self('add to ', self::ADD_CONFIG_PROVIDER_TO_CONFIG)) |
| 73 | + ->append( |
| 74 | + ColorEnum::colorize('config/config.php', ColorEnum::ForegroundBrightWhite) |
| 75 | + ) |
| 76 | + ->append(':') |
| 77 | + ->appendLine( |
| 78 | + ColorEnum::colorize(sprintf(' %s,', $fqcn), ColorEnum::ForegroundBrightYellow) |
| 79 | + ); |
| 80 | + } |
| 81 | + |
| 82 | + public static function addCoreConfigProviderToConfig(string $fqcn): self |
| 83 | + { |
| 84 | + return self::addConfigProviderToConfig($fqcn)->setPriority(self::ADD_CORE_CONFIG_PROVIDER_TO_CONFIG); |
| 85 | + } |
| 86 | + |
| 87 | + public static function addModuleToComposer(string $rootNamespace, string $moduleName): self |
| 88 | + { |
| 89 | + return (new self('add to ', self::ADD_MODULE_TO_COMPOSER)) |
| 90 | + ->append( |
| 91 | + ColorEnum::colorize('composer.json', ColorEnum::ForegroundBrightWhite) |
| 92 | + ) |
| 93 | + ->append(' under ') |
| 94 | + ->append( |
| 95 | + ColorEnum::colorize('autoload', ColorEnum::ForegroundBrightWhite) |
| 96 | + ) |
| 97 | + ->append('.') |
| 98 | + ->append( |
| 99 | + ColorEnum::colorize('psr-4', ColorEnum::ForegroundBrightWhite) |
| 100 | + ) |
| 101 | + ->append(':') |
| 102 | + ->appendLine( |
| 103 | + ColorEnum::colorize( |
| 104 | + sprintf(' "%s\\\\%s\\\\": "src/%s/src/"', $rootNamespace, $moduleName, $moduleName), |
| 105 | + ColorEnum::ForegroundBrightYellow |
| 106 | + ) |
| 107 | + ); |
| 108 | + } |
| 109 | + |
| 110 | + public static function addCoreModuleToComposer(string $moduleName): self |
| 111 | + { |
| 112 | + return (new self('add to ', self::ADD_CORE_MODULE_TO_COMPOSER)) |
| 113 | + ->append( |
| 114 | + ColorEnum::colorize('composer.json', ColorEnum::ForegroundBrightWhite) |
| 115 | + ) |
| 116 | + ->append(' under ') |
| 117 | + ->append( |
| 118 | + ColorEnum::colorize('autoload', ColorEnum::ForegroundBrightWhite) |
| 119 | + ) |
| 120 | + ->append('.') |
| 121 | + ->append( |
| 122 | + ColorEnum::colorize('psr-4', ColorEnum::ForegroundBrightWhite) |
| 123 | + ) |
| 124 | + ->append(':') |
| 125 | + ->appendLine( |
| 126 | + ColorEnum::colorize( |
| 127 | + sprintf( |
| 128 | + ' "%s\\\\%s\\\\": "src/%s/src/%s/src/"', |
| 129 | + ContextInterface::NAMESPACE_CORE, |
| 130 | + $moduleName, |
| 131 | + ContextInterface::NAMESPACE_CORE, |
| 132 | + $moduleName |
| 133 | + ), |
| 134 | + ColorEnum::ForegroundBrightYellow |
| 135 | + ) |
| 136 | + ); |
| 137 | + } |
| 138 | + |
| 139 | + public static function addMiddlewareToPipeline(string $fqcn): self |
| 140 | + { |
| 141 | + return (new self('add to ', self::ADD_MIDDLEWARE_TO_PIPELINE)) |
| 142 | + ->append( |
| 143 | + ColorEnum::colorize('config/pipeline.php', ColorEnum::ForegroundBrightWhite) |
| 144 | + ) |
| 145 | + ->append(':') |
| 146 | + ->appendLine( |
| 147 | + ColorEnum::colorize(sprintf(' $app->pipe(%s);', $fqcn), ColorEnum::ForegroundBrightYellow) |
| 148 | + ); |
| 149 | + } |
| 150 | + |
| 151 | + public static function dumpComposerAutoloader(): self |
| 152 | + { |
| 153 | + return (new self('dump Composer autoloader by executing this command:')) |
| 154 | + ->setPriority(self::DUMP_COMPOSER_AUTOLOADER) |
| 155 | + ->appendLine(ColorEnum::colorize(' composer dump', ColorEnum::ForegroundBrightYellow)); |
| 156 | + } |
| 157 | + |
| 158 | + public static function generateMigration(): self |
| 159 | + { |
| 160 | + return (new self('generate Doctrine migration:')) |
| 161 | + ->setPriority(self::GENERATE_MIGRATION) |
| 162 | + ->appendLine( |
| 163 | + ColorEnum::colorize(' php ./vendor/bin/doctrine-migrations diff', ColorEnum::ForegroundBrightYellow) |
| 164 | + ); |
| 165 | + } |
| 166 | + |
| 167 | + public function getPriority(): int |
| 168 | + { |
| 169 | + return $this->priority; |
| 170 | + } |
| 171 | + |
| 172 | + public function setPriority(int $priority): self |
| 173 | + { |
| 174 | + $this->priority = $priority; |
| 175 | + |
| 176 | + return $this; |
| 177 | + } |
| 178 | +} |
0 commit comments