Skip to content

Commit a09caaf

Browse files
committed
Add plugins via application class methods.
If the application class is present, use its methods to load a plugin.
1 parent 1b53551 commit a09caaf

File tree

2 files changed

+98
-27
lines changed

2 files changed

+98
-27
lines changed

src/Shell/Task/LoadTask.php

Lines changed: 56 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -52,35 +52,70 @@ public function main($plugin = null)
5252
return false;
5353
}
5454

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));
61104
}
62105

63106
/**
64107
* Update the applications bootstrap.php file.
65108
*
66109
* @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
71111
* @return bool If modify passed.
72112
*/
73-
protected function _modifyBootstrap($plugin, $hasBootstrap, $hasRoutes, $hasAutoloader)
113+
protected function _modifyBootstrap($plugin, $options)
74114
{
75115
$bootstrap = new File($this->bootstrap, false);
76116
$contents = $bootstrap->read();
77117
if (!preg_match("@\n\s*Plugin::loadAll@", $contents)) {
78-
$autoloadString = $hasAutoloader ? "'autoload' => true" : '';
79-
$bootstrapString = $hasBootstrap ? "'bootstrap' => true" : '';
80-
$routesString = $hasRoutes ? "'routes' => true" : '';
81-
82118
$append = "\nPlugin::load('%s', [%s]);\n";
83-
$options = implode(', ', array_filter([$autoloadString, $bootstrapString, $routesString]));
84119

85120
$bootstrap->append(str_replace(', []', '', sprintf($append, $plugin, $options)));
86121
$this->out('');
@@ -124,6 +159,11 @@ public function getOptionParser()
124159
'boolean' => true,
125160
'default' => false,
126161
])
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+
])
127167
->addArgument('plugin', [
128168
'help' => 'Name of the plugin to load.',
129169
]);

tests/TestCase/Shell/Task/LoadTaskTest.php

Lines changed: 42 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -37,15 +37,6 @@ public function setUp()
3737
{
3838
parent::setUp();
3939

40-
$this->io = $this->getMockBuilder('Cake\Console\ConsoleIo')
41-
->disableOriginalConstructor()
42-
->getMock();
43-
44-
$this->Task = $this->getMockBuilder('Cake\Shell\Task\LoadTask')
45-
->setMethods(['in', 'out', 'err', '_stop'])
46-
->setConstructorArgs([$this->io])
47-
->getMock();
48-
4940
$this->app = APP . DS . 'Application.php';
5041
$this->bootstrap = ROOT . DS . 'config' . DS . 'bootstrap.php';
5142
$this->bootstrapCli = ROOT . DS . 'config' . DS . 'bootstrap_cli.php';
@@ -66,8 +57,6 @@ public function setUp()
6657
public function tearDown()
6758
{
6859
parent::tearDown();
69-
unset($this->shell);
70-
Plugin::unload();
7160

7261
$bootstrap = new File($this->bootstrap, false);
7362
$bootstrap->write($this->originalBootstrapContent);
@@ -172,4 +161,46 @@ public function testLoadNothing()
172161
$contents = file_get_contents($this->bootstrap);
173162
$this->assertContains("Plugin::load('TestPlugin');", $contents);
174163
}
164+
165+
/**
166+
* Test loading the app
167+
*
168+
* @return void
169+
*/
170+
public function testLoadApp()
171+
{
172+
$this->exec('plugin load TestPlugin');
173+
$this->assertExitCode(Shell::CODE_SUCCESS);
174+
175+
$contents = file_get_contents($this->app);
176+
$this->assertContains("\$this->addPlugin('TestPlugin');", $contents);
177+
}
178+
179+
/**
180+
* Test loading the app
181+
*
182+
* @return void
183+
*/
184+
public function testLoadAppBootstrap()
185+
{
186+
$this->exec('plugin load --bootstrap TestPlugin');
187+
$this->assertExitCode(Shell::CODE_SUCCESS);
188+
189+
$contents = file_get_contents($this->app);
190+
$this->assertContains("\$this->addPlugin('TestPlugin', ['bootstrap' => true]);", $contents);
191+
}
192+
193+
/**
194+
* Test loading the app
195+
*
196+
* @return void
197+
*/
198+
public function testLoadAppRoutes()
199+
{
200+
$this->exec('plugin load --routes TestPlugin');
201+
$this->assertExitCode(Shell::CODE_SUCCESS);
202+
203+
$contents = file_get_contents($this->app);
204+
$this->assertContains("\$this->addPlugin('TestPlugin', ['routes' => true]);", $contents);
205+
}
175206
}

0 commit comments

Comments
 (0)