Perforce Chronicle 2012.2/486814
API Documentation
|
Modules are containers for functionality. More...
Public Member Functions | |
areDependenciesSatisfied () | |
Determine if all of the module dependencies are satisfied and the module can be enabled. | |
callIntegrationMethod ($method, $params=array()) | |
Call a static method on the integration class. | |
canDisable () | |
Check if the module can be disabled. | |
canEnable () | |
Check if the module can be enabled. | |
disable () | |
Disable this module. | |
enable () | |
Enable this module for the current site. | |
getBaseUrl () | |
Get the url to this module's resources folder. | |
getConfig () | |
Get the configuration of this module. | |
getConfigUri () | |
Get the URI to configure this module. | |
getDependencies () | |
Get any dependencies that this module has on other packages. | |
getIntegrationClassName () | |
Get the name of the corresponding integration class for this module. | |
getName () | |
Get the name of this package. | |
getRouteFormattedName () | |
Get the route-formatted name of this module (e.g. | |
getRoutes () | |
Get any routes configured for this module. | |
hasDependencies () | |
Determine if the module has any dependencies. | |
hasDependents () | |
Determine if any other enabled modules depend upon this module. | |
hasIntegrationClass () | |
Determine if this module has a corresponding static module integration class. | |
hasIntegrationMethod ($method) | |
Determine whether an integration class method exists. | |
init () | |
Initialize the module in the environment. | |
isConfigurable () | |
Determine if this module is configurable. | |
isCoreModule () | |
Determine if this module is a core module. | |
isEnabled () | |
Check if this module is enabled. | |
isLoaded () | |
Get the flag indicating loaded status. | |
load () | |
Attempt to load this module. | |
readOnly () | |
Throws read-only exceptions on behalf of most mutators. | |
saveConfig ($config=null, $description=null) | |
Save the configuration of this module. | |
setConfig ($config) | |
Set the configuration of this module. | |
setName ($name) | |
Set the name of this package. | |
setPath ($path) | |
Set the full path to the package folder. | |
Static Public Member Functions | |
static | clearConfigCache () |
Clear the config records cache. | |
static | fetchAllCore () |
Get all core modules. | |
static | fetchAllDisabled () |
Get all modules that are disabled. | |
static | fetchAllEnabled ($excludeCoreModules=false) |
Get all modules that are enabled - includes core modules unless $excludeCoreModules is set to true. | |
static | getCoreModulesPath () |
Get the path to the core modules. | |
static | getPackagesPaths () |
Extends parent to add core modules path last. | |
static | isDependencySatisfied ($name, $type, $versions) |
Check if the given dependency is satisified. | |
static | reset () |
Clear the config cache, modules paths and the list of loaded and initialized modules. | |
static | setCoreModulesPath ($path) |
Set the path to the core modules. | |
Public Attributes | |
const | CONFIG_RECORD_PATH = 'config/module' |
const | PACKAGE_FILENAME = 'module.ini' |
Protected Member Functions | |
_ensureDisabled () | |
Ensure that this module is disabled. | |
_ensureEnabled () | |
Ensure that this module is enabled. | |
_getConfigRecord () | |
Get the instance copy of the config record associated with this module. | |
_getConfigRecords () | |
Get all module config records from storage. | |
_hasStoredConfigRecord () | |
Determine if this module has a configuration record in storage. | |
Protected Attributes | |
$_configFolderName = 'module' | |
$_configRecord = null | |
Static Protected Attributes | |
static | $_configRecords = null |
static | $_coreModulesPath = null |
static | $_dependencyTypes = array('library', 'module') |
static | $_fields |
static | $_idField = 'name' |
static | $_isInitialized = array() |
static | $_isLoaded = array() |
static | $_packagesPaths = array() |
Modules are containers for functionality.
The module model provides access to modules so that they can be listed and interrogated. Module models are read-only.
Modules may contain a static module class 'Module.php', (aka the module integration class) which can provide functionality and integrate the module with the rest of the system. The module integration class name should be begin with the name of the module, followed by an underscore and the word Module (e.g. 'Foo_Module').
To use modules, you are required to indicate where "core" modules reside via the setCoreModulesPath() method. Additional paths can be added via addPackagesPath(). The order that paths are added is significant. If a module exists in two or more paths, the path that was added last takes precedence, unless it is a core module. Core modules are always loaded from the core modules path.
P4Cms_Module::_ensureDisabled | ( | ) | [protected] |
Ensure that this module is disabled.
P4Cms_Module_Exception | if the module is not disabled. |
{ if ($this->isEnabled()) { throw new P4Cms_Module_Exception( "The '" . $this->getName() . "' module is not disabled." ); } }
P4Cms_Module::_ensureEnabled | ( | ) | [protected] |
Ensure that this module is enabled.
P4Cms_Module_Exception | if the module is not enabled. |
{ if (!$this->isEnabled()) { throw new P4Cms_Module_Exception( "The '" . $this->getName() . "' module is a not enabled." ); } }
P4Cms_Module::_getConfigRecord | ( | ) | [protected] |
Get the instance copy of the config record associated with this module.
If no record exists in storage, one will be created in memory.
{ // get/make instance copy of config record if necessary. if ($this->_configRecord === null) { if ($this->_hasStoredConfigRecord()) { $records = $this->_getConfigRecords(); $this->_configRecord = clone $records[$this->getName()]; } else { $this->_configRecord = new P4Cms_Record_Config; $this->_configRecord->setId(self::CONFIG_RECORD_PATH . '/' . $this->getName()); } } return $this->_configRecord; }
P4Cms_Module::_getConfigRecords | ( | ) | [protected] |
Get all module config records from storage.
Provides localized cache of records to avoid repeatedly fetching records from Perforce. Records are keyed on module name for quick/easy lookups.
{ // only fetch config records once. if (static::$_configRecords === null) { $records = P4Cms_Record_Config::fetchAll( P4Cms_Record_Query::create()->addPath(self::CONFIG_RECORD_PATH .'/...') ); // store records keyed on module name. static::$_configRecords = array(); foreach ($records as $record) { static::$_configRecords[basename($record->getId())] = $record; } } return static::$_configRecords; }
P4Cms_Module::_hasStoredConfigRecord | ( | ) | [protected] |
Determine if this module has a configuration record in storage.
This method will only return true if the record is in storage.
{ return array_key_exists($this->getName(), $this->_getConfigRecords()); }
P4Cms_Module::areDependenciesSatisfied | ( | ) |
Determine if all of the module dependencies are satisfied and the module can be enabled.
{ // if no dependencies, can be enabled. if (!$this->hasDependencies()) { return true; } // check if required modules are installed and enabled. foreach ($this->getDependencies() as $dependency) { extract($dependency); if (!static::isDependencySatisfied($name, $type, $versions)) { return false; } } // all dependencies are satisfied - can enable! return true; }
P4Cms_Module::callIntegrationMethod | ( | $ | method, |
$ | params = array() |
||
) |
Call a static method on the integration class.
string | $method | the name of the method to call. |
array | $params | optional - the arguments to pass to the method. |
P4Cms_Module_Exception | if the integration class or method does not exist. |
{ // ensure module is enabled. $this->_ensureEnabled(); if (!$this->hasIntegrationClass()) { throw new P4Cms_Module_Exception("Can't call integration method: '" . $method . "'. " . $this->getName() . " module does not have an integration class."); } if (!method_exists($this->getIntegrationClassName(), $method)) { throw new P4Cms_Module_Exception("Can't call integration method: '" . $method . "'. " . $this->getName() . " module does not have this method."); } return call_user_func_array(array($this->getIntegrationClassName(), $method), $params); }
P4Cms_Module::canDisable | ( | ) |
Check if the module can be disabled.
{ if ($this->isCoreModule() || !$this->isEnabled() || $this->hasDependents()) { return false; } else { return true; } }
P4Cms_Module::canEnable | ( | ) |
Check if the module can be enabled.
{ if ($this->isCoreModule() || $this->isEnabled() || !$this->areDependenciesSatisfied()) { return false; } else { return true; } }
static P4Cms_Module::clearConfigCache | ( | ) | [static] |
Clear the config records cache.
{ static::$_configRecords = null; }
P4Cms_Module::disable | ( | ) |
Disable this module.
P4Cms_Module_Exception | if the module cannot be disabled. |
{ // ensure module is enabled to begin with. $this->_ensureEnabled(); // ensure module is not a core module. if ($this->isCoreModule()) { throw new P4Cms_Module_Exception("The '" . $this->getName() . "' module is a core module. Core modules cannot be disabled."); } // ensure dependencies are respected. if ($this->hasDependents()) { throw new P4Cms_Module_Exception("Can't disable the '" . $this->getName() . "' module. One or more enabled modules depend upon it."); } // call the 'disable' integration method if defined. if ($this->hasIntegrationMethod('disable')) { $this->callIntegrationMethod('disable'); } // disable module by deleting config record. if ($this->_hasStoredConfigRecord()) { $this->_getConfigRecord()->delete("Disabled '" . $this->getName() . "' module."); unset(static::$_configRecords[$this->getName()]); } return $this; }
P4Cms_Module::enable | ( | ) |
Enable this module for the current site.
P4Cms_Module_Exception | if the module cannot be enabled. |
{ // ensure module is disabled. $this->_ensureDisabled(); // ensure dependencies are satisfied. if (!$this->areDependenciesSatisfied()) { throw new P4Cms_Module_Exception("Can't enable the '" . $this->getName() . "' module. One or more dependencies are not satisfied."); } // enable module by saving its configuration. // this will restore the previous config if one exists. $this->saveConfig(null, "Enabled '" . $this->getName() . "' module."); // initialize the module in the environment. $this->init(); try { if ($this->hasIntegrationMethod('enable')) { $this->callIntegrationMethod('enable'); } } catch (Exception $e) { $message = "Failed to enable the '" . $this->getName() . "' module."; P4Cms_Log::logException($message, $e); $this->disable(); throw new P4Cms_Module_Exception($message); } return $this; }
static P4Cms_Module::fetchAllCore | ( | ) | [static] |
Get all core modules.
{ $modules = new P4Cms_Model_Iterator; foreach (static::fetchAll() as $module) { if ($module->isCoreModule()) { $modules[] = $module; } } return $modules; }
static P4Cms_Module::fetchAllDisabled | ( | ) | [static] |
Get all modules that are disabled.
{ $modules = new P4Cms_Model_Iterator; foreach (static::fetchAll() as $module) { if (!$module->isEnabled()) { $modules[] = $module; } } return $modules; }
static P4Cms_Module::fetchAllEnabled | ( | $ | excludeCoreModules = false | ) | [static] |
Get all modules that are enabled - includes core modules unless $excludeCoreModules is set to true.
bool | $excludeCoreModules | optional - defaults to false - set to true to exclude core modules from the set of returned modules. |
{ $modules = new P4Cms_Model_Iterator; foreach (static::fetchAll() as $module) { if ((!$module->isCoreModule() || !$excludeCoreModules) && $module->isEnabled()) { $modules[] = $module; } } return $modules; }
P4Cms_Module::getBaseUrl | ( | ) |
Get the url to this module's resources folder.
Reimplemented from P4Cms_PackageAbstract.
{ return parent::getBaseUrl() . '/resources'; }
P4Cms_Module::getConfig | ( | ) |
Get the configuration of this module.
This returns a Zend_Config object which can contain any configuration information the module developer chooses to store.
{ return $this->_getConfigRecord()->getConfig(); }
P4Cms_Module::getConfigUri | ( | ) |
Get the URI to configure this module.
{ if (!$this->isConfigurable()) { throw new P4Cms_Module_Exception( "Cannot get URI to configure '" . $this->getName() . "'. The module is not configurable." ); } // if no module is specified, default to this module. $info = $this->getPackageInfo(); $routeParams = $info['configure']; if (!isset($routeParams['module'])) { $routeParams['module'] = $this->getRouteFormattedName(); } // return assembled uri. $router = Zend_Controller_Front::getInstance()->getRouter(); return $router->assemble($routeParams, null, true); }
static P4Cms_Module::getCoreModulesPath | ( | ) | [static] |
Get the path to the core modules.
{ if (!static::$_coreModulesPath) { throw new P4Cms_Module_Exception("Can't get core modules. Path is unset."); } return static::$_coreModulesPath; }
P4Cms_Module::getDependencies | ( | ) |
Get any dependencies that this module has on other packages.
{ $info = $this->getPackageInfo(); if (!isset($info['dependencies']) || !is_array($info['dependencies'])) { return array(); } // normalize/flatten dependencies. $dependencies = array(); foreach (static::$_dependencyTypes as $type) { if (!isset($info['dependencies'][$type]) || !is_array($info['dependencies'][$type])) { continue; } // convert comma-separated version list to array form. foreach ($info['dependencies'][$type] as $name => $versions) { $dependencies[] = array( 'name' => $name, 'type' => $type, 'versions' => str_getcsv($versions, ',') ); } } return $dependencies; }
P4Cms_Module::getIntegrationClassName | ( | ) |
Get the name of the corresponding integration class for this module.
{ return $this->getName() . '_Module'; }
P4Cms_Module::getName | ( | ) |
Get the name of this package.
Extends parent to upper-case the first letter.
Reimplemented from P4Cms_PackageAbstract.
{
return ucfirst(parent::getName());
}
static P4Cms_Module::getPackagesPaths | ( | ) | [static] |
Extends parent to add core modules path last.
Reimplemented from P4Cms_PackageAbstract.
{ $paths = static::$_packagesPaths; $paths[] = static::getCoreModulesPath(); return $paths; }
P4Cms_Module::getRouteFormattedName | ( | ) |
Get the route-formatted name of this module (e.g.
foo-bar instead of FooBar).
{ return P4Cms_Controller_Router_Route_Module::formatRouteParam( $this->getName() ); }
P4Cms_Module::getRoutes | ( | ) |
Get any routes configured for this module.
{ $info = $this->getPackageInfo(); return isset($info['routes']) && is_array($info['routes']) ? $info['routes'] : array(); }
P4Cms_Module::hasDependencies | ( | ) |
Determine if the module has any dependencies.
{ return (bool) count($this->getDependencies()); }
P4Cms_Module::hasDependents | ( | ) |
Determine if any other enabled modules depend upon this module.
{ // check if any enabled modules are dependent on this module. foreach (P4Cms_Module::fetchAllEnabled() as $module) { if (!$module->hasDependencies()) { continue; } $dependencies = $module->getDependencies(); foreach ($dependencies as $dependency) { if ($dependency['type'] === 'module' && $dependency['name'] == $this->getName() ) { return true; } } } return false; }
P4Cms_Module::hasIntegrationClass | ( | ) |
Determine if this module has a corresponding static module integration class.
{ // ensure module is enabled. $this->_ensureEnabled(); if (is_file($this->getPath() . '/Module.php') && class_exists($this->getIntegrationClassName())) { return true; } else { return false; } }
P4Cms_Module::hasIntegrationMethod | ( | $ | method | ) |
Determine whether an integration class method exists.
string | $method | the name of the method to find. |
{ // ensure module is enabled. $this->_ensureEnabled(); if (!$this->hasIntegrationClass()) { return false; } if (!method_exists($this->getIntegrationClassName(), $method)) { return false; } return true; }
P4Cms_Module::init | ( | ) |
Initialize the module in the environment.
This method only needs to be called once per-module, per-request. to register the module with the application.
{ // ensure module is enabled. $this->_ensureEnabled(); // don't initialize modules twice if ($this->_isInitialized()) { return $this; } // an instance of the view is required to properly initialize a module. if (!Zend_Layout::getMvcInstance()) { Zend_Layout::startMvc(); } $view = Zend_Layout::getMvcInstance()->getView(); if (!$view instanceof Zend_View) { throw new P4Cms_Module_Exception("Can't initialize module. Failed to get view instance."); } // add module to loader so that module classes will auto-load. P4Cms_Loader::addPackagePath($this->getName(), $this->getPath()); // add module's controllers directory to front controller so that requests will route. Zend_Controller_Front::getInstance()->addControllerDirectory( $this->getPath() . '/controllers', basename($this->getPath()) ); // each module can have layouts - add layout script path. if (is_dir($this->getPath() . '/layouts/scripts')) { $view->addScriptPath($this->getPath() . '/layouts/scripts'); } // each module can have helpers (under views or layouts) - add helper paths. if (is_dir($this->getPath() . '/views/helpers')) { $view->addHelperPath( $this->getPath() . '/views/helpers/', $this->getName() . '_View_Helper_' ); } if (is_dir($this->getPath() . '/layouts/helpers')) { $view->addHelperPath( $this->getPath() . '/layouts/helpers/', $this->getName() . '_View_Helper_' ); } // each module can have form plugins: // elements, decorators, validators and filters. $plugins = array( array(Zend_Form::ELEMENT, '/forms/elements/', 'Form_Element'), array(Zend_Form::DECORATOR, '/forms/decorators/', 'Form_Decorator'), array(Zend_Form_Element::VALIDATE, '/validators/', 'Validate'), array(Zend_Form_Element::FILTER, '/filters/', 'Filter') ); foreach ($plugins as $plugin) { if (is_dir($this->getPath() . $plugin[1])) { P4Cms_Form::registerPrefixPath( $this->getName() . "_" . $plugin[2], $this->getPath() . $plugin[1], $plugin[0] ); } } // each module can have controller action helpers (under controllers) if (is_dir($this->getPath() . '/controllers/helpers')) { Zend_Controller_Action_HelperBroker::addPath( $this->getPath() . '/controllers/helpers/', $this->getName() . '_Controller_Helper_' ); } // call the 'init' integration method if defined. try { if ($this->hasIntegrationMethod('init')) { $this->callIntegrationMethod('init'); } } catch (Exception $e) { P4Cms_Log::logException("Failed to init the '" . $this->getName() . "' module.", $e); } // flag the module as initialized $this->_setInitialized(); return $this; }
P4Cms_Module::isConfigurable | ( | ) |
Determine if this module is configurable.
Considered configurable if the module.ini file specifies a configure controller.
{ // ensure module is enabled. $this->_ensureEnabled(); $info = $this->getPackageInfo(); return isset($info['configure']['controller']) && is_array($info['configure']); }
P4Cms_Module::isCoreModule | ( | ) |
Determine if this module is a core module.
{ return (dirname($this->getPath()) == $this->getCoreModulesPath()); }
static P4Cms_Module::isDependencySatisfied | ( | $ | name, |
$ | type, | ||
$ | versions | ||
) | [static] |
Check if the given dependency is satisified.
Versions may be specified using wildcards such as '*' and '?'. Matching is performed using fnmatch() and therefore supports all of the same shell patterns recognized by that function.
string | $name | the name of the required package. |
string | $type | the type of dependency (module or library) |
string | array | $versions | suitable versions of the package. |
{ // dependency name and a valid type must be specified. if (!isset($name) || !in_array($type, static::$_dependencyTypes)) { return false; } // accept single version string or array of versions. $versions = (array) $versions; // differentiate module and library dependency checking. if ($type === 'library') { $className = $name . "_Version"; if (!class_exists($className)) { return false; } $version = $className::VERSION; } else if ($type === 'module') { // check if the named module exists. if (!P4Cms_Module::exists($name)) { return false; } // fetch the named module and check if it's enabled. $module = P4Cms_Module::fetch($name); if (!$module->isEnabled()) { return false; } $version = $module->getVersion(); } // check version. foreach ($versions as $suitableVersion) { if (fnmatch($suitableVersion, $version)) { return true; } } return false; }
P4Cms_Module::isEnabled | ( | ) |
Check if this module is enabled.
A module is considered enabled if it has a config record in storage.
{ // core modules are always enabled. if ($this->isCoreModule()) { return true; } // check for stored config record. return $this->_hasStoredConfigRecord(); }
P4Cms_Module::isLoaded | ( | ) |
Get the flag indicating loaded status.
{ return isset(static::$_isLoaded[$this->getName()]); }
P4Cms_Module::load | ( | ) |
Attempt to load this module.
{ // ensure module is enabled. $this->_ensureEnabled(); // don't load modules twice (without unloading first) if ($this->isLoaded()) { return $this; } // add module meta, stylesheets and scripts to view. $this->_loadHtmlMeta(); $this->_loadStylesheets(); $this->_loadScripts(); // load dojo components. $this->_loadDojo(); // if the module is a route provider add the routes to the router. $router = Zend_Controller_Front::getInstance()->getRouter(); $router->addConfig(new Zend_Config($this->getRoutes())); try { if ($this->hasIntegrationMethod('load')) { $this->callIntegrationMethod('load'); } $this->_setLoaded(); } catch (Exception $e) { P4Cms_Log::logException("Failed to load the '" . $this->getName() . "' module.", $e); } return $this; }
P4Cms_Module::readOnly | ( | ) |
Throws read-only exceptions on behalf of most mutators.
P4Cms_Module_Exception | Every time; configured for specific mutators. |
{ throw new P4Cms_Module_Exception('P4Cms_Module is primarily a read-only class.'); }
static P4Cms_Module::reset | ( | ) | [static] |
Clear the config cache, modules paths and the list of loaded and initialized modules.
This is useful for testing purposes.
{ static::$_isLoaded = array(); static::$_isInitialized = array(); static::$_coreModulesPath = null; static::clearPackagesPaths(); static::clearConfigCache(); }
P4Cms_Module::saveConfig | ( | $ | config = null , |
$ | description = null |
||
) |
Save the configuration of this module.
Zend_Config | $config | optional - the configuration to save. |
string | $description | optional - a description of the change. |
P4Cms_Record_Exception | if an invalid config object is given. |
{ $record = $this->_getConfigRecord(); // if config is given, set it. if ($config) { $record->setConfig($config); } // if no description is given, generate one. if (!$description) { $description = "Saved configuration for '" . $this->getName() . "' module."; } // save config record. $record->save($description); static::$_configRecords[$this->getName()] = $record; return $this; }
P4Cms_Module::setConfig | ( | $ | config | ) |
Set the configuration of this module.
This does not save the configuration. You must call saveConfig() to store the configuration persistently.
Zend_Config | $config | the configuration to set. |
InvalidArgumentException | if the given config is invalid. |
{ $this->_getConfigRecord()->setConfig($config); return $this; }
static P4Cms_Module::setCoreModulesPath | ( | $ | path | ) | [static] |
Set the path to the core modules.
string | $path | the path to the core modules. |
{ static::$_coreModulesPath = $path; }
P4Cms_Module::setName | ( | $ | name | ) |
Set the name of this package.
string | $name | The new name to use. |
{ return $this->_setValue('name'); }
P4Cms_Module::setPath | ( | $ | path | ) |
Set the full path to the package folder.
Extends parent to ensure that basename is valid in a class name.
string | $path | the full path to the package folder. |
Reimplemented from P4Cms_PackageAbstract.
{ // validate module name - only allow characters valid in a class name. if (preg_match('/[^a-z0-9\\/\\\\_.-]/i', basename($path))) { throw new P4Cms_Module_Exception( "Invalid module ('" . $name . "'). " . "The module name contains illegal characters." ); } return parent::setPath($path); }
P4Cms_Module::$_configFolderName = 'module' [protected] |
P4Cms_Module::$_configRecord = null [protected] |
P4Cms_Module::$_configRecords = null [static, protected] |
P4Cms_Module::$_coreModulesPath = null [static, protected] |
P4Cms_Module::$_dependencyTypes = array('library', 'module') [static, protected] |
P4Cms_Module::$_fields [static, protected] |
Reimplemented from P4Cms_Model.
P4Cms_Module::$_idField = 'name' [static, protected] |
Reimplemented from P4Cms_Model.
P4Cms_Module::$_isInitialized = array() [static, protected] |
P4Cms_Module::$_isLoaded = array() [static, protected] |
P4Cms_Module::$_packagesPaths = array() [static, protected] |
Reimplemented from P4Cms_PackageAbstract.
const P4Cms_Module::CONFIG_RECORD_PATH = 'config/module' |
const P4Cms_Module::PACKAGE_FILENAME = 'module.ini' |
Reimplemented from P4Cms_PackageAbstract.