Skip to content

Commit 9144d1b

Browse files
committed
Merge pull request #1 from nek-v/master
Change PSR
2 parents 67e2888 + 4960ee9 commit 9144d1b

File tree

6 files changed

+271
-6
lines changed

6 files changed

+271
-6
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
/nbproject
2+
/vendor

README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ It is based on a [Symfony Console Component](http://symfony.com/doc/master/compo
77
Depends:
88
* [symfony/console](https://github.com/symfony/Console)
99
* [symfony/finder](https://github.com/symfony/Finder)
10-
* [suncat/symfony-console-extra](https://github.com/suncat2000/symfony-console-extra)
1110

1211
Installation
1312
================

app/console

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ set_time_limit(0);
99

1010
require __DIR__.'/../vendor/autoload.php';
1111

12-
use SunCat\Console\ApplicationExtra;
12+
use Console\ApplicationExtra;
1313

1414
$application = new ApplicationExtra(__DIR__.'/../src/Command/');
1515
$application->run();

composer.json

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,18 @@
77
"email": "[email protected]"
88
}
99
],
10+
"minimum-stability": "stable",
1011
"autoload": {
11-
"psr-0": { "": "src/" }
12+
"psr-4": { "": "src/" }
1213
},
1314
"require": {
14-
"php": ">=5.3.3",
15-
"suncat/symfony-console-extra": "1.0.*@dev"
15+
"php": ">=5.4",
16+
"symfony/console": "~2.2",
17+
"symfony/finder": "~2.2"
1618
},
1719
"extra": {
1820
"branch-alias": {
19-
"dev-master": "1.0.x-dev"
21+
"dev-master": "2.0.x"
2022
}
2123
}
2224
}

src/Console/ApplicationExtra.php

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
<?php
2+
3+
namespace Console;
4+
5+
use Symfony\Component\Console\Application;
6+
use Symfony\Component\Finder\Finder;
7+
8+
use Console\Command\GenerateCommand;
9+
10+
class ApplicationExtra extends Application
11+
{
12+
13+
private $logo = ' ______ __ _______ __
14+
/ ____/___ ____ ____ ___ / / ___ /__ __/___ ___ / /
15+
/ / / __ \/ __ \/ ___/ __ \/ / / _ \ / / / __ \/ __ \/ /
16+
/ /___/ /_/ / / / (__ )/ /_/ / /__/ __/ / / / /_/ / /_/ / /__
17+
\____/\____/_/ /_/____/\____/____/\___/ /_/ \____/\____/____/
18+
';
19+
protected $commandsDir;
20+
21+
/**
22+
* Constructor.
23+
*
24+
* @param string $commandsDir The commands class directory
25+
* @param string $name The name of the application
26+
* @param string $version The version of the application
27+
*
28+
* @api
29+
*/
30+
public function __construct($commandsDir, $name = 'UNKNOWN', $version = 'UNKNOWN')
31+
{
32+
if (!is_dir($commandsDir)) {
33+
throw new \Exception('First argument is not directory!');
34+
}
35+
36+
parent::__construct($name, $version);
37+
$this->commandsDir = $commandsDir;
38+
// register commands
39+
$this->registerCommands();
40+
}
41+
42+
/**
43+
* Register commands
44+
*/
45+
public function registerCommands()
46+
{
47+
$finder = new Finder();
48+
$finder->files()->name('*Command.php')->in($this->commandsDir);
49+
50+
$prefix = 'Command';
51+
foreach ($finder as $file) {
52+
$ns = $prefix;
53+
/* @var \Symfony\Component\Finder\SplFileInfo $file*/
54+
if ($relativePath = $file->getRelativePath()) {
55+
$ns .= '\\'.strtr($relativePath, '/', '\\');
56+
}
57+
$r = new \ReflectionClass($ns.'\\'.$file->getBasename('.php'));
58+
if ($r->isSubclassOf('Symfony\\Component\\Console\\Command\\Command') && !$r->isAbstract()) {
59+
$this->add($r->newInstance());
60+
}
61+
}
62+
}
63+
64+
/**
65+
* Get commands class dir
66+
*
67+
* @return string
68+
*/
69+
public function getCommandsDir()
70+
{
71+
return $this->commandsDir;
72+
}
73+
74+
75+
/**
76+
* Returns the long version of the application.
77+
*
78+
* @return string The long application version
79+
*
80+
* @api
81+
*/
82+
public function getLongVersion()
83+
{
84+
if ('UNKNOWN' !== $this->getName() && 'UNKNOWN' !== $this->getVersion()) {
85+
return sprintf('<info>%s</info> version <comment>%s</comment>', $this->getName(), $this->getVersion());
86+
}
87+
88+
return '<info>' . $this->logo . '</info>';
89+
}
90+
91+
/**
92+
* Gets the default commands that should always be available.
93+
*
94+
* @return array An array of default Command instances
95+
*/
96+
protected function getDefaultCommands()
97+
{
98+
$defaultCommands = parent::getDefaultCommands();
99+
$defaultCommands[] = new GenerateCommand();
100+
101+
return $defaultCommands;
102+
}
103+
}
Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
1+
<?php
2+
3+
namespace Console\Command;
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\Input\InputOption;
9+
use Symfony\Component\Console\Output\OutputInterface;
10+
11+
class GenerateCommand extends Command
12+
{
13+
private $template = '<?php
14+
15+
namespace Command;
16+
17+
use Symfony\Component\Console\Command\Command;
18+
use Symfony\Component\Console\Input\InputArgument;
19+
use Symfony\Component\Console\Input\InputInterface;
20+
use Symfony\Component\Console\Input\InputOption;
21+
use Symfony\Component\Console\Output\OutputInterface;
22+
23+
/**
24+
* <class>
25+
*/
26+
class <class> extends Command
27+
{
28+
/**
29+
* Configuration of command
30+
*/
31+
protected function configure()
32+
{
33+
$this
34+
->setName("<name>")
35+
->setDescription("Command <name>")
36+
;
37+
}
38+
39+
/**
40+
* Execute method of command
41+
*
42+
* @param InputInterface $input
43+
* @param OutputInterface $output
44+
*
45+
* @return int|null|void
46+
*/
47+
protected function execute(InputInterface $input, OutputInterface $output)
48+
{
49+
$output->writeln(array("","<info>Execute</info>",""));
50+
}
51+
}';
52+
53+
/**
54+
* Configuration of command
55+
*/
56+
protected function configure()
57+
{
58+
$this
59+
->setName('generate')
60+
->setDescription('Generate skeleton class for new command')
61+
->setHelp(<<<EOF
62+
The <info>generate</info> command create skeleton of new command class.
63+
64+
<info>php app/console generate</info>
65+
EOF
66+
);
67+
;
68+
}
69+
70+
/**
71+
* Execute method of command
72+
*
73+
* @param InputInterface $input
74+
* @param OutputInterface $output
75+
*
76+
* @return int|null|void
77+
*/
78+
protected function execute(InputInterface $input, OutputInterface $output)
79+
{
80+
$output->writeln(array(
81+
'',
82+
'<comment>Welcome to the command generator</comment>',
83+
''
84+
));
85+
/* @var \Symfony\Component\Console\Helper\DialogHelper $dialog */
86+
$dialog = $this->getHelperSet()->get('dialog');
87+
$commandClass = $dialog->askAndValidate(
88+
$output,
89+
"<info>Please enter the name of the command class</info>:",
90+
function ($answer) {
91+
if ('Command' !== substr($answer, -7)) {
92+
throw new \RunTimeException(
93+
'The name of the command should be suffixed with \'Command\''
94+
);
95+
}
96+
return $answer;
97+
},
98+
false,
99+
'DefaultCommand'
100+
);
101+
102+
//
103+
$commandName = $this->colonize($commandClass);
104+
105+
$path = $this->generateCommand($commandClass, $commandName);
106+
$output->writeln(sprintf('Generated new command class to "<info>%s</info>"', realpath($path)));
107+
}
108+
109+
/**
110+
* Generate command skeleton
111+
*
112+
* @param $commandClass
113+
* @param $commandName
114+
*
115+
* @return string
116+
*
117+
* @throws \Exception
118+
*/
119+
protected function generateCommand($commandClass, $commandName)
120+
{
121+
$placeHolders = array(
122+
'<class>',
123+
'<name>'
124+
);
125+
$replacements = array(
126+
$commandClass,
127+
$commandName
128+
);
129+
$code = str_replace($placeHolders, $replacements, $this->template);
130+
/* @var \Console\ApplicationExtra $app*/
131+
$app = $this->getApplication();
132+
$dir = $app->getCommandsDir();
133+
$dir = rtrim($dir, '/');
134+
$path = $dir . '/'.$commandClass.'.php';
135+
136+
if (!file_exists($dir)) {
137+
throw new \Exception(sprintf('Commands directory "%s" does not exist.', $dir));
138+
}
139+
140+
file_put_contents($path, $code);
141+
142+
return $path;
143+
}
144+
145+
/**
146+
* Colonize command name
147+
*
148+
* @param $word
149+
* @return string
150+
*/
151+
protected function colonize($word)
152+
{
153+
$word = str_replace('Command', '', $word);
154+
155+
return strtolower(preg_replace('/[^A-Z^a-z^0-9]+/',':',
156+
preg_replace('/([a-zd])([A-Z])/','\1:\2',
157+
preg_replace('/([A-Z]+)([A-Z][a-z])/','\1:\2',$word))));
158+
}
159+
}

0 commit comments

Comments
 (0)