Class `CmsPropertyHelper` has been deprecated

Cms Objects now support basic queries that return a collection
This commit is contained in:
Sam Georges 2014-05-23 19:35:56 +10:00
parent 206d7e43e4
commit d616d3ae8b
6 changed files with 168 additions and 19 deletions

View File

@ -1,3 +1,7 @@
* **Build 90** (2014-05-23)
- Class `CmsPropertyHelper` has been deprecated, will be removed year > 2014.
- Cms Objects now support basic queries that return a collection. Eg: `Page::sortBy('title')->lists('title', 'baseFileName')`
* **Build 89** (2014-05-22)
- Components have a new override method `onRender()` called before a component is rendered.
- The `{% component %}` tag now supports passing parameters that override the component properties when they are rendered.

View File

@ -5,7 +5,6 @@ use Lang;
use Cache;
use Config;
use Validator;
use Cms\Classes\FileHelper;
use System\Classes\SystemException;
use System\Classes\ApplicationException;
use October\Rain\Support\ValidationException;
@ -63,7 +62,7 @@ class CmsObject
* Creates an instance of the object and associates it with a CMS theme.
* @param \Cms\Classes\Theme $theme Specifies the theme the object belongs to.
*/
public function __construct(Theme $theme)
public function __construct(Theme $theme = null)
{
$this->theme = $theme;
}
@ -370,6 +369,9 @@ class CmsObject
*/
public static function listInTheme($theme, $skipCache = false)
{
if (!$theme)
throw new ApplicationException(Lang::get('cms::lang.theme.active.not_set'));
$dirPath = $theme->getPath().'/'.static::getObjectTypeDirName();
$result = [];
@ -433,6 +435,48 @@ class CmsObject
return false;
}
//
// Queries
//
/**
* Get a new query builder for the object
* @return CmsObjectQuery
*/
public function newQuery()
{
$query = new CmsObjectQuery($this, $this->theme);
return $query;
}
/**
* Handle dynamic method calls into the method.
* @param string $method
* @param array $parameters
* @return mixed
*/
public function __call($method, $parameters)
{
$query = $this->newQuery();
return call_user_func_array(array($query, $method), $parameters);
}
/**
* Handle dynamic static method calls into the method.
* @param string $method
* @param array $parameters
* @return mixed
*/
public static function __callStatic($method, $parameters)
{
$instance = new static;
return call_user_func_array([$instance, $method], $parameters);
}
//
// Overrides
//
/**
* Initializes the object properties from the cached data.
* @param array $cached The cached data array.

View File

@ -0,0 +1,14 @@
<?php namespace Cms\Classes;
use Illuminate\Support\Collection as CollectionBase;
use System\Classes\ApplicationException;
/**
* This class represents a collection of Cms Objects.
*
* @package october\cms
* @author Alexey Bobkov, Samuel Georges
*/
class CmsObjectCollection extends CollectionBase
{
}

View File

@ -0,0 +1,92 @@
<?php namespace Cms\Classes;
use System\Classes\ApplicationException;
/**
* This class provides helper methods to make the CmsObject behave like a Model
*
* @package october\cms
* @author Alexey Bobkov, Samuel Georges
*/
class CmsObjectQuery
{
protected $useCache = false;
protected $cmsObject;
protected $theme;
public function __construct($cmsObject, $theme)
{
$this->cmsObject = $cmsObject;
if ($theme)
$this->theme = $theme;
else
$this->inEditTheme();
}
/**
* Set the theme to a specific one.
* @return self
*/
public function inTheme($theme)
{
$this->theme = $theme;
return $this;
}
/**
* Set the theme to the editing one.
* @return self
*/
public function inEditTheme()
{
return $this->inTheme(Theme::getEditTheme());
}
/**
* Set the theme to the activated one.
* @return self
*/
public function inActiveTheme()
{
return $this->inTheme(Theme::getActiveTheme());
}
/**
* Enable or disable cache
* @param boolean $value
* @return self
*/
public function useCache($value = true)
{
$this->useCache = $value;
return $this;
}
/**
* Returns all CMS objects for the set theme
* @return CmsObjectCollection
*/
public function all()
{
$collection = forward_static_call([$this->cmsObject, 'listInTheme'], $this->theme, !$this->useCache);
$collection = new CmsObjectCollection($collection);
return $collection;
}
/**
* Handle dynamic method calls into the method.
* @param string $method
* @param array $parameters
* @return mixed
*/
public function __call($method, $parameters)
{
$collection = $this->all();
return call_user_func_array(array($collection, $method), $parameters);
}
}

View File

@ -1,12 +1,19 @@
<?php namespace Cms\Classes;
/**
*
* DEPRECATED WARNING: This class is deprecated and should be deleted
* if the current year is equal to or greater than 2015 @todo.
*
*/
use Flash;
use Cms\Classes\Page;
use Cms\Classes\Theme;
use System\Classes\ApplicationException;
/**
* This class provides helper methods for inspectable properties.
*
*
* @package october\cms
* @author Alexey Bobkov, Samuel Georges
*/
@ -18,19 +25,7 @@ class CmsPropertyHelper
*/
public static function listPages()
{
if (!($theme = Theme::getEditTheme()))
throw new ApplicationException(Lang::get('cms::lang.theme.edit.not_found'));
$pages = Page::listInTheme($theme, true);
$result = [];
foreach ($pages as $page) {
$fileName = $page->getBaseFileName();
$result[$fileName] = $fileName;
}
ksort($result);
return $result;
Flash::warning("CmsPropertyHelper::listPages() is deprecated, use Page::sortBy('baseFileName')->lists('baseFileName', 'baseFileName') instead.");
return Page::sortBy('baseFileName')->lists('baseFileName', 'baseFileName');
}
}

View File

@ -22,7 +22,7 @@ class Page extends CmsCompoundObject
* Creates an instance of the object and associates it with a CMS theme.
* @param \Cms\Classes\Theme $theme Specifies the theme the object belongs to.
*/
public function __construct(Theme $theme)
public function __construct(Theme $theme = null)
{
parent::__construct($theme);