|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Modulus\Console\Commands; |
| 4 | + |
| 5 | +use Modulus\Console\ModulusCLI; |
| 6 | +use AtlantisPHP\Console\Command; |
| 7 | +use Modulus\Scaffolding\Template; |
| 8 | +use Symfony\Component\Console\Input\InputInterface; |
| 9 | +use Symfony\Component\Console\Output\OutputInterface; |
| 10 | + |
| 11 | +class CraftMail extends Command |
| 12 | +{ |
| 13 | + /** |
| 14 | + * The name and signature of the console command. |
| 15 | + * |
| 16 | + * @var string |
| 17 | + */ |
| 18 | + protected $signature = 'craft:mail {name}'; |
| 19 | + |
| 20 | + /** |
| 21 | + * The full command description. |
| 22 | + * |
| 23 | + * @var string |
| 24 | + */ |
| 25 | + protected $help = 'This command allows you to create a mail'; |
| 26 | + |
| 27 | + /** |
| 28 | + * The descriptions of the console commands. |
| 29 | + * |
| 30 | + * @var array |
| 31 | + */ |
| 32 | + protected $descriptions = [ |
| 33 | + 'craft:mail' => 'Create a new mail', |
| 34 | + 'name' => 'The name of the class' |
| 35 | + ]; |
| 36 | + |
| 37 | + /** |
| 38 | + * @param InputInterface $input |
| 39 | + * @param OutputInterface $output |
| 40 | + * |
| 41 | + * @return void |
| 42 | + */ |
| 43 | + protected function execute(InputInterface $input, OutputInterface $output) |
| 44 | + { |
| 45 | + $name = $input->getArgument('name'); |
| 46 | + |
| 47 | + if ($this->add($name)) { |
| 48 | + return $output->writeln('<info>Mail "' . $name . '" has been successfully created.</info>'); |
| 49 | + } |
| 50 | + |
| 51 | + return $output->writeln('Mail "' . $name . '" already exists.'); |
| 52 | + } |
| 53 | + |
| 54 | + /** |
| 55 | + * Add asset |
| 56 | + * |
| 57 | + * @param string $name |
| 58 | + * @return boolean |
| 59 | + */ |
| 60 | + private function add(string $name) : bool |
| 61 | + { |
| 62 | + $notifications = ModulusCLI::$appdir . 'app' . DIRECTORY_SEPARATOR . 'Mail'; |
| 63 | + $notification = $notifications . DIRECTORY_SEPARATOR . $name . '.php'; |
| 64 | + $namespace = ''; |
| 65 | + |
| 66 | + if (substr_count($name, '/') > 0) { |
| 67 | + ModulusCLI::_dir(substr($notification, 0, strrpos($notification, DIRECTORY_SEPARATOR))); |
| 68 | + $namespace = substr($name, 0, strrpos($name, DIRECTORY_SEPARATOR)); |
| 69 | + $name = str_replace($namespace . DIRECTORY_SEPARATOR, '', $name); |
| 70 | + |
| 71 | + $namespace = '\\' . str_replace('/', '\\', $namespace); |
| 72 | + } |
| 73 | + |
| 74 | + ModulusCLI::_dir($notifications); |
| 75 | + |
| 76 | + $content = Template::asset('mail_template'); |
| 77 | + $content = str_replace('{mail_name}', $name, $content); |
| 78 | + $content = str_replace('{namespace}', $namespace, $content); |
| 79 | + |
| 80 | + if (file_exists($notification)) return false; |
| 81 | + |
| 82 | + file_put_contents($notification, $content); |
| 83 | + return true; |
| 84 | + } |
| 85 | +} |
0 commit comments