/** * Executes the command. * * @param \Symfony\Component\Console\Input\InputInterface $input * @param \Symfony\Component\Console\Output\OutputInterface $output * @return object|\Symfony\Component\Console\Output\OutputInterface */ protected function execute(InputInterface $input, OutputInterface $output) { $config = Configuration::get(); $templates = str_replace('Commands', 'Templates', __DIR__); $contents = []; $generator = new ViewGenerator($this->describe); $data = ['{name}' => $input->getArgument('name'), '{pluralTitle}' => Inflector::ucwords(str_replace('_', ' ', Inflector::pluralize($input->getArgument('name')))), '{plural}' => Inflector::pluralize($input->getArgument('name')), '{singular}' => Inflector::singularize($input->getArgument('name'))]; $contents['create'] = $generator->generate($data, $templates, 'create'); $contents['edit'] = $generator->generate($data, $templates, 'edit'); $contents['index'] = $generator->generate($data, $templates, 'index'); $contents['show'] = $generator->generate($data, $templates, 'show'); $fileName = $config->folders->views . '/' . $data['{plural}'] . '/'; $layout = $config->folders->views . '/layouts/master.twig'; if (!$this->filesystem->has($layout)) { $template = file_get_contents($templates . '/Views/layout.twig'); $keywords = ['{application}' => $config->application->name]; $template = str_replace(array_keys($keywords), array_values($keywords), $template); $this->filesystem->write($layout, $template); } foreach ($contents as $type => $content) { $view = $fileName . $type . '.twig'; if ($this->filesystem->has($view)) { if (!$input->getOption('overwrite')) { return $output->writeln('<error>View already exists.</error>'); } else { $this->filesystem->delete($view); } } $this->filesystem->write($view, $content); } return $output->writeln('<info>View created successfully.</info>'); }
/** * Executes the command. * * @param \Symfony\Component\Console\Input\InputInterface $input * @param \Symfony\Component\Console\Output\OutputInterface $output * @return object|\Symfony\Component\Console\Output\OutputInterface */ protected function execute(InputInterface $input, OutputInterface $output) { $config = Configuration::get(); $slash = DIRECTORY_SEPARATOR; $directory = str_replace('Commands', 'Templates' . $slash . 'Application', __DIR__); $templates = Helpers::glob($directory . $slash . '*.*'); $appDirectory = $directory . $slash; $result = []; array_push($templates, $appDirectory . '.env'); array_push($templates, $appDirectory . 'public' . $slash . '.htaccess'); $data = ['application' => $config->application, 'author' => $config->author, 'database' => $config->database, 'directory' => $directory, 'files' => $config->files, 'folders' => $config->folders, 'namespaces' => (object) $config->namespaces]; Helpers::render($this->filesystem, $this->renderer, $templates, $data); // system('composer update'); $text = 'Application created successfully.'; return $output->writeln('<info>' . $text . '</info>'); }
/** * Generates route contents. * * @param string $routeContents * @param string $tableName */ public function generateRoute(&$routeContents, $tableName) { $config = Configuration::get(); $lines = preg_split("/\\r\\n|\\r|\\n/", $routeContents); $endBracket = $lines[count($lines) - 2]; if ($endBracket != '];') { $endBracket = $lines[count($lines) - 1]; } $template = $this->routesTemplate . $endBracket; $routeContents = str_replace($endBracket, $template, $routeContents); $keywords = []; $keywords['{pluralTitle}'] = Inflector::classify(Inflector::pluralize($tableName)); $keywords['{pluralTitleDescription}'] = Inflector::ucwords(str_replace('_', ' ', Inflector::pluralize($tableName))); $keywords['{plural}'] = Inflector::pluralize($tableName); $keywords['{application}'] = $config->application->name; $keywords['{namespace}'] = $config->namespaces->controllers; $routeContents = str_replace(array_keys($keywords), array_values($keywords), $routeContents); }
/** * Executes the command. * * @param \Symfony\Component\Console\Input\InputInterface $input * @param \Symfony\Component\Console\Output\OutputInterface $output * @return object|\Symfony\Component\Console\Output\OutputInterface */ protected function execute(InputInterface $input, OutputInterface $output) { $config = Configuration::get(); $data = ['application' => $config->application, 'author' => $config->author, 'model' => $input->getOption('model'), 'namespaces' => $config->namespaces, 'password' => $input->getOption('password'), 'plural' => lcfirst(Inflector::classify(Inflector::pluralize($input->getOption('model')))), 'singular' => lcfirst(Inflector::classify(Inflector::singularize($input->getOption('model')))), 'username' => $input->getOption('username')]; $controller = $this->renderer->render('Authentication/AuthenticationController.php', $data); $middleware = $this->renderer->render('Authentication/AuthenticateMiddleware.php', $data); $middlewares = $this->generateFile($config, 'middleware'); $routes = $this->generateFile($config, 'route'); $controllerFile = $config->folders->controllers . '/AuthenticationController.php'; $middlewareFile = $config->folders->middlewares . '/AuthenticateMiddleware.php'; if ($this->filesystem->has($controllerFile)) { if ($input->getOption('overwrite')) { $this->filesystem->delete($controllerFile); } else { $text = 'AuthenticationController.php already exists.'; return $output->writeln('<error>' . $text . '</error>'); } } if ($this->filesystem->has($middlewareFile)) { if ($input->getOption('overwrite')) { $this->filesystem->delete($middlewareFile); } else { $text = 'AuthenticateMiddleware.php already exists.'; return $output->writeln('<error>' . $text . '</error>'); } } $this->filesystem->write($controllerFile, $controller); $this->filesystem->write($middlewareFile, $middleware); if ($this->filesystem->has($config->files->routes)) { $this->filesystem->update($config->files->routes, $routes); } if ($this->filesystem->has($config->files->middlewares)) { $this->filesystem->update($config->files->middlewares, $middlewares); } $text = 'Basic authentication created successfully.'; return $output->writeln('<info>' . $text . '</info>'); }
/** * Returns the required data for model. * * @return void */ public function concat(array &$data) { $config = Configuration::get(); $columns = $this->describe->getTable($data['name']); $counter = 0; $foreignKeys = 0; $data['singular'] = lcfirst(Inflector::classify($data['singular'])); $data['createColumns'] = ''; $data['updateColumns'] = ''; foreach ($columns as $column) { if ($column->isForeignKey()) { $foreignKeys++; } } foreach ($columns as $column) { if (!$column->isForeignKey()) { continue; } $referencedTable = $this->stripTableSchema($column->getReferencedTable()); $template = $this->foreignVariableTemplate; $keywords = ['{application}' => $config->application->name, '{models}' => $config->namespaces->models, '{name}' => $column->getField(), '{referencedTable}' => $referencedTable, '{table}' => ucfirst($referencedTable)]; $template = str_replace(array_keys($keywords), array_values($keywords), $template); $data['createColumns'] .= $template; $data['updateColumns'] .= $template; if ($counter < $foreignKeys - 1) { $data['createColumns'] .= ' '; $data['updateColumns'] .= ' '; } $counter++; } if ($data['createColumns'] != '') { $data['createColumns'] .= "\n "; $data['updateColumns'] .= "\n "; } $counter = 0; foreach ($columns as $column) { if ($column->isPrimaryKey()) { continue; } $keywords = ['{name}' => $column->getField(), '{mutatorName}' => Inflector::camelize('set_' . $column->getField()), '{singular}' => lcfirst($data['singularTitle'])]; $template = $this->mutatorMethodTemplate; if ($column->isForeignKey()) { $referencedTable = $this->stripTableSchema($column->getReferencedTable()); $template = $this->foreignMutatorMethodTemplate; $keywords['{table}'] = lcfirst(Inflector::classify($referencedTable)); $keywords['{mutatorName}'] = Inflector::camelize('set_' . $referencedTable); } if ($column->getField() == 'datetime_created' || $column->getField() == 'datetime_updated') { $template = str_replace('$data[\'{name}\']', "'now'", $template); } else { if ($column->getDataType() == 'integer' && $column->getLength() == 1) { $template = str_replace('$data[\'{name}\']', 'isset($data[\'{name}\'])', $template); } else { if ($column->getField() == 'password') { $template = str_replace('$data[\'{name}\']', 'md5($data[\'{name}\'])', $template); } else { if ($column->isNull()) { $template = str_replace('$data[\'{name}\']', 'isset($data[\'{name}\']) ? $data[\'{name}\'] : null', $template); } } } } $template = str_replace(array_keys($keywords), array_values($keywords), $template); if ($column->getField() != 'datetime_updated' && strpos($column->getDataType(), 'blob') === false) { $data['createColumns'] .= $template; } if ($column->getField() != 'datetime_created' && $column->getField() != 'password' && strpos($column->getDataType(), 'blob') === false) { $data['updateColumns'] .= $template; } if ($counter < count($columns) - 1) { if ($column->getField() != 'datetime_updated' && strpos($column->getDataType(), 'blob') === false) { $data['createColumns'] .= ' '; } if ($column->getField() != 'datetime_created' && $column->getField() != 'password' && strpos($column->getDataType(), 'blob') === false) { $data['updateColumns'] .= ' '; } } $counter++; } $data['createColumns'] = trim($data['createColumns']); $data['updateColumns'] = trim($data['updateColumns']); foreach ($columns as $column) { if ($column->getField() != 'password' && strpos($column->getDataType(), 'blob') === false) { continue; } if ($column->getField() == 'password') { $table = lcfirst(Inflector::classify($data['singular'])); $mutator = Inflector::camelize('set_' . $column->getField()); $data['updateColumns'] .= "\n\n " . 'if ($data[\'' . $column->getField() . '\']) {' . "\n " . '$' . $table . '->' . $mutator . '(md5($data[\'' . $column->getField() . '\']));' . "\n " . '}'; } if (strpos($column->getDataType(), 'blob') !== false) { $table = lcfirst(Inflector::classify($data['singular'])); $mutator = Inflector::camelize('set_' . $column->getField()); $code = "\n\n " . 'if (! $data[\'' . $column->getField() . '\']->getError()) {' . "\n " . '$' . $column->getField() . ' = $data[\'' . $column->getField() . '\']->getStream()->getContents();' . "\n\n " . '$' . $table . '->' . $mutator . '($' . $column->getField() . ');' . "\n " . '}'; $data['createColumns'] .= $code; $data['updateColumns'] .= $code; } } }
/** * Configures the current command. * * @return void */ protected function configure() { $config = Configuration::get(); $this->setName('make:mvc')->setDescription('Creates a new controller, model, view, repository and validator')->addArgument('name', InputArgument::REQUIRED, 'Base table name for the files')->addOption('overwrite', null, InputOption::VALUE_NONE, 'Overwrite the specified files'); }
/** * Executes the command. * * @param \Symfony\Component\Console\Input\InputInterface $input * @param \Symfony\Component\Console\Output\OutputInterface $output * @return object|\Symfony\Component\Console\Output\OutputInterface */ protected function execute(InputInterface $input, OutputInterface $output) { $config = Configuration::get(); $name = $input->getArgument('name'); $type = Inflector::pluralize($this->type); $data = ['application' => $config->application, 'author' => $config->author, 'foreignClasses' => '', 'name' => $input->getArgument('name'), 'namespaces' => $config->namespaces, 'nameTitle' => Inflector::classify($name), 'plural' => Inflector::pluralize($name), 'pluralTitle' => Inflector::classify(Inflector::pluralize($name)), 'singular' => Inflector::singularize($name), 'singularTitle' => Inflector::classify(Inflector::singularize($name))]; $directory = $config->folders->{$type}; $type = ucfirst($this->type); $item = ucfirst(Inflector::pluralize($name) . $type); switch ($this->type) { case 'component': $item = ucfirst($name . $type); break; case 'model': $item = ucfirst($data['singular']); break; case 'middleware': case 'repository': case 'validator': $item = ucfirst(Inflector::singularize($name) . $type); break; } $fileName = $directory . '/' . Inflector::classify($item) . '.php'; switch ($this->type) { case 'controller': $generator = new ControllerGenerator($this->describe); $routes = $this->filesystem->read($config->files->routes); $generator->concat($data); $generator->generateRoute($routes, $input->getArgument('name')); if (!$this->filesystem->has($fileName)) { if ($this->filesystem->has($config->files->routes)) { $this->filesystem->update($config->files->routes, $routes); } else { $this->filesystem->write($config->files->routes, $routes); } } break; case 'model': $generator = new ModelGenerator($this->describe); $generator->concat($data); break; case 'repository': $generator = new RepositoryGenerator($this->describe); $generator->concat($data); break; case 'validator': $generator = new ValidatorGenerator($this->describe); $generator->concat($data); $validator = str_replace(Inflector::classify($item), 'BaseValidator', $fileName); if (!$this->filesystem->has($validator)) { $content = $this->renderer->render('BaseValidator.php', $data); $this->filesystem->write($validator, $content); } break; } $content = $this->renderer->render($this->type . '.php', $data); if ($this->filesystem->has($fileName)) { if (!$input->getOption('overwrite')) { $text = ucfirst($this->type) . ' already exists.'; return $output->writeln('<error>' . $text . '</error>'); } else { $this->filesystem->delete($fileName); } } $this->filesystem->write($fileName, $content); $text = ucfirst($this->type) . ' created successfully.'; return $output->writeln('<info>' . $text . '</info>'); }
/** * Sets specified fields for foreign key values. * * @param \Rougin\Describe\Column $column */ public function setForeignKey(Column $column, array &$data, array &$keywords) { if (!$column->isForeignKey()) { return; } $config = Configuration::get(); $template = $this->foreignColumnTemplate; $data['methods'] .= "\n " . $this->mutatorMethodTemplate . "\n "; $data['methods'] .= $this->accessorMethodTemplate; $referencedTable = $this->stripTableSchema($column->getReferencedTable()); $keywords['{accessorName}'] = Inflector::camelize('get_' . $referencedTable); $keywords['{mutatorName}'] = Inflector::camelize('set_' . $referencedTable); $keywords['{primaryKey}'] = $this->describe->getPrimaryKey($referencedTable); $keywords['{variable}'] = $referencedTable; $keywords['{referencedTable}'] = ucfirst($referencedTable); $keywords['{description}'] = $referencedTable; $keywords['{class}'] = ucfirst($referencedTable) . ' '; $keywords['{datatype}'] = '\\' . $config->application->name . '\\' . $config->namespaces->models . '\\' . ucfirst($referencedTable); $template = str_replace(array_keys($keywords), array_values($keywords), $template); $data['methods'] = str_replace(array_keys($keywords), array_values($keywords), $data['methods']); $data['columns'] .= $template; }