|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * The file contains Maker trait |
| 4 | + * |
| 5 | + * PHP version 5 |
| 6 | + * |
| 7 | + * @category Library |
| 8 | + * @package Cherry |
| 9 | + * @author Temuri Takalandze <[email protected]> |
| 10 | + * @license https://github.com/cherry-framework/console/blob/master/LICENSE MIT |
| 11 | + * @link https://github.com/cherry-framework/console |
| 12 | + */ |
| 13 | + |
| 14 | +namespace Cherry\Console\Command; |
| 15 | + |
| 16 | +use Cherry\Console\Input\ArgvInput; |
| 17 | +use Cherry\Console\Output\Output; |
| 18 | + |
| 19 | +/** |
| 20 | + * Maker Trait for Cherry Console. |
| 21 | + * |
| 22 | + * @category Library |
| 23 | + * @package Cherry |
| 24 | + * @author Temuri Takalandze <[email protected]> |
| 25 | + * @license https://github.com/cherry-framework/console/blob/master/LICENSE MIT |
| 26 | + * @link https://github.com/cherry-framework/console |
| 27 | + */ |
| 28 | +trait Maker |
| 29 | +{ |
| 30 | + private $_templatesPath = __DIR__.'/Maker/Templates'; |
| 31 | + |
| 32 | + /** |
| 33 | + * Run Cherry Features maker. |
| 34 | + * |
| 35 | + * @param ArgvInput $input CLI Input interface |
| 36 | + * @param Output $output CLI Output interface |
| 37 | + * |
| 38 | + * @return void |
| 39 | + */ |
| 40 | + private function _make(ArgvInput $input, Output $output) |
| 41 | + { |
| 42 | + $argv = $input->getArgv(); |
| 43 | + |
| 44 | + if ($input->getArgvCount() == 1) { |
| 45 | + $this->_makeHelp($output); |
| 46 | + } else { |
| 47 | + $this->_callMakerMethod($argv[1], $input, $output); |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + /** |
| 52 | + * Call maker by argument. |
| 53 | + * |
| 54 | + * @param string $method Method for calling |
| 55 | + * @param ArgvInput $input CLI Input interface |
| 56 | + * @param Output $output CLI Output interface |
| 57 | + * |
| 58 | + * @return void |
| 59 | + */ |
| 60 | + private function _callMakerMethod($method, ArgvInput $input, Output $output) |
| 61 | + { |
| 62 | + $method = '_make'.ucfirst($method); |
| 63 | + |
| 64 | + if (method_exists($this, $method)) { |
| 65 | + $this->{$method}($input, $output); |
| 66 | + } else { |
| 67 | + $this->_makeHelp($output); |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + /** |
| 72 | + * Get Maker help |
| 73 | + * |
| 74 | + * @param Output $output CLI Output interface |
| 75 | + * |
| 76 | + * @return void |
| 77 | + */ |
| 78 | + private function _makeHelp(Output $output) |
| 79 | + { |
| 80 | + $help = file_get_contents(__DIR__.'/Maker/Docs/help.txt'); |
| 81 | + print $output->text($help); |
| 82 | + } |
| 83 | + |
| 84 | + /** |
| 85 | + * Make new Cherry Controller. |
| 86 | + * |
| 87 | + * @param ArgvInput $input CLI Input interface |
| 88 | + * @param Output $output CLI Output interface |
| 89 | + * |
| 90 | + * @return void |
| 91 | + */ |
| 92 | + private function _makeController(ArgvInput $input, Output $output) |
| 93 | + { |
| 94 | + print $output->success('Make Controller.')."\n"; |
| 95 | + print $output->text('Enter Controller Name [hello]:'); |
| 96 | + |
| 97 | + //Get controller name from stdin |
| 98 | + $controllerTitle = readline() ?: 'hello'; |
| 99 | + |
| 100 | + // Remove spaces |
| 101 | + $controllerTitle = trim($controllerTitle); |
| 102 | + |
| 103 | + // If empty, set default value |
| 104 | + $controllerTitle = $controllerTitle == '' ? 'hello' : $controllerTitle; |
| 105 | + |
| 106 | + // Generate Controller name |
| 107 | + $controllerName = ucfirst($controllerTitle).'Controller'; |
| 108 | + |
| 109 | + // Check if controller exists |
| 110 | + if (file_exists(CONTROLLERS_PATH.'/'.$controllerName.'.php')) { |
| 111 | + print "\n".$output |
| 112 | + ->warning("Controller {$controllerTitle} already exists!"); |
| 113 | + } else { |
| 114 | + $templatesPath = $this->_templatesPath . '/Controller/'; |
| 115 | + |
| 116 | + // Get templates |
| 117 | + $controllerTemplate = file_get_contents( |
| 118 | + $templatesPath . '/controller.txt' |
| 119 | + ); |
| 120 | + $templateTemplate = file_get_contents($templatesPath . '/template.txt'); |
| 121 | + |
| 122 | + // Replace controller name in templates |
| 123 | + $controllerTemplate = str_replace( |
| 124 | + ['{controllerName}', '{controllerTitle}'], |
| 125 | + [$controllerName, $controllerTitle], |
| 126 | + $controllerTemplate |
| 127 | + ); |
| 128 | + $templateTemplate = str_replace( |
| 129 | + '{controllerName}', |
| 130 | + $controllerName, |
| 131 | + $templateTemplate |
| 132 | + ); |
| 133 | + |
| 134 | + // Create directories if they not found |
| 135 | + if (!file_exists(CONTROLLERS_PATH)) { |
| 136 | + mkdir(CONTROLLERS_PATH, 0755, true); |
| 137 | + } |
| 138 | + if (!file_exists(TEMPLATES_PATH . '/' . $controllerTitle)) { |
| 139 | + mkdir(TEMPLATES_PATH . '/' . $controllerTitle, 0755, true); |
| 140 | + } |
| 141 | + |
| 142 | + // Write to files |
| 143 | + file_put_contents( |
| 144 | + CONTROLLERS_PATH . '/' . $controllerName . '.php', |
| 145 | + $controllerTemplate |
| 146 | + ); |
| 147 | + file_put_contents( |
| 148 | + TEMPLATES_PATH . '/' . $controllerTitle . '/index.templater.php', |
| 149 | + $templateTemplate |
| 150 | + ); |
| 151 | + |
| 152 | + // Add route for new controller |
| 153 | + |
| 154 | + // Get old routes |
| 155 | + $routes = file_get_contents(ROUTES_FILE); |
| 156 | + $routes = json_decode($routes, 1); |
| 157 | + |
| 158 | + // Add new route |
| 159 | + $routes[$controllerTitle] = array( |
| 160 | + 'path' => '/' . $controllerTitle, |
| 161 | + 'method' => 'GET', |
| 162 | + 'action' => 'Cherry\Controller\\' . $controllerName . '::hello' |
| 163 | + ); |
| 164 | + |
| 165 | + // Save routes |
| 166 | + file_put_contents( |
| 167 | + ROUTES_FILE, |
| 168 | + json_encode($routes, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES) |
| 169 | + ); |
| 170 | + |
| 171 | + print "\n" . $output |
| 172 | + ->success("Controller {$controllerTitle} created successfully!"); |
| 173 | + } |
| 174 | + } |
| 175 | +} |
0 commit comments