Browse Source

Make autoload an option and only include truthy options.

Don't output falsey options and make autoload a CLI flag.

Refs #6042
Mark Story 11 years ago
parent
commit
e9f8bd9bcd
2 changed files with 46 additions and 11 deletions
  1. 18 6
      src/Shell/Task/LoadTask.php
  2. 28 5
      tests/TestCase/Shell/Task/LoadTaskTest.php

+ 18 - 6
src/Shell/Task/LoadTask.php

@@ -46,7 +46,12 @@ class LoadTask extends Shell
             return false;
         }
 
-        return (bool)$this->_modifyBootstrap($plugin, $this->params['bootstrap'], $this->params['routes'], false);
+        return $this->_modifyBootstrap(
+            $plugin,
+            $this->params['bootstrap'],
+            $this->params['routes'],
+            $this->params['autoload']
+        );
     }
 
     /**
@@ -64,13 +69,14 @@ class LoadTask extends Shell
         $bootstrap = new File($this->bootstrap, false);
         $contents = $bootstrap->read();
         if (!preg_match("@\n\s*Plugin::loadAll@", $contents)) {
-            $autoloadString = $hasAutoloader ? null : "'autoload' => true, ";
-            $bootstrapString = $hasBootstrap ? "'bootstrap' => true, " : "'bootstrap' => false, ";
-            $routesString = $hasRoutes ? "'routes' => true" : "'routes' => false";
+            $autoloadString = $hasAutoloader ? "'autoload' => true" : '';
+            $bootstrapString = $hasBootstrap ? "'bootstrap' => true" : '';
+            $routesString = $hasRoutes ? "'routes' => true" : '';
 
-            $append = "\nPlugin::load('%s', [%s%s%s]);\n";
+            $append = "\nPlugin::load('%s', [%s]);\n";
+            $options = implode(', ', array_filter([$autoloadString, $bootstrapString, $routesString]));
 
-            $bootstrap->append(sprintf($append, $plugin, $autoloadString, $bootstrapString, $routesString));
+            $bootstrap->append(sprintf($append, $plugin, $options));
             $this->out('');
             $this->out(sprintf('%s modified', $this->bootstrap));
             return true;
@@ -99,6 +105,12 @@ class LoadTask extends Shell
                     'boolean' => true,
                     'default' => false,
                 ])
+                ->addOption('autoload', [
+                    'help' => 'Will autoload the plugin using CakePHP. ' .
+                        'Set to true if you are not using composer to autoload your plugin.',
+                    'boolean' => true,
+                    'default' => false,
+                ])
                 ->addArgument('plugin', [
                     'help' => 'Name of the plugin to load.',
                 ]);

+ 28 - 5
tests/TestCase/Shell/Task/LoadTaskTest.php

@@ -40,7 +40,6 @@ class LoadTaskTest extends TestCase
         $this->bootstrap = ROOT . DS . 'config' . DS . 'bootstrap.php';
 
         $bootstrap = new File($this->bootstrap, false);
-
         $this->originalBootstrapContent = $bootstrap->read();
     }
 
@@ -56,7 +55,6 @@ class LoadTaskTest extends TestCase
         Plugin::unload();
 
         $bootstrap = new File($this->bootstrap, false);
-
         $bootstrap->write($this->originalBootstrapContent);
     }
 
@@ -70,13 +68,14 @@ class LoadTaskTest extends TestCase
         $this->Task->params = [
             'bootstrap' => false,
             'routes' => false,
+            'autoload' => true,
         ];
 
         $action = $this->Task->main('TestPlugin');
 
         $this->assertTrue($action);
 
-        $expected = "Plugin::load('TestPlugin', ['autoload' => true, 'bootstrap' => false, 'routes' => false]);";
+        $expected = "Plugin::load('TestPlugin', ['autoload' => true]);";
         $bootstrap = new File($this->bootstrap, false);
         $this->assertContains($expected, $bootstrap->read());
     }
@@ -91,13 +90,14 @@ class LoadTaskTest extends TestCase
         $this->Task->params = [
             'bootstrap' => true,
             'routes' => false,
+            'autoload' => true,
         ];
 
         $action = $this->Task->main('TestPlugin');
 
         $this->assertTrue($action);
 
-        $expected = "Plugin::load('TestPlugin', ['autoload' => true, 'bootstrap' => true, 'routes' => false]);";
+        $expected = "Plugin::load('TestPlugin', ['autoload' => true, 'bootstrap' => true]);";
         $bootstrap = new File($this->bootstrap, false);
         $this->assertContains($expected, $bootstrap->read());
     }
@@ -112,13 +112,36 @@ class LoadTaskTest extends TestCase
         $this->Task->params = [
             'bootstrap' => false,
             'routes' => true,
+            'autoload' => true,
+        ];
+
+        $action = $this->Task->main('TestPlugin');
+
+        $this->assertTrue($action);
+
+        $expected = "Plugin::load('TestPlugin', ['autoload' => true, 'routes' => true]);";
+        $bootstrap = new File($this->bootstrap, false);
+        $this->assertContains($expected, $bootstrap->read());
+    }
+
+    /**
+     * test load no autoload
+     *
+     * @return void
+     */
+    public function testLoadNoAutoload()
+    {
+        $this->Task->params = [
+            'bootstrap' => false,
+            'routes' => true,
+            'autoload' => false,
         ];
 
         $action = $this->Task->main('TestPlugin');
 
         $this->assertTrue($action);
 
-        $expected = "Plugin::load('TestPlugin', ['autoload' => true, 'bootstrap' => false, 'routes' => true]);";
+        $expected = "Plugin::load('TestPlugin', ['routes' => true]);";
         $bootstrap = new File($this->bootstrap, false);
         $this->assertContains($expected, $bootstrap->read());
     }