Anonymous avatar Anonymous committed be6fc1a

* Version command refactored to work better!
* Executable can now get its version property.

Comments (0)

Files changed (3)

Trunk/Tests/Functional/test_Version.php

+<?php
+
+include_once '../../VersionControl/Hg.php';
+
+$hg = new VersionControl_Hg('H:\Development\_Webroot\Trunk\Tests\Fixtures\Test_Repository');
+
+
+var_dump($hg->version()->run(array('verbose' => null)));
+var_dump($hg->version()->run('verbose'));
+
+//@TODO 'major' arugments not yet implemented
+var_dump($hg->version('raw')->run('verbose'));
+var_dump($hg->version('major')->run('verbose'));
+var_dump($hg->version('minor')->run('verbose'));
+var_dump($hg->version('raw')->run('verbose'));
+
+/* getting the property */
+//This currently does not / cannot (?) work since it only
+//echo $hg->version;
+
+//property can only be gotten through the executable
+echo $hg->executable->version;

Trunk/VersionControl/Hg/Command/Version.php

-<?php
-/**
- * Contains the definition of the Version command
- *
- * PHP version 5
- *
- * @category    VersionControl
- * @package     Hg
- * @subpackage  Command
- * @author      Michael Gatto <mgatto@lisantra.com>
- * @copyright   2009 Lisantra Technologies, LLC
- * @license     http://www.opensource.org/licenses/mit-license.html  MIT License
- * @link        http://pear.php.net/package/VersionControl_Hg
- */
-
-/**
- * Provides the required interface for all commands
- */
-require_once 'Interface.php';
-
-/**
- * Provides base functionality common to all commands
- */
-require_once 'Abstract.php';
-
-/**
- * Provides Exceptions for commands (VersionControl_Hg_Command_Exception)
- */
-require_once 'Exception.php';
-
-/**
- * Implements the version command
- *
- * PHP version 5
- *
- * @category    VersionControl
- * @package     Hg
- * @subpackage  Command
- * @author      Michael Gatto <mgatto@lisantra.com>
- * @copyright   2009 Lisantra Technologies, LLC
- * @license     http://www.opensource.org/licenses/mit-license.html  MIT License
- * @link        http://pear.php.net/package/VersionControl_Hg
- */
-class VersionControl_Hg_Command_Version
-    extends VersionControl_Hg_Command_Abstract
-    implements VersionControl_Hg_Command_Interface
-{
-    /**
-     * The name of the mercurial command implemented here
-     *
-     * @var string
-     */
-    protected $command = 'version';
-
-    /**
-     * There are no required options for this command
-     *
-     * Redefine it to an empty array to prevent parent from passing on any
-     * possible unnecessary required commands.
-     *
-     * @var mixed
-     */
-    protected $required_options = array();
-
-    /**
-     * Constructor
-     *
-     * @param   VersionControl_Hg $hg
-     * @return  void
-     */
-    public function __construct($param = null, VersionControl_Hg $hg) {
-
-    }
-
-    /**
-     * (non-PHPdoc)
-     * @see VersionControl/Hg/Command/VersionControl_Hg_Command_Interface#execute($params)
-     */
-    public function execute(array $options = null)
-    {
-        //process options array; everything should be in long format.
-        $modifiers = null;
-        //this $options[0] thingy is because __call makes the arguments into an array
-        if ( is_array($options[0])) {
-            foreach ($options[0] as $option => $argument) {
-                $modifiers .= ' --' . $option . ' ' . $argument;
-            }
-        } elseif ( is_string($options[0]) ) {
-            //we want only a scalar and not an object nor a null
-            $modifiers .= ' --' . $options[0];
-        }
-        //$command_string = escapeshellcmd();
-
-//var_dump($this->hg, $this->hg->executable);die;
-
-        $command_string = '"' . $this->hg->executable . '" ' . $this->command;
-        $command_string .= rtrim($modifiers);
-
-        exec($command_string, $output, $command_status);
-
-        //@todo remove the die()...
-        ($command_status === 0) or die("returned an error: $command_string");
-
-        $ver_string = $output[0];
-
-        /* handle bad input */
-        //@todo replace with a constant and message
-        if ( preg_match('/\(.*\)/', $ver_string, $ver_match) == 0 ) {
-            throw new VersionControl_Hg_Command_Exception(
-                'Unrecognized version data'
-            );
-        }
-
-        $version['raw'] = trim(substr($ver_match[0], 8, strlen($ver_match[0])));
-        //replace the parenthesis because my regex fu is out to lunch.
-        $version['raw'] = str_replace('(' , '', $version['raw']);
-        $version['raw'] = str_replace(')' , '', $version['raw']);
-        //break up string into version components
-        //does the version have a date after the version number?
-        if ( strstr($version['raw'], '+') ) {
-            $ver_parts = explode('+', $version['raw']);
-            //handle if the text after '+' is a changeset, not a date
-            //@todo replace date_parse() this to remove dependency on Php 5.2.x
-            if ( date_parse($ver_parts[1]) ) {
-                $version['date'] = $ver_parts[1];
-            }
-            else{
-               $version['changeset'] = $ver_parts[1];
-            }
-        }
-        else {
-            $ver_parts[0] = $version['raw'];
-        }
-
-        $version['complete'] = $ver_parts[0];
-
-        $version_tmp = explode('.', $ver_parts[0]);
-
-        $version['major'] = $version_tmp[0];
-        $version['minor'] = $version_tmp[1];
-
-        return $version['raw'];
-    }
-
-    /**
-     *
-     * @return string
-     */
-    public function __toString() {
-        return ""; //join(" ", $this->output);
-    }
-}
+<?php
+/**
+ * Contains the definition of the Version class
+ *
+ * PHP version 5
+ *
+ * @category   VersionControl
+ * @package    Hg
+ * @subpackage Command
+ * @author     Michael Gatto <mgatto@lisantra.com>
+ * @copyright  2011 Lisantra Technologies, LLC
+ * @license    http://www.opensource.org/licenses/mit-license.html  MIT License
+ * @link       http://pear.php.net/package/VersionControl_Hg
+ * @filesource
+ */
+
+/**
+ * Provides the required interface for all commands
+ */
+require_once 'Interface.php';
+
+/**
+ * Provides base functionality common to all commands
+ */
+require_once 'Abstract.php';
+
+/**
+ * Provides Exceptions for commands (VersionControl_Hg_Command_Exception)
+ */
+require_once 'Exception.php';
+
+/**
+ * Provides an output formatter
+ */
+require_once 'Output/Formatter.php';
+
+/**
+ * Print the version of the HG executable in use
+ *
+ * Usage:
+ * <code>
+ * $hg = new VersionControl_Hg();
+ * $hg->version()->run();
+ *
+ * $hg->version()->format('raw')->run();
+ * </code>
+ *
+ * You may also echo the property since this command implements _toString():
+ * <code>
+ * $hg = new VersionControl_Hg();
+ * echo $hg->version;
+ * </code>
+ * or
+ * <code>
+ * $hg = new VersionControl_Hg();
+ * echo $hg->executable->version;
+ * </code>
+ *
+ * PHP version 5
+ *
+ * @category   VersionControl
+ * @package    Hg
+ * @subpackage Command
+ * @author     Michael Gatto <mgatto@lisantra.com>
+ * @copyright  2011 Lisantra Technologies, LLC
+ * @license    http://www.opensource.org/licenses/mit-license.html  MIT License
+ * @link       http://pear.php.net/package/VersionControl_Hg
+ */
+class VersionControl_Hg_Command_Version
+    extends VersionControl_Hg_Command_Abstract
+        implements VersionControl_Hg_Command_Interface
+{
+    /**
+     * The name of the mercurial command implemented here
+     *
+     * @var string
+     */
+    protected $command = 'version';
+
+    /**
+     * Required options for this specific command. These may not be required
+     * by Mercurial itself, but are required for the proper functioning of
+     * this package.
+     *
+     * @var mixed
+     */
+    protected $required_options = array(
+        'noninteractive' => null,
+    );
+
+    /**
+     * Permissable options.
+     *
+     * The actual option must be the key, while 'null' is a value here to
+     * accommodate the current implementation of setting options.
+     *
+     * @var mixed
+     */
+    protected $allowed_options = array(
+        'output' => null,
+    );
+
+    /**
+     * Constructor
+     *
+     * @param mixed             $params One or more parameters to modify the command
+     * @param VersionControl_Hg $hg     The base Hg instance
+     *
+     * @return void
+     */
+    public function __construct($params = null, VersionControl_Hg $hg)
+    {
+        $this->hg = $hg;
+
+        /* should always be called so we have a full array of valid options */
+        $this->setOptions(array()); //$params
+    }
+
+    /**
+     * Execute the command and return the results.
+     *
+     * @param mixed $params The options passed to the Log command
+     *
+     * @return string
+     */
+    public function execute(array $params = null, VersionControl_Hg $hg)
+    {
+        /* take care of options passed into run() as such:
+         * $hg->cat('/file/')->run('verbose'));
+         * Although, 'verbose|quiet' probably have no effect...?
+         */
+        if ( ! empty($params) ) {
+            $this->setOptions($params);
+        }
+
+        /* --noninteractive is required since issuing the command is
+         * unattended by nature of using this package.
+         *
+         * --repository PATH is required since the PWD on which hg is invoked
+         * will not be within the working copy of the repo. */
+        $this->addOptions(
+            array(
+                'noninteractive' => null,
+            )
+        );
+
+        /* Despite its being so not variable, we need to set the command string
+         * only after manually setting options and other command-specific data */
+        $this->setCommandString();
+
+        //var_dump($this->command_string);
+
+        /* no var assignment, since 2nd param holds output */
+        exec($this->command_string, $this->output, $this->status);
+
+        if ( $this->status !== 0 ) {
+            throw new VersionControl_Hg_Command_Exception(
+                VersionControl_Hg_Command_Exception::COMMANDLINE_ERROR
+            );
+        }
+
+        /* parse this->output for version string parts */
+        $version = array();
+
+        /* Regex to get the version between parenthesis */
+        preg_match('!\(([^\)]+)\)!', $this->output[0], $version); //'/\((.*?)\)/',
+
+        $version['raw'] = trim(str_replace('version', '', $version[1]));
+        //break up string into version components
+        //does the version have a date after the version number?
+        if ( strstr($version['raw'], '+') ) {
+            /* handle if the text after '+' is a changeset, not a date */
+            $ver_parts = explode('+', $version['raw']);
+
+            //@todo replace date_parse() this to remove dependency on Php 5.2.x
+            if ( date_parse($ver_parts[1]) ) {
+                $version['date'] = $ver_parts[1];
+            } else {
+                $version['changeset'] = $ver_parts[1];
+            }
+        } else {
+            $ver_parts[0] = $version['raw'];
+        }
+
+        $version_tmp = explode('.', $ver_parts[0]);
+
+        $version['major'] = $version_tmp[0];
+        $version['minor'] = $version_tmp[1];
+        $version['maintenance'] = $version_tmp[2];
+
+        /* clean up from intermediate data */
+        unset($version[0], $version[1]);
+
+        return $version;
+    }
+}

Trunk/VersionControl/Hg/Executable.php

      */
     private function __construct($hg, $path)
     {
+        $this->hg = $hg;
+
         /* Attempt to set the executable */
         $this->setExecutable($path);
 
      */
     protected function setVersion()
     {
-        $this->version = $this->_base->version()->run('quiet');
-
-        //$command = new VersionControl_Hg_CommandProxy($this->_base);
-        //return
-        /*$version = call_user_func_array(
-            array($command, 'version'), array('quiet')
-        );*/
-
-        //$version2 = $command->version()->run('quiet');
-        //var_dump($command, $version, $version2);die;
-        // = $command->version()->run('quiet');
+        $this->version = $this->hg->version()->run();
     }
 
     /**
      * Get the version of Mercurial we are currently using
      *
+     * Because of circular dependcies, we have to call the command here
+     * instead of in the constructor, since $this->hg is not yet populated.
+     *
      * @return string
      */
     public function getVersion()
     {
+        $version = $this->hg->version()->run();
+
+        return $version['raw'];
+
         /*
          * I don't want programmers to have to test for null,
          * especially when this is auto-set in the constructor
             );
         }
 
-        return $this->version;
     }
 
     /**
         trigger_error('Clone is not allowed.', E_USER_ERROR);
     }
 
+    /**
+     * Get an unaccessible class property
+     *
+     * @param string $value The class property trying to be gotten
+     *
+     * @return string
+     */
+    public function __get($value)
+    {
+        $method = 'get' . ucfirst($value);
+
+        return $this->$method();
+    }
+
 }
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.