* echo Bedrock::get_setting('PageTypes.MyPage.Fields.Headline.DefaultValue')
*
*
* Note that if the path resolves to an array, a {@link BedrockSetting} object
* will be returned, which can be traversed as an array with its {@see Iterator_Aggregate}
* implementation.
* Example:
*
* foreach(Bedrock::get_setting('PageTypes') as $pageName => $pageConfig) {
* echo "$pageName {$pageConfig->getDefaultSort()}";
* }
*
*
* @param string $setting The path to the setting (ex. "PageTypes.SomePage")
* @return string|BedrockSetting
*/
public static function get_setting($setting) {
$current = self::$configuration;
$last_key = null;
foreach(explode('.', $setting) as $key) {
$last_key = $key;
if(!isset($current[$key]))
return Bedrock::get_default($key);
if(is_array($current[$key]))
$current = $current[$key];
else
return is_array($current[$key]) ? BedrockSetting::create($key, $current[$key], $setting) : $current[$key];
}
return is_array($current) ? BedrockSetting::create($last_key, $current, $setting) : $current;
}
/**
* Fallback on the value contained in _defaults.yml
*
* @param string $setting The setting to pull out of defaults
* @return boolean|string
*/
public static function get_default($setting) {
if(isset(self::$defaults[$setting]))
return self::$defaults[$setting];
return false;
}
/**
* Using the stored {@link $component_paths}, get the configuration for a given component
*
* @param string $component The classname of the component to check
* @return BedrockSetting|boolean
*/
public static function get_component($component) {
if(isset(self::$component_paths[$component]))
return Bedrock::get_setting(self::$component_paths[$component]);
return false;
}
/**
* Get the configuration for a given page type
*
* @param string $component The classname of the page to check
* @return BedrockSetting|boolean
*/
public static function get_page_type($type) {
return self::get_setting("PageTypes.{$type}");
}
public static function register_pagetype($page) {
if(!in_array($page, self::$decoratable_pagetypes)) {
self::$decoratable_pagetypes[] = $page;
}
}
public static function register_component($comp) {
if(!in_array($comp, self::$decoratable_components)) {
self::$decoratable_components[] = $comp;
}
}
/**
* This is the main bootstrap function that loads all the YML and applies the decorators
* on every request. Don't forget to add Bedrock::load(); to your _config.php!
*/
public static function load() {
require_once "spyc/spyc.php";
self::$defaults = Spyc::YAMLLoad(dirname(dirname(__FILE__))."/_defaults.yml");
self::$configuration = Spyc::YAMLLoad(Director::baseFolder()."/".project()."/_project.yml");
if($pages = self::get_setting('PageTypes')) {
self::load_pages($pages);
}
if($components = Bedrock::get_setting("Components")) {
self::load_components($components);
}
if($configs = Bedrock::get_setting("Configuration")) {
foreach($configs as $class => $settings) {
if(class_exists($class) && singleton($class)->hasMethod('getProjectConfiguration')) {
call_user_func(array($class,'getProjectConfiguration'), $settings);
}
}
}
self::decorate();
}
public static function decorate() {
foreach(self::$decoratable_pagetypes as $page_type) {
// We don't want something to be a component and a SiteTree at the same time.
if(in_array($page_type, self::$decoratable_components)) {
$i = array_search($page_type, self::$decoratable_components);
unset(self::$decoratable_components[$i]);
}
if(!Object::has_extension(get_parent_class($page_type),'BedrockSiteTree')) {
Object::add_extension($page_type,'BedrockSiteTree');
singleton($page_type)->defineMethods();
Bedrock::message("Decorated $page_type with 'BedrockSiteTree'","good");
if(class_exists($page_type."_Controller")) {
Object::add_extension($page_type."_Controller", "BedrockPageController");
Bedrock::message("Decorated {$page_type}_Controller with BedrockPageController","good");
}
}
elseif(!Object::has_extension(get_parent_class($page_type),'BedrockPage')) {
Object::add_extension($page_type,'BedrockPage');
singleton($page_type)->defineMethods();
Bedrock::message("Decorated $page_type with 'BedrockPage'","good");
}
else {
Object::add_extension($page_type,'BedrockSubPage');
singleton($page_type)->defineMethods();
Bedrock::message("Decorated $page_type with 'BedrockSubPage'","good");
}
}
foreach(self::$decoratable_components as $component_type) {
Object::add_extension($component_type, 'BedrockDataObject');
singleton($component_type)->defineMethods();
Bedrock::message("Decorated $component_type with 'BedrockDataObject'","good");
}
}
/**
* Assign decorators to all of the page types configured. Also, check them for components
* and load those as well.
*
* @param BedrockSetting $pages The configuration of all pages
*/
public static function load_pages(BedrockSetting $pages) {
if($pages) {
foreach($pages as $page_type => $page) {
$page_type = BedrockUtil::proper_form($page_type);
if(class_exists($page_type)) {
self::register_pagetype($page_type);
}
else {
Bedrock::message("$page_type doesn't exist!","bad");
}
if($components = $page->getComponents()) {
self::load_components($components);
}
}
}
}
/**
* Assign decorators to all of the components configured.
*
* @param BedrockSetting $pages The configuration of all components
*/
public static function load_components(BedrockSetting $components) {
if($components) {
foreach($components as $component_type => $component) {
$component_type = BedrockUtil::proper_form($component_type);
if($component->getFields()) {
self::$component_paths[$component_type] = $component->getPath();
}
if(class_exists($component_type)) {
self::register_component($component_type);
}
else {
Bedrock::message("$component_type doesn't exist!","bad");
}
if($sub_components = $component->getComponents()) {
self::load_components($sub_components);
}
}
}
}
/**
* Recursively parses out an array of parent pages and their children and builds them in the CMS.
* Works with {@link BedrockGenerator} and passes back the pages that need to be duplicated
* after the tree is built out.
*
* @param array $arr The tree of pages
* @param int $parentID The parentID to be assigned to the page that is created
* @param int $level The level of heirarchy to which the page was added
* @return string
*/
public static function build_fixtures($arr, $parentID = 0, $level = 0) {
static $ret;
if(is_array($arr)) {
foreach($arr as $title => $children) {
if(isset($children['Type']) && class_exists($children['Type']) && is_subclass_of($children['Type'], "SiteTree")) {
$class = $children['Type'];
unset($children['Type']);
$tab = "";
for($i=0;$i<$level;$i++) $tab .= " ";
if(!$existing = DataObject::get_one($class, "ParentID = $parentID AND Title = '".Convert::raw2sql($title)."'")) {
$p = new $class();
$p->Title = $title;
$p->Content = Bedrock::get_default('DefaultContent');
$p->Status = "Published";
$p->ParentID = $parentID;
$p->write();
$p->publish("Stage", "Live");
if(isset($children['Count']) && is_numeric($children['Count'])) {
BedrockGenerator::$duplicate[] = $level."_".$p->ID."_".$children['Count'];
unset($children['Count']);
}
$ret .= "