Author: Derick Rethans Date: 2006-10-09 13:59:39 +0200 (Mon, 09 Oct 2006) New Revision: 3664
Log: - Added the ezcBaseInit class that assists you by setting up on-demand configurations for objects (most notable useful for singleton classes). Added: trunk/Base/src/exceptions/init_callback_configured.php trunk/Base/src/init.php trunk/Base/src/interfaces/ trunk/Base/src/interfaces/configuration_initializer.php trunk/Base/tests/base_init_test.php Modified: trunk/Base/ChangeLog trunk/Base/src/base_autoload.php trunk/Base/tests/suite.php Modified: trunk/Base/ChangeLog =================================================================== --- trunk/Base/ChangeLog 2006-10-09 07:04:59 UTC (rev 3663) +++ trunk/Base/ChangeLog 2006-10-09 11:59:39 UTC (rev 3664) @@ -1,7 +1,11 @@ 1.2beta1 - [RELEASEDATE] ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ -- Added the class ezcFeatures to check for features of the enviroment. +- Added the ezcBaseFeatures class to check whether the current PHP + installation and environment provides features that can be used in the + components. +- Added the ezcBaseInit class that assists you by setting up on-demand + configurations for objects (most notable useful for singleton classes). - Implemented FR #8508: display search paths for the autoload files in case of a missing class. Modified: trunk/Base/src/base_autoload.php =================================================================== --- trunk/Base/src/base_autoload.php 2006-10-09 07:04:59 UTC (rev 3663) +++ trunk/Base/src/base_autoload.php 2006-10-09 11:59:39 UTC (rev 3664) @@ -8,20 +8,23 @@ */ return array( + 'ezcBaseConfigurationInitializer' => 'Base/interfaces/configuration_initializer.php', + 'ezcBaseFeatures' => 'Base/features.php', + 'ezcBaseInit' => 'Base/init.php', + 'ezcBaseOptions' => 'Base/options.php', + 'ezcBaseStruct' => 'Base/struct.php', + 'ezcBaseException' => 'Base/exceptions/exception.php', 'ezcBaseFileException' => 'Base/exceptions/file_exception.php', 'ezcBaseFileNotFoundException' => 'Base/exceptions/file_not_found.php', 'ezcBaseFileIoException' => 'Base/exceptions/file_io.php', 'ezcBaseFilePermissionException' => 'Base/exceptions/file_permission.php', + 'ezcBaseInitCallbackConfiguredException' => 'Base/exceptions/init_callback_configured.php', 'ezcBasePropertyPermissionException' => 'Base/exceptions/property_permission.php', 'ezcBasePropertyNotFoundException' => 'Base/exceptions/property_not_found.php', 'ezcBaseSettingNotFoundException' => 'Base/exceptions/setting_not_found.php', 'ezcBaseSettingValueException' => 'Base/exceptions/setting_value.php', 'ezcBaseValueException' => 'Base/exceptions/value.php', - - 'ezcBaseOptions' => 'Base/options.php', - 'ezcBaseFeatures' => 'Base/features.php', - 'ezcBaseStruct' => 'Base/struct.php', ); ?> Added: trunk/Base/src/exceptions/init_callback_configured.php =================================================================== --- trunk/Base/src/exceptions/init_callback_configured.php 2006-10-09 07:04:59 UTC (rev 3663) +++ trunk/Base/src/exceptions/init_callback_configured.php 2006-10-09 11:59:39 UTC (rev 3664) @@ -0,0 +1,31 @@ +<?php +/** + * File containing the ezcBaseInitCallbackConfiguredException class + * + * @package Base + * @version //autogen// + * @copyright Copyright (C) 2005, 2006 eZ systems as. All rights reserved. + * @license http://ez.no/licenses/new_bsd New BSD License + */ +/** + * ezcBaseInitCallbackConfiguredException is thrown when you try to assign a + * callback clasname to an identifier, while there is already a callback class + * configured for this identifier. + * + * @package Base + * @version //autogen// + */ +class ezcBaseInitCallbackConfiguredException extends ezcBaseException +{ + /** + * Constructs a new ezcBaseInitCallbackConfiguredException. + * + * @param string $identifier + * @param string $originalCallbackClassName + */ + function __construct( $identifier, $originalCallbackClassName ) + { + parent::__construct( "The <{$identifier}> is already configured with callback class <{$originalCallbackClassName}>." ); + } +} +?> Property changes on: trunk/Base/src/exceptions/init_callback_configured.php ___________________________________________________________________ Name: svn:eol-style + native Added: trunk/Base/src/init.php =================================================================== --- trunk/Base/src/init.php 2006-10-09 07:04:59 UTC (rev 3663) +++ trunk/Base/src/init.php 2006-10-09 11:59:39 UTC (rev 3664) @@ -0,0 +1,108 @@ +<?php +/** + * File containing the ezcBaseInit class. + * + * @package Base + * @version //autogentag// + * @copyright Copyright (C) 2005, 2006 eZ systems as. All rights reserved. + * @license http://ez.no/licenses/new_bsd New BSD License + */ +/** + * Provides a method to implement delayed initialization of objects. + * + * With the methods in this class you can implement callbacks to configure + * singleton classes. In order to do so you will have to change the + * getInstance() method of your singleton class to include a call to + * ezcBaseInit::fetchConfig() as in the following example: + * + * <code> + * <?php + * public static function getInstance() + * { + * if ( is_null( self::$instance ) ) + * { + * self::$instance = new ezcConfigurationmanager(); + * ezcBaseInit::fetchConfig( 'ConfigurationManagerConfig', self::$instance ); + * } + * return self::$instance; + * } + * ?> + * </code> + * + * You will also need to configure which callback class to call. This you do + * with the ezcBaseInit::setCallback() method. The following examples sets the + * callback classname for the configuration identifier + * 'ConfigurationManagerConfig' to 'cfgConfigurationManager': + * + * <code> + * <?php + * ezcBaseInit::setCallback( 'ConfigurationManagerConfig', 'cfgConfigurationManager' ); + * ?> + * </code> + * + * The class 'cfgConfigurationManager' is required to implement the + * ezcBaseConfigurationInitializer interface, which defines only one method: + * configureObject(). An example on how to implement such a class could be: + * + * <code> + * <?php + * class cfgConfigurationManager + * { + * static public function configureObject( ezcConfigurationManager $cfgManagerObject ) + * { + * $cfgManagerObject->init( 'ezcConfigurationIniReader', 'settings', array( 'useComments' => true ) ); + * } + * } + * ?> + * </code> + * + * Ofcourse the implementation of this callback class is up to the application + * developer that uses the component (in this example the Configuration + * component's class ezcConfigurationManager). + * + * @package Base + * @version //autogentag// + */ +class ezcBaseInit +{ + /** + * Contains the callback where the identifier is the key of the array, and the classname to callback to the value. + * + * @var array(string=>string) + */ + static private $callbackMap = array(); + + /** + * Adds the classname $callbackClassname as callback for the identifier $identifier. + * + * @param string $identifier + * @param string $callbackClassname + */ + public static function setCallback( $identifier, $callbackClassname ) + { + if ( array_key_exists( $identifier, self::$callbackMap ) ) + { + throw new ezcBaseInitCallbackConfiguredException( $identifier, self::$callbackMap[$identifier] ); + } + else + { + self::$callbackMap[$identifier] = $callbackClassname; + } + } + + /** + * Uses the configured callback belonging to $identifier to configure the $object. + * + * @param string $identifier + * @param object $object + */ + public static function fetchConfig( $identifier, $object ) + { + if ( isset( self::$callbackMap[$identifier] ) ) + { + $callbackClassname = self::$callbackMap[$identifier]; + call_user_func( array( $callbackClassname, 'configureObject' ), $object ); + } + } +} +?> Property changes on: trunk/Base/src/init.php ___________________________________________________________________ Name: svn:eol-style + native Added: trunk/Base/src/interfaces/configuration_initializer.php =================================================================== --- trunk/Base/src/interfaces/configuration_initializer.php 2006-10-09 07:04:59 UTC (rev 3663) +++ trunk/Base/src/interfaces/configuration_initializer.php 2006-10-09 11:59:39 UTC (rev 3664) @@ -0,0 +1,30 @@ +<?php +/** + * File containing the ezcBaseConfigurationInitializer class + * + * @package Base + * @version //autogen// + * @copyright Copyright (C) 2005, 2006 eZ systems as. All rights reserved. + * @license http://ez.no/licenses/new_bsd New BSD License + */ +/** + * This class provides the interface that classes need to implement to act as an + * callback initializer class to work with the delayed initialization mechanism. + * + * @package Base + * @version //autogen// + */ +interface ezcBaseConfigurationInitializer +{ + /** + * Sets the options for the writer. + * + * The options will be used the next time the save() method is called. The + * $options array is an associative array with the options for the writer. + * It depends on the specific writer which options are allowed here. + * + * @param array $options + */ + static public function configureObject( $object ); +} +?> Property changes on: trunk/Base/src/interfaces/configuration_initializer.php ___________________________________________________________________ Name: svn:eol-style + native Added: trunk/Base/tests/base_init_test.php =================================================================== --- trunk/Base/tests/base_init_test.php 2006-10-09 07:04:59 UTC (rev 3663) +++ trunk/Base/tests/base_init_test.php 2006-10-09 11:59:39 UTC (rev 3664) @@ -0,0 +1,75 @@ +<?php +/** + * @package Base + * @subpackage Tests + * @version //autogentag// + * @copyright Copyright (C) 2005, 2006 eZ systems as. All rights reserved. + * @license http://ez.no/licenses/new_bsd New BSD License + */ +/** + * @package Base + * @subpackage Tests + */ +class ezcBaseInitTest extends ezcTestCase +{ + public function setUp() + { + testBaseInitClass::$instance = null; + } + + public function testCallback1() + { + $obj = testBaseInitClass::getInstance(); + $this->assertEquals( false, $obj->configured ); + } + + public function testCallback2() + { + ezcBaseInit::setCallback( 'testBaseInit', 'testBaseInitCallback' ); + $obj = testBaseInitClass::getInstance(); + $this->assertEquals( true, $obj->configured ); + } + + public function testCallback3() + { + try + { + ezcBaseInit::setCallback( 'testBaseInit', 'testBaseInitCallback' ); + $this->fail( "Expected exception not thrown." ); + } + catch ( ezcBaseInitCallbackConfiguredException $e ) + { + $this->assertEquals( "The <testBaseInit> is already configured with callback class <testBaseInitCallback>.", $e->getMessage() ); + } + } + + public static function suite() + { + return new PHPUnit_Framework_TestSuite("ezcBaseInitTest"); + } +} + +class testBaseInitCallback implements ezcBaseConfigurationInitializer +{ + static public function configureObject( $objectToConfigure ) + { + $objectToConfigure->configured = true; + } +} + +class testBaseInitClass +{ + public $configured = false; + public static $instance; + + public static function getInstance() + { + if ( is_null( self::$instance ) ) + { + self::$instance = new testBaseInitClass(); + ezcBaseInit::fetchConfig( 'testBaseInit', self::$instance ); + } + return self::$instance; + } +} +?> Property changes on: trunk/Base/tests/base_init_test.php ___________________________________________________________________ Name: svn:eol-style + native Modified: trunk/Base/tests/suite.php =================================================================== --- trunk/Base/tests/suite.php 2006-10-09 07:04:59 UTC (rev 3663) +++ trunk/Base/tests/suite.php 2006-10-09 11:59:39 UTC (rev 3664) @@ -8,6 +8,7 @@ */ require_once( "base_test.php"); +require_once( "base_init_test.php"); require_once( "features_test.php"); /** @@ -22,6 +23,7 @@ $this->setName("Base"); $this->addTest( ezcBaseTest::suite() ); + $this->addTest( ezcBaseInitTest::suite() ); $this->addTest( ezcBaseFeaturesTest::suite() ); } -- svn-components mailing list svn-components@lists.ez.no http://lists.ez.no/mailman/listinfo/svn-components