@@ -52,35 +52,70 @@ public function main($plugin = null)
52
52
return false ;
53
53
}
54
54
55
- return $ this ->_modifyBootstrap (
56
- $ plugin ,
57
- $ this ->params ['bootstrap ' ],
58
- $ this ->params ['routes ' ],
59
- $ this ->params ['autoload ' ]
60
- );
55
+ $ options = $ this ->makeOptions ();
56
+
57
+ $ app = APP . 'Application.php ' ;
58
+ if (file_exists ($ app ) && !$ this ->param ('no_app ' )) {
59
+ return $ this ->modifyApplication ($ app , $ plugin , $ options );
60
+ }
61
+
62
+ return $ this ->_modifyBootstrap ($ plugin , $ options );
63
+ }
64
+
65
+ /**
66
+ * Create options string for the load call.
67
+ *
68
+ * @return string
69
+ */
70
+ protected function makeOptions ()
71
+ {
72
+ $ autoloadString = $ this ->param ('autoload ' ) ? "'autoload' => true " : '' ;
73
+ $ bootstrapString = $ this ->param ('bootstrap ' ) ? "'bootstrap' => true " : '' ;
74
+ $ routesString = $ this ->param ('routes ' ) ? "'routes' => true " : '' ;
75
+
76
+ return implode (', ' , array_filter ([$ autoloadString , $ bootstrapString , $ routesString ]));
77
+ }
78
+
79
+ /**
80
+ * Modify the application class
81
+ *
82
+ * @param string $app The Application file to modify.
83
+ * @param string $plugin The plugin name to add.
84
+ * @param string $options The plugin options to add
85
+ * @return void
86
+ */
87
+ protected function modifyApplication ($ app , $ plugin , $ options )
88
+ {
89
+ $ file = new File ($ app , false );
90
+ $ contents = $ file ->read ();
91
+
92
+ $ append = "\n \$this->addPlugin('%s', [%s]); \n" ;
93
+ $ insert = str_replace (', [] ' , '' , sprintf ($ append , $ plugin , $ options ));
94
+
95
+ if (!preg_match ('/function bootstrap\(\)/m ' , $ contents )) {
96
+ $ this ->abort ('Your Application class does not have a bootstrap() method. Please add one. ' );
97
+ } else {
98
+ $ contents = preg_replace ('/(function bootstrap\(\)(?:\s+)\{)/m ' , '$1 ' . $ insert , $ contents );
99
+ }
100
+ $ file ->write ($ contents );
101
+
102
+ $ this ->out ('' );
103
+ $ this ->out (sprintf ('%s modified ' , $ app ));
61
104
}
62
105
63
106
/**
64
107
* Update the applications bootstrap.php file.
65
108
*
66
109
* @param string $plugin Name of plugin.
67
- * @param bool $hasBootstrap Whether or not bootstrap should be loaded.
68
- * @param bool $hasRoutes Whether or not routes should be loaded.
69
- * @param bool $hasAutoloader Whether or not there is an autoloader configured for
70
- * the plugin.
110
+ * @param string $options The options string
71
111
* @return bool If modify passed.
72
112
*/
73
- protected function _modifyBootstrap ($ plugin , $ hasBootstrap , $ hasRoutes , $ hasAutoloader )
113
+ protected function _modifyBootstrap ($ plugin , $ options )
74
114
{
75
115
$ bootstrap = new File ($ this ->bootstrap , false );
76
116
$ contents = $ bootstrap ->read ();
77
117
if (!preg_match ("@ \n\s*Plugin::loadAll@ " , $ contents )) {
78
- $ autoloadString = $ hasAutoloader ? "'autoload' => true " : '' ;
79
- $ bootstrapString = $ hasBootstrap ? "'bootstrap' => true " : '' ;
80
- $ routesString = $ hasRoutes ? "'routes' => true " : '' ;
81
-
82
118
$ append = "\nPlugin::load('%s', [%s]); \n" ;
83
- $ options = implode (', ' , array_filter ([$ autoloadString , $ bootstrapString , $ routesString ]));
84
119
85
120
$ bootstrap ->append (str_replace (', [] ' , '' , sprintf ($ append , $ plugin , $ options )));
86
121
$ this ->out ('' );
@@ -124,6 +159,11 @@ public function getOptionParser()
124
159
'boolean ' => true ,
125
160
'default ' => false ,
126
161
])
162
+ ->addOption ('no_app ' , [
163
+ 'help ' => 'Do not update the Application if it exist. Forces config/bootstrap.php to be updated. ' ,
164
+ 'boolean ' => true ,
165
+ 'default ' => false ,
166
+ ])
127
167
->addArgument ('plugin ' , [
128
168
'help ' => 'Name of the plugin to load. ' ,
129
169
]);
0 commit comments