Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 67 additions & 0 deletions bin/console
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
#!/usr/bin/php
<?php

$commands = [
'make' => [
'controller',
'middleware',
]
];

$commandOption = null;
foreach ($argv as $arg) {
foreach (array_keys($commands) as $command) {
if (strpos($arg, $command . ':') === 0) {
$commandOption = explode($command . ':', $arg)[1];
break;
}
}
}

if (!$commandOption || !in_array($commandOption, $commands[$command])) {
echo 'Options:' . PHP_EOL;
echo ' make:controller Create a new Controller' . PHP_EOL;
echo ' make:middleware Create a new Middleware' . PHP_EOL;

exit;
}

if ($command == 'make') {
$defaulDirectory = explode('/vendor/', getcwd())[0] . '/' . ucfirst($commandOption) . 's';
$directory = readline('Enter the target directory [' . $defaulDirectory . ']:') ?: $defaulDirectory;

if (!is_dir($directory)) {
echo PHP_EOL . 'ERROR: ' . 'The specified directory does not exist!' . PHP_EOL;

exit;
}


$className = readline("Enter the {$commandOption} name:");

$defaulNamespace = ucfirst($commandOption) . 's';
$namespace = readline('Enter the target namespace [' . $defaulNamespace . ']:') ?: $defaulNamespace;

try {
ob_start();
echo '<?php';
include __DIR__ . '/../src/Template/' . ucfirst($commandOption) . 'Template.php';
$content = ob_get_contents();
ob_end_clean();

$filePath = $directory . "/{$className}.php";

if (is_file($filePath)) {
throw new Exception("The {$commandOption} already exists!");
}

$file = fopen($filePath, 'w+');

file_put_contents($filePath, $content);
fclose($file);

echo PHP_EOL . 'SUCCESS!' . PHP_EOL;
} catch (\Exception $exception) {
echo PHP_EOL . 'ERROR: ' . $exception->getMessage() . PHP_EOL;
}
}
18 changes: 18 additions & 0 deletions src/Template/ControllerTemplate.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@


namespace <?= $namespace ?>;

/**
* Class <?= $className ?>

*
* @package <?= $namespace ?>

*/
class <?= $className ?>

{
public function main()
{
}
}
21 changes: 21 additions & 0 deletions src/Template/MiddlewareTemplate.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@


namespace <?= $namespace ?>;

/**
* Class <?= $className ?>

*
* @package <?= $namespace ?>

*/
class <?= $className ?>

{
public function handle()
{
// your middleware codes

return true;
}
}