Zend Framework

Autoloading Zend Framework Like Classes In Kohana v3 – Enhanced

I have posted few days ago about Autoloading Zend Framework-like classes in Kohana v3 and think it was good. But after several iterations, I found out that it can be improved further. Now, it is fully infected with Zend Framework style – since I added a new method for registering namespaces as what Zend Framework does on its autoloading utility.

It was just a small modifications where I injected a notion of namespaces – (it does not mean PHP namespace, in Zend Framework, it is some sort of class prefix or name prefix or vendor prefix). In Zend Framework, the Zend prefix is autoloaded by default. If you have a custom library, you need to add your library class prefix and register it as namespace.

So in our Kohana v3, it is going to be something like this:

The Modifications

I override the Kohana core using the kohana.php file inside the application/class directory. I will show it later.

Modify Bootstrap

We need to add this line in the kohana’s bootstrap file at application/bootstrap.php, just before the ini_set() call.

/**
 * Register namespaces here to autoload Zend Framework like classes
 * Those classes must have vendor prefix, for example:
 * My_Super_Class, the prefix is 'My_'
 * Register the prefix so that native kohana autoloading will not be used
 * These classes must exists only in application directory (APPPATH)
 */
Kohana::registerNamespace('Dc_');
Kohana::registerNamespace('Zend_');

Now, this is our kohana core file at application/classes/kohana.php. I just added a static array which will contain the namespaces (class prefix), so that if it exists in the list, it will only look at the application directory using the Zend Framework file naming style instead of using the previous which searches the whole tree with both naming styles (kohana and Zend Framework) – which is inefficient.

<?php defined('SYSPATH') or die('No direct script access.');

/**
 * Overrides kohana core class to provide
 * custom features
 */
class Kohana extends Kohana_Core
{
	/**
	 * Registered namespaces for loading
	 * Zend Framework like classes
	 *
	 * @var array
	 */
	protected static $_namespaces = array();
	
	/**
	 * Adds a namespace to autoload zend framework like classes
	 *
	 * @param string $namespace
	 * @return boolean
	 */
	public static function registerNamespace($namespace)
	{
		if (!self::isRegistered($namespace))
		{
			self::$_namespaces&#91;&#93; = $namespace;
		}
	}
	
	/**
	 * Returns true if and only if $namespace is already
	 * registered from the namespace stack
	 *
	 * @param string $namespace
	 * @return boolean
	 */
	public static function isRegistered($namespace)
	{
		if (in_array($namespace, self::$_namespaces))
		{
			return true;
		}
		
		return false;
	}
	
	/**
	 * Overrides kohana core's autoloading mechanism
	 * to allow autoloading of Zend Framework like classes
	 * to be used along with kohana classes
	 *
	 * @param string $class
	 * @return boolean
	 */
	public static function auto_load($class)
	{
		// get the first chunck of the class name to get
		// the class vendor prefix
		
		$prefix = '';
		$upos = strpos($class, '_');
		if ($upos !== false && $upos > 0)
		{
			// check if the class prefix is registered
			$prefix = substr($class, 0, $upos + 1);
			if (self::isRegistered($prefix))
			{
				// autoload Zend Framework like classes
				$file = str_replace('_', '/', $class);
				if ($path = Kohana::find_file('classes', $file))
				{
					// Load the class file
					require $path;
		
					// Class has been found
					return TRUE;
				}
				
				// if not found, since registered, we will not let kohana
				// find it using its own naming convention,
				// and just return false
				return FALSE;
			}
		}
		
		// use first kohana's autoloading
		// Transform the class name into a path
		$file = str_replace('_', '/', strtolower($class));

		if ($path = Kohana::find_file('classes', $file))
		{
			// Load the class file
			require $path;

			// Class has been found
			return TRUE;
		}
		
		// Class is not in the filesystem
		return FALSE;
	}
}

NOTE: All classes the will be in Zend Framework style names should be contained in a library style structure, such as when you’re creating your own library. This should not be used to replace the Kohana’s applications files such as controllers, views, models etc.

Leave a reply

Your email address will not be published. Required fields are marked *