ZF-35: ZF Version check (TRAC#22)

Description

http://framework.zend.com/developer/ticket/22

sometimes it is a must to check, what ZF version is running, where your applications shares one ZF installation ..

it would be nice, to have a method, where you can check the installed ZF version against a required version,

e.g. if ZendVersion::check("0.3.15") then output "need at least ZF 0.3.15" -- die();


    class ZendVersion
    {
        static $version = "0.1.13";
        function check($version, $strict = false)
        {
            list($main, $major, $minor) = explode(".", $version);
            list($zf_main, $zf_major, $zf_minor) = explode(".", ZendVersion::$version);
            if (!$strict) {
                return ($minor*10+$major*1000+$main*10000) <= ($zf_minor*10+$zf_major*1000+$zf_main*10000); 
            } else {
                return ($minor*10+$major*1000+$main*10000) == ($zf_minor*10+$zf_major*1000+$zf_main*10000);
            }
        }
    }

simple test


    $tests = array();
    $tests["0.1.12"] = true;
    $tests["0.1.13"] = true;
    $tests["0.1.14"] = false;
    $tests["1.1.14"] = false;
    echo "ZF version " . ZendVersion::$version . "\n";
    foreach ($tests as $key=>$value)
    {
        echo "checking for '" . $key . "' ";
        if ($value == true)
        {
            echo "true .. ? ";
        } else
        {
            echo "false .. ? ";
        }
        if ($value == ZendVersion::check($key))
        {
            echo "OK";
        } else
        {
            echo "FAILED";
        }
        echo "\n";
    }

test output


ZF version 0.1.13
checking for '0.1.12' true .. ? OK
checking for '0.1.13' true .. ? OK
checking for '0.1.14' false .. ? OK
checking for '1.1.14' false .. ? OK

Comments

Changing fix version to 0.6.0.

Here are some thoughts:

  • This class should be Zend_Version; the file Version.php should live under the Zend directory.
  • This class should be final; there is no reason to subclass it.
  • check() method should not assume version strings are in NN.NN.NN format. It's possible that version strings may be more complex. I suggest splitting on any non-alphanumeric character into an array, not three scalars.
  • Need method isEqual($version, $strict = false)
  • Need method isGreaterThan($version)
  • Need method isLessThan($version)

Reformat code in description to be less wide.

Hmmm .. I have a different, simpler approach, requiring less code. If anyone objects, please let me know asap. See the Fisheye link for the changeset and patch.