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
+ }
0 commit comments