Perforce Chronicle 2012.2/486814
API Documentation

P4Cms_Site_Config Class Reference

A specialized record class for use with site branch objects. More...

Inheritance diagram for P4Cms_Site_Config:
P4Cms_Model P4Cms_ModelInterface

List of all members.

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

Detailed Description

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.

Copyright:
2011-2012 Perforce Software. All rights reserved
License:
Please see LICENSE.txt in top-level folder of this distribution.
Version:
2012.2/486814

Constructor & Destructor Documentation

P4Cms_Site_Config::__construct ( P4Cms_Site site)

Create a new site config instance for the given site/branch.

Parameters:
P4Cms_Site$sitesite to represent config for
    {
        $this->_site = $site;
    }

Member Function Documentation

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.

Parameters:
string$idThe record id to retrieve/create
Returns:
P4Cms_Site_Config provides fluent interface.
    {
        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.

Parameters:
string$fieldthe name of the field to get the value of.
Returns:
mixed the value of the field.

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.

Parameters:
string$fieldthe name of the field to set the value of.
mixed$valuethe value to set in the field.
Returns:
P4Cms_Site_Config provides fluent interface.
Exceptions:
P4Cms_Model_Exceptionif 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.

Returns:
string the description for the associated site branch returns an empty string if none set.
    {
        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.

Returns:
array all fields for this model

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.

Returns:
P4Cms_Site the site branch this config model is for.
    {
        return $this->_site;
    }
P4Cms_Site_Config::getTheme ( )

Get the current theme for the associated site branch.

Returns:
string the name of the theme for the site branch returns the default theme if none has been set.
    {
        return $this->_getValue('theme') ?: P4Cms_Theme::DEFAULT_THEME;
    }
P4Cms_Site_Config::getTitle ( )

Get the friendly (configurable) title for the associated site branch.

Returns:
string the site branch's title (defaults to the site id).
    {
        return (string) $this->_getValue('title') ?: $this->getSite()->getId();
    }
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-).

Returns:
string a url for this site/branch
    {
        $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.

Returns:
array the urls the 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.

Parameters:
string$descriptionoptional - a description of the change.
Returns:
P4Cms_Site_Config provides a fluent interface
    {
        // 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.

Parameters:
string$descriptionthe description for the associated site branch
Returns:
P4Cms_Site_Config provides a fluent interface.
    {
        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.

Parameters:
string$themethe name of the theme to use.
Returns:
P4Cms_Site_Config provides a fluent interface.
Exceptions:
P4Cms_Site_Exceptionif 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.

Parameters:
string$titlethe title for the site branch.
Returns:
P4Cms_Site_Config provides a fluent interface.
    {
        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.

Parameters:
array | string | null$urlsthe urls for the site branch.
Returns:
P4Cms_Site_Config provides a fluent interface.
    {
        // 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);
    }

Member Data Documentation

P4Cms_Site_Config::$_dirty = array() [protected]
P4Cms_Site_Config::$_fields [static, protected]
Initial value:
 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'

The documentation for this class was generated from the following file: