Skip to content

Commit a189ec6

Browse files
committed
Automatically initialize configuration
1 parent 01fbd73 commit a189ec6

File tree

7 files changed

+154
-42
lines changed

7 files changed

+154
-42
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
namespace spec\PHPCR\Shell\Event;
4+
5+
use PhpSpec\ObjectBehavior;
6+
use Prophecy\Argument;
7+
use Symfony\Component\Console\Application;
8+
9+
class ApplicationInitEventSpec extends ObjectBehavior
10+
{
11+
function it_is_initializable()
12+
{
13+
$this->shouldHaveType('PHPCR\Shell\Event\ApplicationInitEvent');
14+
}
15+
16+
function let(
17+
Application $application
18+
)
19+
{
20+
$this->beConstructedWith($application);
21+
}
22+
23+
function it_will_return_the_application(
24+
Application $application
25+
)
26+
{
27+
$this->getApplication()->shouldReturn($application);
28+
}
29+
}

src/PHPCR/Shell/Console/Application/ShellApplication.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
use PHPCR\Shell\PhpcrSession;
3232
use Symfony\Component\EventDispatcher\EventDispatcher;
3333
use PHPCR\Shell\Event\PhpcrShellEvents;
34+
use PHPCR\Shell\Event\ApplicationInitEvent;
3435

3536
/**
3637
* Main application for PHPCRSH
@@ -93,12 +94,16 @@ public function init()
9394
);
9495
}
9596

97+
9698
$this->initializeTransports();
9799
$this->initSession();
98100
$this->registerHelpers();
99101
$this->registerCommands();
100102
$this->registerEventListeners();
101103

104+
$event = new ApplicationInitEvent($this);
105+
$this->dispatcher->dispatch(PhpcrShellEvents::APPLICATION_INIT, $event);
106+
102107
$this->initialized = true;
103108
}
104109

@@ -225,6 +230,7 @@ private function registerCommands()
225230

226231
private function registerEventListeners()
227232
{
233+
$this->dispatcher->addSubscriber(new Subscriber\ConfigInitSubscriber());
228234
$this->dispatcher->addSubscriber(new Subscriber\ExceptionSubscriber());
229235
$this->dispatcher->addSubscriber(new Subscriber\AliasSubscriber($this->getHelperSet()->get('config')));
230236
}

src/PHPCR/Shell/Console/Command/Shell/ConfigInitCommand.php

Lines changed: 2 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -24,47 +24,7 @@ public function configure()
2424
public function execute(InputInterface $input, OutputInterface $output)
2525
{
2626
$this->output = $output;
27-
$this->configHelper = $this->getHelper('config');
28-
29-
$fs = new Filesystem();
30-
if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
31-
$output->writeln('<error>This feature is currently only supported on Linux and OSX (maybe). Please submit a PR to support it on windows.</error>');
32-
return 1;
33-
}
34-
35-
$configDir = $this->configHelper->getConfigDir();
36-
$distDir = __DIR__ . '/../../../Resources/config.dist';
37-
38-
if (!file_exists($configDir)) {
39-
$this->logCreation($configDir);
40-
$fs->mkdir($configDir);
41-
}
42-
43-
$configFilenames = array(
44-
'alias.yml',
45-
);
46-
47-
foreach ($configFilenames as $configFilename) {
48-
$srcFile = $distDir . '/' . $configFilename;
49-
$destFile = $configDir . '/' . $configFilename;
50-
51-
if (!file_exists($srcFile)) {
52-
throw new \Exception('Dist (source) file "' . $srcFile . '" does not exist.');
53-
}
54-
55-
if (file_exists($destFile)) {
56-
if (!$this->getHelper('dialog')->askConfirmation($output, '"' . $configFilename . '" already exists, do you want to overwrite it?')) {
57-
return 0;
58-
}
59-
}
60-
61-
$fs->copy($srcFile, $destFile);
62-
$this->logCreation($destFile);
63-
}
64-
}
65-
66-
private function logCreation($path)
67-
{
68-
$this->output->writeln('<info>[+]</info> ' . $path);
27+
$configHelper = $this->getHelper('config');
28+
$configHelper->initConfig($output, $this->getHelper('dialog'));
6929
}
7030
}

src/PHPCR/Shell/Console/Helper/ConfigHelper.php

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44

55
use Symfony\Component\Console\Helper\Helper;
66
use Symfony\Component\Yaml\Yaml;
7+
use Symfony\Component\Console\Output\OutputInterface;
8+
use Symfony\Component\Filesystem\Filesystem;
9+
use Symfony\Component\Console\Helper\DialogHelper;
710

811
/**
912
* Helper for config stuff
@@ -110,4 +113,55 @@ public function getConfig($type)
110113
$this->loadConfig();
111114
return $this->cachedConfig[$type];
112115
}
116+
117+
/**
118+
* Initialize a configuration files
119+
*/
120+
public function initConfig(OutputInterface $output = null, DialogHelper $dialogHelper = null)
121+
{
122+
$log = function ($message) use ($output) {
123+
if ($output) {
124+
$output->writeln($message);
125+
}
126+
};
127+
128+
$fs = new Filesystem();
129+
if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
130+
throw new \RuntimeException('This feature is currently only supported on Linux and OSX (maybe). Please submit a PR to support it on windows.');
131+
}
132+
133+
$configDir = $this->getConfigDir();
134+
$distDir = __DIR__ . '/../../Resources/config.dist';
135+
136+
if (!file_exists($configDir)) {
137+
$log('<info>[+] Creating directory:</info> ' . $configDir);
138+
$fs->mkdir($configDir);
139+
}
140+
141+
$configFilenames = array(
142+
'alias.yml',
143+
);
144+
145+
foreach ($configFilenames as $configFilename) {
146+
$srcFile = $distDir . '/' . $configFilename;
147+
$destFile = $configDir . '/' . $configFilename;
148+
149+
if (!file_exists($srcFile)) {
150+
throw new \Exception('Dist (source) file "' . $srcFile . '" does not exist.');
151+
}
152+
153+
if (file_exists($destFile)) {
154+
if (null !== $dialogHelper) {
155+
if (!$dialogHelper->askConfirmation($output, '"' . $configFilename . '" already exists, do you want to overwrite it?')) {
156+
return 0;
157+
}
158+
} else {
159+
$log(sprintf('<info>File</info> %s <info> already exists, not overwriting.', $destFile));
160+
}
161+
}
162+
163+
$fs->copy($srcFile, $destFile);
164+
$log('<info>[+] Creating file:</info> ' . $destFile);
165+
}
166+
}
113167
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
namespace PHPCR\Shell\Event;
4+
5+
use Symfony\Component\EventDispatcher\Event;
6+
use Symfony\Component\Console\Application;
7+
8+
/**
9+
* This event is fired when the main shell application
10+
* is initialized.
11+
*
12+
* @author Daniel Leech <daniel@dantleech.com>
13+
*/
14+
class ApplicationInitEvent extends Event
15+
{
16+
protected $application;
17+
18+
public function __construct(Application $application)
19+
{
20+
$this->application = $application;
21+
}
22+
23+
public function getApplication()
24+
{
25+
return $this->application;
26+
}
27+
28+
}

src/PHPCR/Shell/Event/PhpcrShellEvents.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,5 @@ class PhpcrShellEvents
66
{
77
const COMMAND_EXCEPTION = 'command.exception';
88
const COMMAND_PRE_RUN = 'command.pre_run';
9+
const APPLICATION_INIT= 'application.init';
910
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
namespace PHPCR\Shell\Subscriber;
4+
5+
use PHPCR\Shell\Event\PhpcrShellEvents;
6+
use PHPCR\Shell\Event\ApplicationInitEvent;
7+
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
8+
9+
/**
10+
* Subscriber to initialize the configuration if it does not
11+
* already exist upon application initialization
12+
*
13+
* @author Daniel Leech <daniel@dantleech.com>
14+
*/
15+
class ConfigInitSubscriber implements EventSubscriberInterface
16+
{
17+
public static function getSubscribedEvents()
18+
{
19+
return array(
20+
PhpcrShellEvents::APPLICATION_INIT => 'handleApplicationInit',
21+
);
22+
}
23+
24+
public function handleApplicationInit(ApplicationInitEvent $event)
25+
{
26+
$application = $event->getApplication();
27+
$config = $application->getHelperSet()->get('config');
28+
$configDir = $config->getConfigDir();
29+
30+
if (!file_exists($configDir)) {
31+
$config->initConfig();
32+
}
33+
}
34+
}

0 commit comments

Comments
 (0)