Skip to content

Commit adfc77b

Browse files
author
Ryuk
committed
Add cli for creating controllers and middlewares
1 parent 8c2095b commit adfc77b

8 files changed

+383
-1
lines changed

composer.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@
1313
}
1414
],
1515
"require": {
16-
"php": ">=5.5.0"
16+
"php": ">=5.5.0",
17+
"symfony/console": "^5.1"
1718
},
1819
"require-dev": {
1920
"phpunit/phpunit": "^4.8 || ^5.7 || ^6.5 || ^7.0",

src/Commands/ControllerTemplate.php

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

src/Commands/MiddlewareTemplate.php

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
2+
3+
namespace <?= $namespace ?>;
4+
5+
/**
6+
* Class <?= $className ?>
7+
8+
*
9+
* @package <?= $namespace ?>
10+
11+
*/
12+
class <?= $className ?>
13+
14+
{
15+
public function handle()
16+
{
17+
// your middleware codes
18+
19+
return true;
20+
}
21+
}

src/console

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#!/usr/bin/env php
2+
<?php
3+
4+
require __DIR__.'/../vendor/autoload.php';
5+
6+
use Symfony\Component\Console\Application;
7+
8+
$application = new Application();
9+
10+
$application->add(new \Buki\Commands\CreateControllerCommand());
11+
$application->add(new \Buki\Commands\CreateMiddlewareCommand());
12+
13+
$application->run();
14+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
<?php
2+
3+
namespace Buki\Tests\Commands;
4+
5+
use Buki\Commands\CreateControllerCommand;
6+
use Buki\Commands\Exception;
7+
use PHPUnit\Framework\TestCase;
8+
use Symfony\Component\Console\Application;
9+
use Symfony\Component\Console\Command\Command;
10+
use Symfony\Component\Console\Exception\RuntimeException;
11+
use Symfony\Component\Console\Tester\CommandTester;
12+
13+
/**
14+
* Class CreateControllerCommandTest
15+
*
16+
* @package Buki\Tests\Commands
17+
*/
18+
class CreateControllerCommandTest extends TestCase
19+
{
20+
/** @var CommandTester */
21+
private $commandTester;
22+
23+
protected function setUp()
24+
{
25+
$application = new Application();
26+
$application->add(new CreateControllerCommand());
27+
$command = $application->find('make:controller');
28+
$this->commandTester = new CommandTester($command);
29+
}
30+
31+
public function testExecute()
32+
{
33+
$this->commandTester->execute([
34+
'name' => 'TestController',
35+
'path' => __DIR__ . '/Controllers',
36+
'namespace' => 'Controllers',
37+
]);
38+
39+
$this->assertEquals(Command::SUCCESS, $this->commandTester->getStatusCode());
40+
}
41+
42+
public function testExecuteShouldThrowExceptionForEmptyName()
43+
{
44+
$this->expectException(RuntimeException::class);
45+
$this->expectExceptionMessage('Not enough arguments (missing: "name")');
46+
47+
$this->commandTester->execute([
48+
// name is required
49+
]);
50+
}
51+
52+
public function testExecuteShouldThrowExceptionForExistentController()
53+
{
54+
$this->expectException(\Exception::class);
55+
$this->expectExceptionMessage('The controller already exists!');
56+
57+
$this->commandTester->execute([
58+
'name' => 'TestController',
59+
'path' => __DIR__ . '/Controllers',
60+
'namespace' => 'Controllers',
61+
]);
62+
}
63+
64+
public function testExecuteShouldThrowExceptionForMkdirPermissionDenied()
65+
{
66+
$this->expectException(\Exception::class);
67+
$this->expectExceptionMessage('mkdir(): Permission denied');
68+
69+
$this->commandTester->execute([
70+
'name' => 'TestController',
71+
'path' => '/SomeDirectory',
72+
'namespace' => 'Controllers',
73+
]);
74+
}
75+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
<?php
2+
3+
namespace Buki\Tests\Commands;
4+
5+
use Buki\Commands\CreateMiddlewareCommand;
6+
use Buki\Commands\Exception;
7+
use PHPUnit\Framework\TestCase;
8+
use Symfony\Component\Console\Application;
9+
use Symfony\Component\Console\Command\Command;
10+
use Symfony\Component\Console\Exception\RuntimeException;
11+
use Symfony\Component\Console\Tester\CommandTester;
12+
13+
/**
14+
* Class CreateMiddlewareCommandTest
15+
*
16+
* @package Buki\Tests\Commands
17+
*/
18+
class CreateMiddlewareCommandTest extends TestCase
19+
{
20+
/** @var CommandTester */
21+
private $commandTester;
22+
23+
protected function setUp()
24+
{
25+
$application = new Application();
26+
$application->add(new CreateMiddlewareCommand());
27+
$command = $application->find('make:middleware');
28+
$this->commandTester = new CommandTester($command);
29+
}
30+
31+
public function testExecute()
32+
{
33+
$this->commandTester->execute([
34+
'name' => 'TestMiddleware',
35+
'path' => __DIR__ . '/Middlewares',
36+
'namespace' => 'Middlewares',
37+
]);
38+
39+
$this->assertEquals(Command::SUCCESS, $this->commandTester->getStatusCode());
40+
}
41+
42+
public function testExecuteShouldThrowExceptionForEmptyName()
43+
{
44+
$this->expectException(RuntimeException::class);
45+
$this->expectExceptionMessage('Not enough arguments (missing: "name")');
46+
47+
$this->commandTester->execute([
48+
// name is required
49+
]);
50+
}
51+
52+
public function testExecuteShouldThrowExceptionForExistentMiddleware()
53+
{
54+
$this->expectException(\Exception::class);
55+
$this->expectExceptionMessage('The middleware already exists!');
56+
57+
$this->commandTester->execute([
58+
'name' => 'TestMiddleware',
59+
'path' => __DIR__ . '/Middlewares',
60+
'namespace' => 'Middlewares',
61+
]);
62+
}
63+
64+
public function testExecuteShouldThrowExceptionForMkdirPermissionDenied()
65+
{
66+
$this->expectException(\Exception::class);
67+
$this->expectExceptionMessage('mkdir(): Permission denied');
68+
69+
$this->commandTester->execute([
70+
'name' => 'TestMiddleware',
71+
'path' => '/SomeDirectory',
72+
'namespace' => 'Middlewares',
73+
]);
74+
}
75+
}

0 commit comments

Comments
 (0)