Browse Source

added cakephp coding standards and added Regex for unloading

Bob 11 years ago
parent
commit
7f7472caa5

+ 5 - 7
src/Shell/PluginShell.php

@@ -12,7 +12,6 @@
  * @since         3.0.0
  * @license       http://www.opensource.org/licenses/mit-license.php MIT License
  */
-
 namespace Cake\Shell;
 
 use Cake\Console\Shell;
@@ -42,20 +41,19 @@ class PluginShell extends Shell
     {
         $parser = parent::getOptionParser();
         $parser->addSubcommand(
-            'load',
-            [
+                'load', [
             'help' => 'Loads a plugin',
             'parser' => $this->Load->getOptionParser(),
-            ]
+                ]
         );
         $parser->addSubcommand(
-            'unload',
-            [
+                'unload', [
             'help' => 'Unloads a plugin',
             'parser' => $this->Unload->getOptionParser(),
-            ]
+                ]
         );
 
         return $parser;
     }
+
 }

+ 25 - 22
src/Shell/Task/LoadTask.php

@@ -12,11 +12,9 @@
  * @since         3.0.0
  * @license       http://www.opensource.org/licenses/mit-license.php MIT License
  */
-
 namespace Cake\Shell\Task;
 
 use Cake\Console\Shell;
-use Cake\Core\App;
 use Cake\Filesystem\File;
 
 /**
@@ -25,20 +23,22 @@ use Cake\Filesystem\File;
  */
 class LoadTask extends Shell
 {
-
-    public $path = null;
+    /**
+     * Path to the bootstrap file.
+     *
+     * @var string
+     */
     public $bootstrap = null;
 
     /**
-     * Execution method always used for tasks
+     * Execution method always used for tasks.
      *
-     * @return boolean if action passed
+     * @param string $plugin The plugin name.
+     * @return bool if action passed.
      *
      */
     public function main($plugin = null)
     {
-
-        $this->path = current(App::path('Plugin'));
         $this->bootstrap = ROOT . DS . 'config' . DS . 'bootstrap.php';
 
         if (empty($plugin)) {
@@ -60,22 +60,25 @@ class LoadTask extends Shell
     /**
      * Update the applications bootstrap.php file.
      *
-     * @param string $plugin Name of plugin
+     * @param string $plugin Name of plugin.
+     * @param bool $hasBootstrap Whether or not bootstrap should be loaded.
+     * @param bool $hasRoutes Whether or not routes should be loaded.
      * @param bool $hasAutoloader Whether or not there is an autoloader configured for
-     * the plugin
-     * @return void
+     * the plugin.
+     * @return bool If modify passed.
      */
     protected function _modifyBootstrap($plugin, $hasBootstrap, $hasRoutes, $hasAutoloader)
     {
         $bootstrap = new File($this->bootstrap, false);
         $contents = $bootstrap->read();
         if (!preg_match("@\n\s*Plugin::loadAll@", $contents)) {
-            $autoload = $hasAutoloader ? null : "'autoload' => true, ";
-            $bootstrap->append(sprintf(
-                "\nPlugin::load('%s', [%s'bootstrap' => " . ($hasBootstrap ? 'true' : 'false') . ", 'routes' => " . ($hasRoutes ? 'true' : 'false') . "]);\n",
-                $plugin,
-                $autoload
-            ));
+            $_autoload = $hasAutoloader ? null : "'autoload' => true, ";
+            $_bootstrap = $hasBootstrap ? "'bootstrap' => true, " : "'bootstrap' => false, ";
+            $_routes = $hasRoutes ? "'routes' => true" : "'routes' => false";
+
+            $append = "\nPlugin::load('%s', [%s%s%s]);\n";
+
+            $bootstrap->append(sprintf($append, $plugin, $_autoload, $_bootstrap, $_routes));
             $this->out('');
             $this->out(sprintf('%s modified', $this->bootstrap));
             return true;
@@ -86,22 +89,22 @@ class LoadTask extends Shell
     /**
      * GetOptionParser method.
      *
-     * @return type
+     * @return \Cake\Console\ConsoleOptionParser
      */
     public function getOptionParser()
     {
         $parser = parent::getOptionParser();
 
         $parser->addOption('bootstrap', [
-            'short'   => 'b',
-            'help'    => 'Will load bootstrap.php from plugin.',
+            'short' => 'b',
+            'help' => 'Will load bootstrap.php from plugin.',
             'boolean' => true,
             'default' => false,
         ]);
 
         $parser->addOption('routes', [
-            'short'   => 'r',
-            'help'    => 'Will load routes.php from plugin.',
+            'short' => 'r',
+            'help' => 'Will load routes.php from plugin.',
             'boolean' => true,
             'default' => false,
         ]);

+ 20 - 25
src/Shell/Task/UnloadTask.php

@@ -12,11 +12,9 @@
  * @since         3.0.0
  * @license       http://www.opensource.org/licenses/mit-license.php MIT License
  */
-
 namespace Cake\Shell\Task;
 
 use Cake\Console\Shell;
-use Cake\Core\App;
 use Cake\Filesystem\File;
 
 /**
@@ -25,20 +23,23 @@ use Cake\Filesystem\File;
  */
 class UnloadTask extends Shell
 {
-    public $path      = null;
+    /**
+     * Path to the bootstrap file.
+     *
+     * @var string
+     */
     public $bootstrap = null;
 
     /**
-     * Execution method always used for tasks
+     * Execution method always used for tasks.
      *
-     * @return boolean if action passed
+     * @param string $plugin The plugin name.
+     * @return boolean if action passed.
      *
      */
     public function main($plugin = null)
     {
-
-        $this->path      = current(App::path('Plugin'));
-        $this->bootstrap = ROOT.DS.'config'.DS.'bootstrap.php';
+        $this->bootstrap = ROOT . DS . 'config' . DS . 'bootstrap.php';
 
         if (empty($plugin)) {
             $this->err('<error>You must provide a plugin name in CamelCase format.</error>');
@@ -59,31 +60,25 @@ class UnloadTask extends Shell
     /**
      * Update the applications bootstrap.php file.
      *
-     * @param string $plugin Name of plugin
-     * @param bool $hasAutoloader Whether or not there is an autoloader configured for
-     * the plugin
-     * @return void
+     * @param string $plugin Name of plugin.
+     * @return bool If modify passed.
      */
     protected function _modifyBootstrap($plugin)
     {
-        $finder = "Plugin::load('".$plugin."',";
+        $bool = "(false|true)";
+        $finder = "/Plugin::load\('$plugin', \['autoload' => $bool, 'bootstrap' => $bool, 'routes' => $bool]\);\n/";
 
         $bootstrap = new File($this->bootstrap, false);
-        $contents  = $bootstrap->read();
-        if (!preg_match("@\n\s*Plugin::loadAll@", $contents)) {
-            $_contents = explode("\n", $contents);
+        $contents = $bootstrap->read();
 
-            foreach ($_contents as $content) {
-                if (strpos($content, $finder) !== false) {
-                    $loadString = $content;
-                    $loadString .= "\n";
+        if (!preg_match("@\n\s*Plugin::loadAll@", $contents)) {
+            $contents = preg_replace($finder, "", $contents);
 
-                    $bootstrap->replaceText(sprintf($loadString), null);
-                }
-            }
+            $bootstrap->write($contents);
 
             $this->out('');
             $this->out(sprintf('%s modified', $this->bootstrap));
+
             return true;
         }
         return false;
@@ -92,7 +87,7 @@ class UnloadTask extends Shell
     /**
      * GetOptionParser method.
      *
-     * @return type
+     * @return \Cake\Console\ConsoleOptionParser
      */
     public function getOptionParser()
     {
@@ -104,4 +99,4 @@ class UnloadTask extends Shell
 
         return $parser;
     }
-}
+}

+ 6 - 15
tests/TestCase/Shell/Task/LoadTaskTest.php

@@ -9,23 +9,20 @@
  *
  * @copyright     Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  * @link          http://cakephp.org CakePHP Project
- * @since         1.2.0
  * @license       http://www.opensource.org/licenses/mit-license.php MIT License
  */
-
 namespace Cake\Test\TestCase\Shell\Task;
 
 use Cake\Core\Plugin;
-use Cake\TestSuite\TestCase;
 use Cake\Filesystem\File;
+use Cake\TestSuite\TestCase;
 
 /**
- * LoadTaskTest class
+ * LoadTaskTest class.
  *
  */
 class LoadTaskTest extends TestCase
 {
-
     /**
      * setUp method
      *
@@ -47,7 +44,6 @@ class LoadTaskTest extends TestCase
 
         $this->original_bootstrap_content = $bootstrap->read();
     }
-
     /**
      * tearDown method
      *
@@ -63,7 +59,6 @@ class LoadTaskTest extends TestCase
 
         $bootstrap->write($this->original_bootstrap_content);
     }
-
     /**
      * testLoad
      *
@@ -73,7 +68,7 @@ class LoadTaskTest extends TestCase
     {
         $this->Task->params = [
             'bootstrap' => false,
-            'routes'    => false,
+            'routes' => false,
         ];
 
         $action = $this->Task->main('TestPlugin');
@@ -84,7 +79,6 @@ class LoadTaskTest extends TestCase
         $bootstrap = new File($this->bootstrap, false);
         $this->assertContains($expected, $bootstrap->read());
     }
-
     /**
      * testLoadWithBootstrap
      *
@@ -94,7 +88,7 @@ class LoadTaskTest extends TestCase
     {
         $this->Task->params = [
             'bootstrap' => true,
-            'routes'    => false,
+            'routes' => false,
         ];
 
         $action = $this->Task->main('TestPlugin');
@@ -105,7 +99,6 @@ class LoadTaskTest extends TestCase
         $bootstrap = new File($this->bootstrap, false);
         $this->assertContains($expected, $bootstrap->read());
     }
-
     /**
      * testLoadWithRoutes
      *
@@ -115,7 +108,7 @@ class LoadTaskTest extends TestCase
     {
         $this->Task->params = [
             'bootstrap' => false,
-            'routes'    => true,
+            'routes' => true,
         ];
 
         $action = $this->Task->main('TestPlugin');
@@ -126,7 +119,6 @@ class LoadTaskTest extends TestCase
         $bootstrap = new File($this->bootstrap, false);
         $this->assertContains($expected, $bootstrap->read());
     }
-
     /**
      * testLoadNoName
      *
@@ -136,7 +128,7 @@ class LoadTaskTest extends TestCase
     {
         $this->Task->params = [
             'bootstrap' => false,
-            'routes'    => true,
+            'routes' => true,
         ];
 
         $action = $this->Task->main();
@@ -147,5 +139,4 @@ class LoadTaskTest extends TestCase
         $bootstrap = new File($this->bootstrap, false);
         $this->assertNotContains($expected, $bootstrap->read());
     }
-
 }

+ 0 - 4
tests/TestCase/Shell/Task/UnloadTaskTest.php

@@ -9,10 +9,8 @@
  *
  * @copyright     Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  * @link          http://cakephp.org CakePHP Project
- * @since         1.2.0
  * @license       http://www.opensource.org/licenses/mit-license.php MIT License
  */
-
 namespace Cake\Test\TestCase\Shell\Task;
 
 use Cake\Core\Plugin;
@@ -81,7 +79,6 @@ class UnloadTaskTest extends TestCase
         $action = $this->Task->main('TestPlugin');
 
         $this->assertTrue($action);
-
         $expected = "Plugin::load('TestPlugin', ['autoload' => true, 'bootstrap' => false, 'routes' => false]);";
         $this->assertNotContains($expected, $bootstrap->read());
     }
@@ -113,5 +110,4 @@ class UnloadTaskTest extends TestCase
         $bootstrap = new File($this->bootstrap, false);
         $bootstrap->append("Plugin::load('" . $name . "', ['autoload' => true, 'bootstrap' => false, 'routes' => false]);\n");
     }
-
 }