-
-
Notifications
You must be signed in to change notification settings - Fork 47
/
Copy pathCreateMiddlewareCommand.php
89 lines (73 loc) · 2.54 KB
/
CreateMiddlewareCommand.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
<?php
namespace Buki\Commands;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
/**
* Class CreateMiddlewareCommand
*
* @package Buki\Commands
*/
class CreateMiddlewareCommand extends Command
{
protected static $defaultName = 'make:middleware';
protected function configure()
{
$this->setDescription('Create a new Middleware');
$this->addArgument('name', InputArgument::REQUIRED, 'Name of the middleware');
$this->addArgument('path', InputArgument::OPTIONAL, 'Absolute path of the middleware\'s directory');
$this->addArgument('namespace', InputArgument::OPTIONAL, 'Namespace of the middleware directory');
}
protected function execute(InputInterface $input, OutputInterface $output)
{
$className = $input->getArgument('name');
if (!is_dir($directory = $input->getArgument('path'))) {
if (is_null($directory)) {
$directory = explode('/vendor/', getcwd())[0] . '/Middlewares';
$output->writeln([
'',
'Default directory is: ' . $directory,
]);
} else {
try {
mkdir($directory);
} catch (\Exception $exception) {
throw new \Exception($exception->getMessage());
}
}
}
if (is_null($namespace = $input->getArgument('namespace'))) {
$namespace = 'Middlewares';
$output->writeln([
'',
'Default namespace is: ' . $namespace,
]);
}
$filePath = $directory . "/{$className}.php";
if (is_file($filePath)) {
throw new \Exception('The middleware already exists!');
}
try {
ob_start();
echo '<?php';
include __DIR__ . '/MiddlewareTemplate.php';
$content = ob_get_contents();
ob_end_clean();
$file = fopen($filePath, 'w+');
file_put_contents($filePath, $content);
fclose($file);
$output->writeln([
'',
'SUCCESS!',
]);
return Command::SUCCESS;
} catch (\Exception $exception) {
$output->writeln([
'',
'ERROR: ' . $exception->getMessage(),
]);
return Command::FAILURE;
}
}
}