[Solar-svn] Revision 2582

pmjones at solarphp.com pmjones at solarphp.com
Tue Jul 10 15:53:34 CDT 2007


Solar_Controller_Console: [CHG] reorganize some internal logic


Modified: trunk/Solar/Controller/Console.php
===================================================================
--- trunk/Solar/Controller/Console.php	2007-07-10 20:32:05 UTC (rev 2581)
+++ trunk/Solar/Controller/Console.php	2007-07-10 20:53:34 UTC (rev 2582)
@@ -65,6 +65,15 @@
     
     /**
      * 
+     * The list of commands this controller can invoke.
+     * 
+     * @var array
+     * 
+     */
+    protected $_command_list = array();
+    
+    /**
+     * 
      * Constructor.
      * 
      * @param array $config User-provided configuration values.
@@ -95,13 +104,45 @@
     
     /**
      * 
-     * Sets up the environment for all commands.
+     * Finds and invokes a command.
      * 
-     * @return void
+     * @param array $argv The command-line arguments.
      * 
+     * @return string The output of the page action.
+     * 
      */
-    protected function _setup()
+    public function exec($argv = null)
     {
+        // get the command-line arguments
+        if ($argv === null) {
+            $argv = $this->_request->server['argv'];
+            array_shift($argv);
+        } else {
+            $argv = (array) $argv;
+        }
+        
+        // take the command name off the top of the path and try to get a
+        // controller class from it.
+        $command = array_shift($argv);
+        $class = $this->_getCommandClass($command);
+        
+        // did we get a class from it?
+        if (! $class) {
+            // put the original segment back on top.
+            array_unshift($argv, $command);
+            // try to get a controller class from the default page name
+            $class = $this->_getCommandClass($this->_default);
+        }
+        
+        // last chance: do we have a class yet?
+        if (! $class) {
+            return $this->_notFound($command);
+        }
+        
+        // instantiate and invoke the command
+        $obj = Solar::factory($class);
+        $obj->setConsoleController($this);
+        return $obj->exec($argv);
     }
     
     /**
@@ -115,17 +156,43 @@
      */
     public function getCommandList()
     {
+        if (! $this->_command_list) {
+            $this->_setCommandList();
+        }
+        return $this->_command_list;
+    }
+    
+    /**
+     * 
+     * Sets up the environment for all commands.
+     * 
+     * @return void
+     * 
+     */
+    protected function _setup()
+    {
+    }
+    
+    /**
+     * 
+     * Populates the list of recognized commands.
+     * 
+     * @return void
+     * 
+     */
+    protected function _setCommandList()
+    {
         $list = array();
-        
+    
         // loop through class stack and add commands
         $stack = $this->_stack->get();
         foreach ($stack as $class) {
-            
+        
             $dir = Solar::isDir(str_replace('_', DIRECTORY_SEPARATOR, $class));
             if (! $dir) {
                 continue;
             }
-            
+        
             // loop through each file in the directory
             $files = scandir($dir);
             foreach ($files as $file) {
@@ -134,78 +201,33 @@
                 $keep = substr($file, -4) == '.php' &&
                         ctype_alpha($char) &&
                         strtoupper($char) == $char;
-                
+            
                 if (! $keep) {
+                    // doesn't look like a command class
                     continue;
                 }
                 
                 // the list-value is the base class name, plus the file name,
                 // minus the .php extension, to give us a class name
                 $val = $class . substr($file, 0, -4);
-                
+            
                 // the list-key is the command name; convert the file name to a
                 // command name.  FooBar.php becomes "foo-bar".
                 $key = substr($file, 0, -4);
                 $key = preg_replace('/([a-z])([A-Z])/', '$1-$2', $key);
                 $key = strtolower($key);
-                
+            
                 // keep the command name and class name
                 $list[$key] = $val;
             }
         }
-        
+    
         // override with explicit routings
-        $list = array_merge($list, $this->_routing);
-        
-        // done!
-        return $list;
+        $this->_command_list = array_merge($list, $this->_routing);
     }
     
     /**
      * 
-     * Finds and invokes a command.
-     * 
-     * @param array $argv The command-line arguments.
-     * 
-     * @return string The output of the page action.
-     * 
-     */
-    public function exec($argv = null)
-    {
-        // get the command-line arguments
-        if ($argv === null) {
-            $argv = $this->_request->server['argv'];
-            array_shift($argv);
-        } else {
-            $argv = (array) $argv;
-        }
-        
-        // take the command name off the top of the path and try to get a
-        // controller class from it.
-        $command = array_shift($argv);
-        $class = $this->_getCommandClass($command);
-        
-        // did we get a class from it?
-        if (! $class) {
-            // put the original segment back on top.
-            array_unshift($argv, $command);
-            // try to get a controller class from the default page name
-            $class = $this->_getCommandClass($this->_default);
-        }
-        
-        // last chance: do we have a class yet?
-        if (! $class) {
-            return $this->_notFound($command);
-        }
-        
-        // instantiate and invoke the command
-        $obj = Solar::factory($class);
-        $obj->setConsoleController($this);
-        return $obj->exec($argv);
-    }
-    
-    /**
-     * 
      * Finds the command class from a command name.
      * 
      * @param string $command The command name.




More information about the Solar-svn mailing list