Skip to content
This repository was archived by the owner on Nov 19, 2024. It is now read-only.

Commit ebdf969

Browse files
author
Sven Speckmaier
committed
Rancherize class + fixed various container registration issues
1 parent 4eb234f commit ebdf969

12 files changed

+180
-153
lines changed

app/Application/Rancherize.php

+115
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
<?php namespace Rancherize\Application;
2+
3+
use Exception;
4+
use Rancherize\Blueprint\Validation\Exceptions\ValidationFailedException;
5+
use Symfony\Component\Console\Application;
6+
use Symfony\Component\Console\ConsoleEvents;
7+
use Symfony\Component\Console\Event\ConsoleCommandEvent;
8+
use Symfony\Component\EventDispatcher\EventDispatcher;
9+
10+
/**
11+
* Class Rancherize
12+
* @package Rancherize\Application
13+
*/
14+
class Rancherize {
15+
16+
/**
17+
* @var Application
18+
*/
19+
protected $application;
20+
21+
public function boot() {
22+
$c = container();
23+
24+
/**
25+
* @var EventDispatcher $dispatcher
26+
*/
27+
$dispatcher = $c['event'];
28+
$this->application = new Application('rancherize');
29+
$this->application->setDispatcher($dispatcher);
30+
// register application in container
31+
$c['app'] = function () { return $this->application; };
32+
33+
$dispatcher->addListener(\Symfony\Component\Console\ConsoleEvents::EXCEPTION, function(\Symfony\Component\Console\Event\ConsoleExceptionEvent $event) {
34+
35+
$e = $event->getException();
36+
$output = $event->getOutput();
37+
38+
39+
if ( $e instanceof ValidationFailedException ) {
40+
41+
$formatter = $output->getFormatter();
42+
43+
$headline = ' Validation failed ';
44+
$output->writeln( [
45+
'',
46+
' ' . $formatter->format( sprintf( "<error> %s </error>", str_repeat(' ', strlen($headline)) ) ) . ' ',
47+
$formatter->format(" <error> $headline </error>"),
48+
' ' . $formatter->format( sprintf( "<error> %s </error>", str_repeat('=', strlen($headline)) ) ) . ' ',
49+
' ' . $formatter->format( sprintf( "<error> %s </error>", str_repeat(' ', strlen($headline)) ) ) . ' ',
50+
"",
51+
]);
52+
53+
/**
54+
* @var \Rancherize\Services\ValidateService $validateService
55+
*/
56+
$validateService = container('validate-service');
57+
$validateService->print($e, $output);
58+
}
59+
60+
});
61+
62+
$dispatcher->addListener(ConsoleEvents::COMMAND, function (ConsoleCommandEvent $event) {
63+
// get the input instance
64+
$input = $event->getInput();
65+
66+
// get the output instance
67+
$output = $event->getOutput();
68+
69+
// get the command to be executed
70+
$command = $event->getCommand();
71+
72+
// get the application
73+
$application = $command->getApplication();
74+
75+
$c = container();
76+
$c['output'] = function() use ($output) { return $output; };
77+
$c['input'] = function() use ($input) { return $input; };
78+
$c['application'] = function() use ($application) { return $application; };
79+
$c['command'] = function() use ($command) { return $command; };
80+
$c['process-helper'] = function() use ($command) { return $command->getHelper('process'); };
81+
});
82+
83+
$internalPlugins = require_once __DIR__.'/../lists/plugins.php';
84+
$pluginLoaderExtra = container('plugin-loader-extra');
85+
foreach($internalPlugins as $internalPlugin) {
86+
/**
87+
* @var \Rancherize\Plugin\Loader\ExtraPluginLoaderDecorator $pluginLoaderExtra
88+
*/
89+
$pluginLoaderExtra->registerExtra($internalPlugin);
90+
}
91+
92+
try {
93+
94+
/**
95+
* @var \Rancherize\Plugin\Loader\PluginLoader $pluginLoader
96+
*/
97+
$pluginLoader = container('plugin-loader');
98+
$pluginLoader->load( $this->application, container() );
99+
100+
} catch(Exception $e) {
101+
102+
echo "Warning! Load Plugins failed: ".get_class($e).' '. $e->getMessage()."\n";
103+
104+
}
105+
}
106+
107+
public function run() {
108+
109+
$returnCode = $this->application->run();
110+
111+
return $returnCode;
112+
113+
}
114+
115+
}

app/Commands/CommandsProvider.php

+17
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
use Rancherize\Blueprint\Factory\BlueprintFactory;
66
use Rancherize\Configuration\Services\EnvironmentConfigurationService;
77
use Rancherize\Docker\DockerAccessService;
8+
use Rancherize\Plugin\Commands\PluginInstallCommand;
9+
use Rancherize\Plugin\Commands\PluginRegisterCommand;
810
use Rancherize\Plugin\Provider;
911
use Rancherize\Plugin\ProviderTrait;
1012
use Rancherize\RancherAccess\InServiceChecker;
@@ -95,6 +97,18 @@ public function register() {
9597
$this->container['command.environment.set'] = function($c) {
9698
return new EnvironmentSetCommand( $c[EnvironmentConfigurationService::class] );
9799
};
100+
101+
$this->container['command.rancher.access'] = function() {
102+
return new RancherAccessCommand();
103+
};
104+
105+
$this->container['command.plugin.install'] = function() {
106+
return new PluginInstallCommand();
107+
};
108+
109+
$this->container['command.plugin.register'] = function() {
110+
return new PluginRegisterCommand();
111+
};
98112
}
99113

100114
/**
@@ -115,6 +129,9 @@ public function boot() {
115129
$app->add( $this->container['command.blueprint.add'] );
116130
$app->add( $this->container['command.blueprint.list'] );
117131
$app->add( $this->container['command.environment.set'] );
132+
$app->add( $this->container['command.rancher.access'] );
133+
$app->add( $this->container['command.plugin.install'] );
134+
$app->add( $this->container['command.plugin.register'] );
118135

119136
$app->add( $this->container['environment-version-command'] );
120137
}

app/File/FileLoader.php

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<?php namespace Rancherize\File;
2+
23
use Rancherize\Configuration\Exceptions\FileNotFoundException;
34

45
/**

app/File/FileProvider.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,12 @@ public function register() {
1818
/**
1919
* File handling
2020
*/
21-
$container[FileLoader::class] = function() {
21+
$this->container[FileLoader::class] = function() {
2222

2323
return new FileLoader();
2424
};
2525

26-
$container['file-writer'] = function() {
26+
$this->container[FileWriter::class] = function() {
2727
return new FileWriter();
2828
};
2929
}

app/File/FileWriter.php

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<?php namespace Rancherize\File;
2+
23
use Rancherize\Configuration\Exceptions\SaveFailedException;
34

45
/**

app/Plugin/Loader/ComposerPluginLoader.php

+27-23
Original file line numberDiff line numberDiff line change
@@ -4,45 +4,36 @@
44
use Rancherize\Composer\PackageNameParser;
55
use Rancherize\Configuration\Configurable;
66
use Rancherize\Configuration\Services\ProjectConfiguration;
7-
use Rancherize\Plugin\Exceptions\PluginAlreadyRegisteredException;
7+
use Rancherize\Exceptions\PluginAlreadyRegisteredException;
88
use Rancherize\Plugin\Provider;
99
use Symfony\Component\Console\Application;
1010

1111
/**
1212
* Class ComposerPluginLoader
1313
*/
1414
class ComposerPluginLoader implements PluginLoader {
15-
/**
16-
* @var Configurable
17-
*/
18-
private $configurable;
19-
/**
20-
* @var ProjectConfiguration
21-
*/
22-
private $projectConfiguration;
15+
2316
/**
2417
* @var PackageNameParser
2518
*/
2619
private $packageNameParser;
2720

2821
/**
29-
* ComposerPluginLoader constructor.
30-
* @param Configurable $configurable
31-
* @param ProjectConfiguration $projectConfiguration
32-
* @param PackageNameParser $packageNameParser
22+
* @var ProjectConfiguration
3323
*/
34-
public function __construct(Configurable $configurable, ProjectConfiguration $projectConfiguration, PackageNameParser $packageNameParser) {
35-
$this->configurable = $configurable;
36-
$this->projectConfiguration = $projectConfiguration;
37-
$this->packageNameParser = $packageNameParser;
38-
}
24+
private $projectConfiguration;
3925

4026
/**
4127
* @param string $pluginName
4228
* @param string $classpath
4329
*/
4430
public function register( string $pluginName, string $classpath) {
45-
$plugins = $this->configurable->get('project.plugins');
31+
/**
32+
* @var Configurable $configurable
33+
*/
34+
$configurable = container('project-config');
35+
36+
$plugins = $configurable->get('project.plugins');
4637

4738
if( !is_array($plugins) )
4839
$plugins = [];
@@ -53,17 +44,30 @@ public function register( string $pluginName, string $classpath) {
5344
$key = $this->removeVersionRestraint($pluginName);
5445

5546
$plugins[$key] = $classpath;
56-
$this->configurable->set('project.plugins', $plugins);
47+
$configurable->set('project.plugins', $plugins);
5748

58-
$this->projectConfiguration->save($this->configurable);
49+
$this->projectConfiguration->save($configurable);
5950
}
6051

6152
/**
62-
* @param \Rancherize\Configuration\Configuration $configuration
6353
* @param Application $application
6454
* @param Container $container
6555
*/
66-
public function load(\Rancherize\Configuration\Configuration $configuration, Application $application, Container $container) {
56+
public function load( Application $application, Container $container) {
57+
$this->packageNameParser = $container[PackageNameParser::class];
58+
59+
/**
60+
* @var ProjectConfiguration $projectConfig
61+
*/
62+
$projectConfig = $container['project-config-service'];
63+
$this->projectConfiguration = $projectConfig;
64+
65+
/**
66+
* @var \Rancherize\Configuration\Configurable $configuration
67+
*/
68+
$configuration = $container['configuration'];
69+
$configuration = $projectConfig->load($configuration);
70+
6771
$pluginClasspathes = $configuration->get('project.plugins');
6872
if( !is_array($pluginClasspathes) )
6973
$pluginClasspathes = [];

app/Plugin/Loader/ExtraPluginLoaderDecorator.php

+3-2
Original file line numberDiff line numberDiff line change
@@ -60,14 +60,14 @@ public function register( string $pluginName, string $classpath ) {
6060
* @param Container $container
6161
* @return
6262
*/
63-
public function load( Configuration $configuration, Application $application, Container $container ) {
63+
public function load( Application $application, Container $container ) {
6464

6565
$extraPlugins = $this->createExtraPlugins($application, $container);
6666

6767
foreach($extraPlugins as $extraPlugin)
6868
$extraPlugin->register();
6969

70-
$success = $this->pluginLoader->load($configuration, $application, $container);
70+
$success = $this->pluginLoader->load($application, $container);
7171

7272
foreach($extraPlugins as $extraPlugin)
7373
$extraPlugin->boot();
@@ -94,6 +94,7 @@ private function createExtraPlugins(Application $application, Container $contain
9494
*
9595
*/
9696
foreach($this->pluginList as $pluginClassPath) {
97+
9798
/**
9899
* @var Provider $plugin
99100
*/

app/Plugin/Loader/PluginLoader.php

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
<?php namespace Rancherize\Plugin\Loader;
22

33
use Pimple\Container;
4-
use Rancherize\Configuration\Configuration;
54
use Symfony\Component\Console\Application;
65

76
/**
@@ -17,10 +16,9 @@ interface PluginLoader {
1716
function register( string $pluginName, string $classpath);
1817

1918
/**
20-
* @param Configuration $configuration
2119
* @param Application $application
2220
* @param Container $container
2321
* @return
2422
*/
25-
function load(Configuration $configuration, Application $application, Container $container);
23+
function load(Application $application, Container $container);
2624
}

app/RancherAccess/RancherAccessProvider.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,11 @@ public function register() {
2424
return new RancherAccessConfigService();
2525
};
2626

27-
$container[RancherService::class] = function($c) {
27+
$this->container[RancherService::class] = function($c) {
2828
return new RancherService( $c[ApiService::class] );
2929
};
3030

31-
$container[InServiceChecker::class] = function() {
31+
$this->container[InServiceChecker::class] = function() {
3232
return new InServiceChecker();
3333
};
3434

app/container.php

+8-5
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
};
2525

2626
$container['writer'] = function($c) {
27-
return new \Rancherize\Configuration\Writer\JsonWriter($c[\Rancherize\File\FileLoader::class]);
27+
return new \Rancherize\Configuration\Writer\JsonWriter($c[\Rancherize\File\FileWriter::class]);
2828
};
2929

3030
$container['global-config-service'] = function($c) {
@@ -101,7 +101,10 @@
101101
* Plugins
102102
*/
103103
$container['plugin-installer'] = function($c) {
104-
global $application;
104+
/**
105+
* @var \Symfony\Component\Console\Application $application
106+
*/
107+
$application = $c['app'];
105108

106109
$nameParser = $c['composer-packet-name-parser'];
107110
$pathMaker = $c['composer-packet-path-maker'];
@@ -125,17 +128,17 @@
125128
return new \Rancherize\Plugin\Loader\ExtraPluginLoaderDecorator($c['loader-interface']);
126129
};
127130

128-
$container['package-name-parser'] = function() {
131+
$container[\Rancherize\Composer\PackageNameParser::class] = function() {
129132
return new \Rancherize\Composer\PackageNameParser();
130133
};
131134

132-
$container['plugin-loader'] = function($c) {
135+
$container['plugin-loader'] = function() {
133136

134137
/*
135138
* project-config is not set in this file - it is set in the rancherize.php once the project config was loaded for
136139
* use with the plugin system
137140
*/
138-
return new \Rancherize\Plugin\Loader\ComposerPluginLoader($c['project-config'], $c['project-config-service'], $c['package-name-parser']);
141+
return new \Rancherize\Plugin\Loader\ComposerPluginLoader( );
139142
};
140143

141144
$container->extend('plugin-loader', function($pluginLoader, $c) {

app/lists/commands.php

-7
This file was deleted.

0 commit comments

Comments
 (0)