Perforce Chronicle 2012.2/486814
API Documentation
|
A specialized record class for use with site branch objects. More...
Public Member Functions | |
__construct (P4Cms_Site $site) | |
Create a new site config instance for the given site/branch. | |
getDescription () | |
Get the description for the associated site branch. | |
getFields () | |
This record utilizes a general, theme and urls record to store the various data. | |
getSite () | |
Get the site branch this configuration is for. | |
getTheme () | |
Get the current theme for the associated site branch. | |
getTitle () | |
Get the friendly (configurable) title for the associated site branch. | |
getUrl () | |
Get a single url for this site/branch. | |
getUrls () | |
Get the urls the associated site branch is configured to respond to. | |
save ($description=null) | |
Our model wraps several records which seperately store the urls, theme and general configuration details. | |
setDescription ($description=null) | |
Set the description for the associated site branch. | |
setTheme ($theme) | |
Set the theme for the associated site branch. | |
setTitle ($title) | |
Set the friendly (configurable) title for the associated site branch. | |
setUrls ($urls) | |
Set the urls for the associated site branch as an array of strings, or null. | |
Public Attributes | |
const | ID_GENERAL = 'config/general' |
const | ID_THEME = 'config/theme' |
const | ID_URLS = 'config/urls' |
Protected Member Functions | |
_getRecord ($id) | |
This method will return the specified record using the model's associated storage adapter. | |
_getValue ($field) | |
The normal _getValue behaviour is to return values present on this model. | |
_setValue ($field, $value) | |
The normal _setValue behaviour is to update values present on this model. | |
Protected Attributes | |
$_dirty = array() | |
$_records = array() | |
$_site = null | |
Static Protected Attributes | |
static | $_fields |
A specialized record class for use with site branch objects.
Site/Branch config consists of a few rather discrete items including Urls, Active Theme Selection and other General config. We break this data into three seperate records to allow our branching operations to deal with them granularly.
The Site Config model adds support for a 'record' property when declaring field definitions so items can choose which location thier data should be stored in. By default fields fall back to the 'ID_GENERAL' record for storage/retrieval.
P4Cms_Site_Config::__construct | ( | P4Cms_Site $ | site | ) |
Create a new site config instance for the given site/branch.
P4Cms_Site | $site | site to represent config for |
{ $this->_site = $site; }
P4Cms_Site_Config::_getRecord | ( | $ | id | ) | [protected] |
This method will return the specified record using the model's associated storage adapter.
If the requested record does not already exist an in memory version will be silently created.
The returned records are cached in the protected '_records' array for later re-use.
string | $id | The record id to retrieve/create |
{ if (isset($this->_records[$id])) { return $this->_records[$id]; } $adapter = $this->getSite()->getStorageAdapter(); try { $record = P4Cms_Record::fetch($id, null, $adapter); } catch (P4Cms_Record_NotFoundException $e) { $record = new P4Cms_Record(null, $adapter); $record->setId($id); } $this->_records[$id] = $record; return $record; }
P4Cms_Site_Config::_getValue | ( | $ | field | ) | [protected] |
The normal _getValue behaviour is to return values present on this model.
We have replaced our parent to route the request to one of our underlying records based on the 'record' property of the field. If the field is unknown or has no record property we default to the general record.
string | $field | the name of the field to get the value of. |
Reimplemented from P4Cms_Model.
{ $id = $this->_getFieldProperty($field, 'record') ?: static::ID_GENERAL; return $this->_getRecord($id)->getValue($field); }
P4Cms_Site_Config::_setValue | ( | $ | field, |
$ | value | ||
) | [protected] |
The normal _setValue behaviour is to update values present on this model.
We have replaced our parent to route the request to one of our underlying records based on the 'record' property of the field. If the field is unknown or has no record property we default to the general record.
We also flag the associated record as 'dirty' so any later save operations know to include it.
string | $field | the name of the field to set the value of. |
mixed | $value | the value to set in the field. |
P4Cms_Model_Exception | if the field does not exist. |
Reimplemented from P4Cms_Model.
{ $id = $this->_getFieldProperty($field, 'record') ?: static::ID_GENERAL; $this->_getRecord($id)->setValue($field, $value); // flag record as dirty so we know to save it later. $this->_dirty[$id] = true; return $this; }
P4Cms_Site_Config::getDescription | ( | ) |
Get the description for the associated site branch.
{ return (string) $this->_getValue('description'); }
P4Cms_Site_Config::getFields | ( | ) |
This record utilizes a general, theme and urls record to store the various data.
The parent getFields would have returned the defined fields for all three; we have extended it to also include any custom fields present on the general config.
This model always routes unknown fields to the general config. As such, we don't include any custom fields from the theme or url records as they would not be accessible using our models accessors/mutators.
Reimplemented from P4Cms_Model.
{ $fields = array_merge( parent::getFields(), $this->_getRecord(static::ID_GENERAL)->getFields() ); // return field names. return array_unique($fields); }
P4Cms_Site_Config::getSite | ( | ) |
Get the site branch this configuration is for.
{
return $this->_site;
}
P4Cms_Site_Config::getTheme | ( | ) |
Get the current theme for the associated site branch.
{ return $this->_getValue('theme') ?: P4Cms_Theme::DEFAULT_THEME; }
P4Cms_Site_Config::getTitle | ( | ) |
P4Cms_Site_Config::getUrl | ( | ) |
Get a single url for this site/branch.
If one or more explicit urls have been set for this site/branch (e.g. http://stage.example.com), the first one will be used. If no explicit urls have been configured, we search up through the parent branches until we find one and append the name of the branch as the first path component (e.g. http://example.com/-stage-).
{ $urls = $this->getUrls(); // if we don't have any urls, we need to grab urls from the // closest parent that does and append a branch specifier. if (!$urls) { $parent = $this->getSite(); while (!$urls && ($parent = $parent->getParent())) { $urls = $parent->getConfig()->getUrls(); } } // normalize url to begin with a protocol/scheme. $url = array_shift($urls); if ($url && substr($url, 0, 7) != 'http://' && substr($url, 0, 8) != 'https://' ) { $url = 'http://' . $url; } // if we got a url from a parent, we need to append // a branch specifier to route to this specific branch if ($url && isset($parent)) { $url = trim($url, "/") . "/-" . $this->getSite()->getBranchBasename() . "-"; } return rtrim($url, "/"); }
P4Cms_Site_Config::getUrls | ( | ) |
Get the urls the associated site branch is configured to respond to.
{ $urls = $this->_getValue('urls'); return is_array($urls) ? $urls : array(); }
P4Cms_Site_Config::save | ( | $ | description = null | ) |
Our model wraps several records which seperately store the urls, theme and general configuration details.
When save is called we start a batch (if one is not already in progress) and save any of the files which have been modified.
string | $description | optional - a description of the change. |
{ // ensure we have a save description. $description = $description ?: "Saved configuration for '" . $this->getTitle() . "' site."; // start the batch $adapter = $this->getSite()->getStorageAdapter(); $batch = !$adapter->inBatch() ? $adapter->beginBatch($description) : false; // try to save each of the 'dirty' records. // note: we reset the adapter in case the record came // from cache or otherwise has a bogus adapter. try { foreach (array_keys($this->_dirty) as $id) { $this->_records[$id]->setAdapter($adapter) ->save(); } $this->_dirty = array(); } catch (Exception $e) { if ($batch) { $adapter->revertBatch(); } throw $e; } // commit the batch. if ($batch) { $adapter->commitBatch(); } return $this; }
P4Cms_Site_Config::setDescription | ( | $ | description = null | ) |
Set the description for the associated site branch.
string | $description | the description for the associated site branch |
{ if (!is_null($description) && !is_string($description)) { throw new InvalidArgumentException("The provided description is not a string."); } return $this->_setValue('description', $description); }
P4Cms_Site_Config::setTheme | ( | $ | theme | ) |
Set the theme for the associated site branch.
string | $theme | the name of the theme to use. |
P4Cms_Site_Exception | if theme does not exist |
{ if ($theme && !P4Cms_Theme::exists($theme)) { throw new P4Cms_Site_Exception("Theme $theme is invalid or does not exist."); } return $this->_setValue('theme', $theme); }
P4Cms_Site_Config::setTitle | ( | $ | title | ) |
Set the friendly (configurable) title for the associated site branch.
string | $title | the title for the site branch. |
{ if (!is_string($title)) { throw new InvalidArgumentException("The provided title is not a string."); } return $this->_setValue('title', $title); }
P4Cms_Site_Config::setUrls | ( | $ | urls | ) |
Set the urls for the associated site branch as an array of strings, or null.
array | string | null | $urls | the urls for the site branch. |
{ // if provided with a string, convert to an array if (is_string($urls)) { $urls = array_filter(array_map('trim', preg_split("/\n|,/", $urls))); } return $this->_setValue('urls', $urls); }
P4Cms_Site_Config::$_dirty = array() [protected] |
P4Cms_Site_Config::$_fields [static, protected] |
array( 'description' => array( 'accessor' => 'getDescription', 'mutator' => 'setDescription' ), 'title' => array( 'accessor' => 'getTitle', 'mutator' => 'setTitle' ), 'theme' => array( 'accessor' => 'getTheme', 'mutator' => 'setTheme', 'record' => self::ID_THEME ), 'urls' => array( 'accessor' => 'getUrls', 'mutator' => 'setUrls', 'record' => self::ID_URLS ) )
Reimplemented from P4Cms_Model.
P4Cms_Site_Config::$_records = array() [protected] |
P4Cms_Site_Config::$_site = null [protected] |
const P4Cms_Site_Config::ID_GENERAL = 'config/general' |
const P4Cms_Site_Config::ID_THEME = 'config/theme' |
const P4Cms_Site_Config::ID_URLS = 'config/urls' |