arg(0)); if ($raw === '') { $this->fail('Укажите имя: php bin/proton ' . $this->name() . ' Имя'); return 1; } $class = Str::studly($raw); $suffix = $this->suffix(); if ($suffix !== '' && !str_ends_with($class, $suffix)) { $class .= $suffix; } $path = APP_ROOT . '/' . trim($this->directory(), '/') . '/' . $class . '.php'; if (is_file($path)) { // Существующий файл не трогаем никогда: генератор не должен затирать код $this->fail('Уже есть — ' . $this->what() . ': ' . $this->relative($path)); return 1; } $content = $this->render($class); $dir = dirname($path); if (!is_dir($dir) && !@mkdir($dir, 0775, true) && !is_dir($dir)) { $this->fail('Не удалось создать каталог ' . $dir); return 1; } if (@file_put_contents($path, $content) === false) { $this->fail('Не удалось записать файл ' . $path); return 1; } $this->ok('Создано — ' . $this->what() . ': ' . $this->relative($path)); $this->after($class, $path); return 0; } /** * Что напечатать после создания — подсказка про реестр, миграцию и прочее. */ protected function after(string $class, string $path): void { } /** * Подстановки в заготовке: {{class}}, {{table}} и так далее. * * @return array */ protected function replacements(string $class): array { return [ '{{class}}' => $class, '{{table}}' => Str::plural(Str::snake($class)), '{{slug}}' => Str::snake($class), ]; } private function render(string $class): string { $stub = APP_ROOT . '/stubs/' . $this->stub(); if (!is_file($stub)) { return "replacements($class)); } protected function relative(string $path): string { return str_replace(APP_ROOT . '/', '', str_replace('\\', '/', $path)); } }