Merge pull request #45 from open-sausages/pulls/4.0/namespace-everything

Upgrade code for namespaces
This commit is contained in:
Ingo Schommer 2016-09-08 17:57:19 +12:00 committed by GitHub
commit c6b6c5fb2a
12 changed files with 243 additions and 233 deletions

View File

@ -10,8 +10,9 @@ indent_style = space
insert_final_newline = true insert_final_newline = true
trim_trailing_whitespace = true trim_trailing_whitespace = true
[{*.yml,package.json}] [*.{yml,js,json,css,scss,eslintrc}]
indent_size = 2 indent_size = 2
indent_style = space
# The indent size used in the package.json file cannot be changed: # The indent size used in the package.json file cannot be changed:
# https://github.com/npm/npm/pull/3180#issuecomment-16336516 # https://github.com/npm/npm/pull/3180#issuecomment-16336516

6
.upgrade.yml Normal file
View File

@ -0,0 +1,6 @@
mappings:
SS_Report: SilverStripe\Reports\SS_Report
SS_ReportWrapper: SilverStripe\Reports\SS_ReportWrapper
ReportAdmin: SilverStripe\Reports\ReportAdmin
SideReportView: SilverStripe\Reports\SideReportView
SideReportWrapper: SilverStripe\Reports\SideReportWrapper

View File

@ -1,9 +1,28 @@
<?php <?php
namespace SilverStripe\Reports;
use SilverStripe\ORM\ArrayList; use SilverStripe\ORM\ArrayList;
use SilverStripe\ORM\SS_List; use SilverStripe\ORM\SS_List;
use SilverStripe\Security\Member; use SilverStripe\Security\Member;
use SilverStripe\Security\Permission; use SilverStripe\Security\Permission;
use SilverStripe\Control\Controller;
use SilverStripe\Core\ClassInfo;
use SilverStripe\Forms\FieldList;
use SilverStripe\Forms\LiteralField;
use SilverStripe\Forms\FormAction;
use SilverStripe\Forms\GridField\GridFieldConfig;
use SilverStripe\Forms\GridField\GridFieldButtonRow;
use SilverStripe\Forms\GridField\GridFieldPrintButton;
use SilverStripe\Forms\GridField\GridFieldExportButton;
use SilverStripe\Forms\GridField\GridFieldToolbarHeader;
use SilverStripe\Forms\GridField\GridFieldSortableHeader;
use SilverStripe\Forms\GridField\GridFieldDataColumns;
use SilverStripe\Forms\GridField\GridFieldPaginator;
use SilverStripe\Forms\GridField\GridField;
use SilverStripe\Core\Convert;
use SilverStripe\View\ViewableData;
use ReflectionClass;
/** /**
* Base "abstract" class creating reports on your data. * Base "abstract" class creating reports on your data.
@ -32,8 +51,6 @@ use SilverStripe\Security\Permission;
* *
* Right now, all subclasses of SS_Report will be shown in the ReportAdmin. In SS3 there is only * Right now, all subclasses of SS_Report will be shown in the ReportAdmin. In SS3 there is only
* one place where reports can go, so this class is greatly simplifed from its version in SS2. * one place where reports can go, so this class is greatly simplifed from its version in SS2.
*
* @package reports
*/ */
class SS_Report extends ViewableData class SS_Report extends ViewableData
{ {
@ -71,9 +88,9 @@ class SS_Report extends ViewableData
* @var array * @var array
*/ */
public static $excluded_reports = array( public static $excluded_reports = array(
'SS_Report', 'SilverStripe\\Reports\\SS_Report',
'SS_ReportWrapper', 'SilverStripe\\Reports\\SS_ReportWrapper',
'SideReportWrapper', 'SilverStripe\\Reports\\SideReportWrapper',
); );
/** /**
@ -325,7 +342,7 @@ class SS_Report extends ViewableData
new GridFieldPaginator() new GridFieldPaginator()
); );
$gridField = new GridField('Report', null, $items, $gridFieldConfig); $gridField = new GridField('Report', null, $items, $gridFieldConfig);
$columns = $gridField->getConfig()->getComponentByType('GridFieldDataColumns'); $columns = $gridField->getConfig()->getComponentByType('SilverStripe\\Forms\\GridField\\GridFieldDataColumns');
$displayFields = array(); $displayFields = array();
$fieldCasting = array(); $fieldCasting = array();
$fieldFormatting = array(); $fieldFormatting = array();
@ -426,121 +443,3 @@ class SS_Report extends ViewableData
return $this->title(); return $this->title();
} }
} }
/**
* SS_ReportWrapper is a base class for creating report wappers.
*
* Wrappers encapsulate an existing report to alter their behaviour - they are implementations of
* the standard GoF decorator pattern.
*
* This base class ensure that, by default, wrappers behave in the same way as the report that is
* being wrapped. You should override any methods that need to behave differently in your subclass
* of SS_ReportWrapper.
*
* It also makes calls to 2 empty methods that you can override {@link beforeQuery()} and
* {@link afterQuery()}
*
* @package reports
*/
abstract class SS_ReportWrapper extends SS_Report
{
protected $baseReport;
public function __construct($baseReport)
{
$this->baseReport = is_string($baseReport) ? new $baseReport : $baseReport;
$this->dataClass = $this->baseReport->dataClass();
parent::__construct();
}
public function ID()
{
return get_class($this->baseReport) . '_' . get_class($this);
}
///////////////////////////////////////////////////////////////////////////////////////////
// Filtering
public function parameterFields()
{
return $this->baseReport->parameterFields();
}
///////////////////////////////////////////////////////////////////////////////////////////
// Columns
public function columns()
{
return $this->baseReport->columns();
}
///////////////////////////////////////////////////////////////////////////////////////////
// Querying
/**
* Override this method to perform some actions prior to querying.
*/
public function beforeQuery($params)
{
}
/**
* Override this method to perform some actions after querying.
*/
public function afterQuery()
{
}
public function sourceQuery($params)
{
if ($this->baseReport->hasMethod('sourceRecords')) {
// The default implementation will create a fake query from our sourceRecords() method
return parent::sourceQuery($params);
} elseif ($this->baseReport->hasMethod('sourceQuery')) {
$this->beforeQuery($params);
$query = $this->baseReport->sourceQuery($params);
$this->afterQuery();
return $query;
} else {
user_error("Please override sourceQuery()/sourceRecords() and columns() in your base report", E_USER_ERROR);
}
}
public function sourceRecords($params = array(), $sort = null, $limit = null)
{
$this->beforeQuery($params);
$records = $this->baseReport->sourceRecords($params, $sort, $limit);
$this->afterQuery();
return $records;
}
///////////////////////////////////////////////////////////////////////////////////////////
// Pass-through
public function title()
{
return $this->baseReport->title();
}
public function group()
{
/** @skipUpgrade */
return $this->baseReport->hasMethod('group') ? $this->baseReport->group() : 'Group';
}
public function sort()
{
return $this->baseReport->hasMethod('sort') ? $this->baseReport->sort() : 0;
}
public function description()
{
return $this->baseReport->description();
}
public function canView($member = null)
{
return $this->baseReport->canView($member);
}
}

View File

@ -1,10 +1,25 @@
<?php <?php
namespace SilverStripe\Reports;
use SilverStripe\Admin\LeftAndMain;
use SilverStripe\Control\Controller;
use SilverStripe\Forms\FieldList;
use SilverStripe\Forms\Form;
use SilverStripe\Forms\GridField\GridFieldConfig;
use SilverStripe\Forms\GridField\GridFieldToolbarHeader;
use SilverStripe\Forms\GridField\GridFieldSortableHeader;
use SilverStripe\Forms\GridField\GridFieldDataColumns;
use SilverStripe\Forms\GridField\GridFieldFooter;
use SilverStripe\Forms\GridField\GridField;
use SilverStripe\Forms\HTMLEditor\HTMLEditorConfig;
use SilverStripe\i18n\i18n;
use SilverStripe\ORM\ArrayList; use SilverStripe\ORM\ArrayList;
use SilverStripe\ORM\SS_List; use SilverStripe\ORM\SS_List;
use SilverStripe\Security\Member; use SilverStripe\Security\Member;
use SilverStripe\Security\PermissionProvider; use SilverStripe\Security\PermissionProvider;
use SilverStripe\Admin\LeftAndMain; use SilverStripe\View\ArrayData;
use SilverStripe\View\Requirements;
/** /**
* Reports section of the CMS. * Reports section of the CMS.
@ -12,10 +27,6 @@ use SilverStripe\Admin\LeftAndMain;
* All reports that should show in the ReportAdmin section * All reports that should show in the ReportAdmin section
* of the CMS need to subclass {@link SS_Report}, and implement * of the CMS need to subclass {@link SS_Report}, and implement
* the appropriate methods and variables that are required. * the appropriate methods and variables that are required.
*
* @see SS_Report
*
* @package reports
*/ */
class ReportAdmin extends LeftAndMain implements PermissionProvider class ReportAdmin extends LeftAndMain implements PermissionProvider
{ {
@ -25,7 +36,7 @@ class ReportAdmin extends LeftAndMain implements PermissionProvider
private static $template_path = null; // defaults to (project)/templates/email private static $template_path = null; // defaults to (project)/templates/email
private static $tree_class = 'SS_Report'; private static $tree_class = 'SilverStripe\\Reports\\SS_Report';
private static $url_handlers = array( private static $url_handlers = array(
'show/$ReportClass/$Action' => 'handleAction' 'show/$ReportClass/$Action' => 'handleAction'
@ -160,7 +171,7 @@ class ReportAdmin extends LeftAndMain implements PermissionProvider
// The root element should explicitly point to the root node. // The root element should explicitly point to the root node.
// Uses session state for current record otherwise. // Uses session state for current record otherwise.
$items[0]->Link = singleton('ReportAdmin')->Link(); $items[0]->Link = singleton('SilverStripe\\Reports\\ReportAdmin')->Link();
if ($this->reportObject) { if ($this->reportObject) {
//build breadcrumb trail to the current report //build breadcrumb trail to the current report
@ -217,7 +228,7 @@ class ReportAdmin extends LeftAndMain implements PermissionProvider
); );
$gridField = new GridField('Reports', false, $this->Reports(), $gridFieldConfig); $gridField = new GridField('Reports', false, $this->Reports(), $gridFieldConfig);
/** @var GridFieldDataColumns $columns */ /** @var GridFieldDataColumns $columns */
$columns = $gridField->getConfig()->getComponentByType('GridFieldDataColumns'); $columns = $gridField->getConfig()->getComponentByType('SilverStripe\\Forms\\GridField\\GridFieldDataColumns');
$columns->setDisplayFields(array( $columns->setDisplayFields(array(
'title' => _t('ReportAdmin.ReportTitle', 'Title'), 'title' => _t('ReportAdmin.ReportTitle', 'Title'),
)); ));

119
code/SS_ReportWrapper.php Normal file
View File

@ -0,0 +1,119 @@
<?php
namespace SilverStripe\Reports;
/**
* SS_ReportWrapper is a base class for creating report wappers.
*
* Wrappers encapsulate an existing report to alter their behaviour - they are implementations of
* the standard GoF decorator pattern.
*
* This base class ensure that, by default, wrappers behave in the same way as the report that is
* being wrapped. You should override any methods that need to behave differently in your subclass
* of SS_ReportWrapper.
*
* It also makes calls to 2 empty methods that you can override {@link beforeQuery()} and
* {@link afterQuery()}
*/
abstract class SS_ReportWrapper extends SS_Report
{
protected $baseReport;
public function __construct($baseReport)
{
$this->baseReport = is_string($baseReport) ? new $baseReport : $baseReport;
$this->dataClass = $this->baseReport->dataClass();
parent::__construct();
}
public function ID()
{
return get_class($this->baseReport) . '_' . get_class($this);
}
///////////////////////////////////////////////////////////////////////////////////////////
// Filtering
public function parameterFields()
{
return $this->baseReport->parameterFields();
}
///////////////////////////////////////////////////////////////////////////////////////////
// Columns
public function columns()
{
return $this->baseReport->columns();
}
///////////////////////////////////////////////////////////////////////////////////////////
// Querying
/**
* Override this method to perform some actions prior to querying.
*/
public function beforeQuery($params)
{
}
/**
* Override this method to perform some actions after querying.
*/
public function afterQuery()
{
}
public function sourceQuery($params)
{
if ($this->baseReport->hasMethod('sourceRecords')) {
// The default implementation will create a fake query from our sourceRecords() method
return parent::sourceQuery($params);
} elseif ($this->baseReport->hasMethod('sourceQuery')) {
$this->beforeQuery($params);
$query = $this->baseReport->sourceQuery($params);
$this->afterQuery();
return $query;
} else {
user_error("Please override sourceQuery()/sourceRecords() and columns() in your base report", E_USER_ERROR);
}
}
public function sourceRecords($params = array(), $sort = null, $limit = null)
{
$this->beforeQuery($params);
$records = $this->baseReport->sourceRecords($params, $sort, $limit);
$this->afterQuery();
return $records;
}
///////////////////////////////////////////////////////////////////////////////////////////
// Pass-through
public function title()
{
return $this->baseReport->title();
}
public function group()
{
/** @skipUpgrade */
return $this->baseReport->hasMethod('group') ? $this->baseReport->group() : 'Group';
}
public function sort()
{
return $this->baseReport->hasMethod('sort') ? $this->baseReport->sort() : 0;
}
public function description()
{
return $this->baseReport->description();
}
public function canView($member = null)
{
return $this->baseReport->canView($member);
}
}

View File

@ -1,9 +1,13 @@
<?php <?php
namespace SilverStripe\Reports;
use TableListField;
use SilverStripe\Core\Convert;
use SilverStripe\View\ViewableData;
/** /**
* Renderer for showing SideReports in CMSMain * Renderer for showing SideReports in CMSMain
*
* @package reports
*/ */
class SideReportView extends ViewableData class SideReportView extends ViewableData
{ {
@ -106,22 +110,3 @@ class SideReportView extends ViewableData
} }
} }
} }
/**
* A report wrapper that makes it easier to define slightly different behaviour for side-reports.
*
* This report wrapper will use sideReportColumns() for the report columns, instead of columns().
*
* @package reports
*/
class SideReportWrapper extends SS_ReportWrapper
{
public function columns()
{
if ($this->baseReport->hasMethod('sideReportColumns')) {
return $this->baseReport->sideReportColumns();
} else {
return parent::columns();
}
}
}

View File

@ -0,0 +1,20 @@
<?php
namespace SilverStripe\Reports;
/**
* A report wrapper that makes it easier to define slightly different behaviour for side-reports.
*
* This report wrapper will use sideReportColumns() for the report columns, instead of columns().
*/
class SideReportWrapper extends SS_ReportWrapper
{
public function columns()
{
if ($this->baseReport->hasMethod('sideReportColumns')) {
return $this->baseReport->sideReportColumns();
} else {
return parent::columns();
}
}
}

View File

@ -23,6 +23,11 @@
"dev-master": "4.0.x-dev" "dev-master": "4.0.x-dev"
} }
}, },
"autoload": {
"psr-4": {
"SilverStripe\\Reports\\": "code/"
}
},
"require-dev": { "require-dev": {
"phpunit/PHPUnit": "~4.8" "phpunit/PHPUnit": "~4.8"
} }

View File

@ -1,41 +0,0 @@
<form $FormAttributes>
<p style="display: none;" class="message " id="Form_EditForm_error"/>
<div id="ScrollPanel">
<fieldset>
$FieldMap.ReportTitle.FieldHolder
$FieldMap.ReportDescription.FieldHolder
<% if $FieldMap.Filters.Children %>
<h4><% _t('ReportAdminForm.FILTERBY', 'Filter by') %></h4>
<div class="filters">
<% loop $FieldMap.Filters %>
<% loop $Children %>
$FieldHolder
<% end_loop %>
<% end_loop %>
</div>
<div id="action_updatereport">
<% if $FieldMap.action_updatereport %>
$FieldMap.action_updatereport.Field
<% end_if %>
</div>
<div style="clear: both">&nbsp;</div>
<% end_if %>
$FieldMap.ReportContent.FieldHolder
<% loop $HiddenFields %>$Field<% end_loop %>
</fieldset>
</div>
<div class="clear"><!-- --></div>
</form>

View File

@ -1,6 +1,11 @@
<?php <?php
use SilverStripe\ORM\ArrayList; use SilverStripe\ORM\ArrayList;
use SilverStripe\Reports\SS_Report;
use SilverStripe\Control\Session;
use SilverStripe\Dev\SapphireTest;
use SilverStripe\Dev\TestOnly;
/** /**
* @package reports * @package reports