Web application versioning when using Zend Framework

This is a simple way to versioning a zend framework application using just two files, a ini file that keeps the current version and a proxy/model file wich load the current version from the ini file and test the user passed version for a new feature. The ini file is splited in three states: production, testing and development.


[production]

; 1.1.1 -
; 1.2.0 - From this we add video section
; 1.0.0 - First release of the site

version = "1.0.0";

[testing : production]

version = "1.0.0";

[development : production]

version = "1.0.0";

The good think about this is that you can set a version for each stage of application. While application is in development mode, just set a constant for enviroement and current version will be that from development block. The class wich handle this operations is a very simple singleton class with “version” method.


define('APPLICATION_ENV', 'development');

class Proxy_Version
{
	private static $_instance = null;

	private $versions;

    /**
     * Singleton instance
     * @abstract
     * @return Proxy_Version
     */
    public static function getInstance()
    {
        if (null === self::$_instance)
        {
            self::$_instance = new self();
        }

        return self::$_instance;
	}

	private function __construct()
	{
		$this->versions = new Zend_Config_Ini('version.ini', APPLICATION_ENV);
	}

    public static function version( $version = null )
    {
        self::getInstance();

    	$currentVersion = self::$versions->version;

    	$userVersion = false;//if you have stored in db the version for each user

    	if( $userVersion !== false )
    	{
    		$currentVersion = $userVersion;
    	}

    	if($version)
    	{
    		return version_compare($currentVersion, $version, ">=");
    	}

    	return $currentVersion;
    }
}

Also if you look closer in this class you can set a user version, for example when you want to let a limited numbers of users to test the next version of the application. To be more easy to use you can apply a shortcut in a view helper and access it with $this->version().


//context of version 1.0

if($this->version("1.2"))
{
//content of version 1.2
}

//context of version 1.0

echo $this->version();

Or you can set a shortcut in a abstract zend controller action and load views depentds of version method.


//controller class
//....
public function indexAction()
{
  if($this->version("1.2"))
    $this->render("index_1.2.phtml");
}
//....

Leave a Comment.