Merge pull request #51 from helpfulrobot/convert-to-psr-2

Converted to PSR-2
This commit is contained in:
Damian Mooyman 2015-12-21 10:30:04 +13:00
commit 1a37a8033a
7 changed files with 1262 additions and 1186 deletions

View File

@ -14,7 +14,8 @@
*
* @package staticpublisher
*/
class StaticExporter extends Controller {
class StaticExporter extends Controller
{
/**
* @config
@ -42,7 +43,8 @@ class StaticExporter extends Controller {
/**
*
*/
public function __construct() {
public function __construct()
{
parent::__construct();
if (class_exists('SiteTree')) {
@ -64,7 +66,8 @@ class StaticExporter extends Controller {
/**
*
*/
public function init() {
public function init()
{
parent::init();
$canAccess = (Director::isDev() || Director::is_cli());
@ -79,7 +82,8 @@ class StaticExporter extends Controller {
*
* @return string
*/
public function Link($action = null) {
public function Link($action = null)
{
return "dev/staticexporter/$action";
}
@ -88,14 +92,16 @@ class StaticExporter extends Controller {
*
* @return string
*/
public function AbsoluteLink($action = null) {
public function AbsoluteLink($action = null)
{
return Director::absoluteURL($this->Link($action));
}
/**
* @return array
*/
public function index() {
public function index()
{
return array(
'Title' => _t('StaticExporter.NAME', 'Static exporter'),
'Form' => $this->StaticExportForm()->forTemplate()
@ -105,7 +111,8 @@ class StaticExporter extends Controller {
/**
* @return Form
*/
public function StaticExportForm() {
public function StaticExportForm()
{
$form = new Form($this, 'StaticExportForm', new FieldList(
new TextField('baseurl', _t('StaticExporter.BASEURL', 'Base URL'))
), new FieldList(
@ -116,15 +123,17 @@ class StaticExporter extends Controller {
}
public function export() {
public function export()
{
if (isset($_REQUEST['baseurl'])) {
$base = $_REQUEST['baseurl'];
if(substr($base,-1) != '/') $base .= '/';
if (substr($base, -1) != '/') {
$base .= '/';
}
Config::inst()->update('Director', 'alternate_base_url', $base);
}
else {
} else {
$base = Director::baseURL();
}
@ -159,7 +168,8 @@ class StaticExporter extends Controller {
*
* @return string path to export
*/
public function doExport($base, $folder, $symlink = true, $quiet = true) {
public function doExport($base, $folder, $symlink = true, $quiet = true)
{
ini_set('max_execution_time', 0);
Config::inst()->update('Director', 'alternate_base_url', $base);
@ -176,8 +186,7 @@ class StaticExporter extends Controller {
if ($symlink) {
`cd $folder; ln -s $f1; ln -s $f2`;
}
else {
} else {
`cp -R $f1 $folder; cp -R $f2 $folder`;
}
@ -230,7 +239,8 @@ class StaticExporter extends Controller {
*
* @return array
*/
public function getExportUrls() {
public function getExportUrls()
{
$classes = $this->config()->get('export_objects');
$urls = array();

View File

@ -3,7 +3,8 @@
/**
* @package staticpublisher
*/
class FilesystemPublisher extends StaticPublisher {
class FilesystemPublisher extends StaticPublisher
{
/**
* @var string
@ -38,7 +39,8 @@ class FilesystemPublisher extends StaticPublisher {
*
* @deprecated 3.2 Use the "FilesystemPublisher.static_base_url" config setting instead
*/
static public function set_static_base_url($url) {
public static function set_static_base_url($url)
{
Deprecation::notice('3.2', 'Use the "FilesystemPublisher.static_base_url" config setting instead');
Config::inst()->update('FilesystemPublisher', 'static_base_url', $url);
@ -52,7 +54,8 @@ class FilesystemPublisher extends StaticPublisher {
* with the filename 'index.html'. If you set the extension to PHP, then a simple PHP script will
* be generated that can do appropriate cache & redirect header negotation.
*/
public function __construct($destFolder = 'cache', $fileExtension = null) {
public function __construct($destFolder = 'cache', $fileExtension = null)
{
// Remove trailing slash from folder
if (substr($destFolder, -1) == '/') {
$destFolder = substr($destFolder, 0, -1);
@ -91,7 +94,8 @@ class FilesystemPublisher extends StaticPublisher {
* @param array $urls Absolute or relative URLs
* @return array Map of original URLs to filesystem paths (relative to {@link $destFolder}).
*/
public function urlsToPaths($urls) {
public function urlsToPaths($urls)
{
$mappedUrls = array();
foreach ($urls as $url) {
@ -115,7 +119,9 @@ class FilesystemPublisher extends StaticPublisher {
$filename = $urlSegment ? "$urlSegment.$this->fileExtension" : "index.$this->fileExtension";
if (Config::inst()->get('FilesystemPublisher', 'domain_based_caching')) {
if (!$urlParts) continue; // seriously malformed url here...
if (!$urlParts) {
continue;
} // seriously malformed url here...
$filename = $urlParts['host'] . '/' . $filename;
}
@ -128,10 +134,13 @@ class FilesystemPublisher extends StaticPublisher {
/**
* @param array $urls
*/
public function unpublishPages($urls) {
public function unpublishPages($urls)
{
// Do we need to map these?
// Detect a numerically indexed arrays
if (is_numeric(join('', array_keys($urls)))) $urls = $this->urlsToPaths($urls);
if (is_numeric(join('', array_keys($urls)))) {
$urls = $this->urlsToPaths($urls);
}
// This can be quite memory hungry and time-consuming
// @todo - Make a more memory efficient publisher
@ -157,7 +166,8 @@ class FilesystemPublisher extends StaticPublisher {
* - "redirect": A redirect location (if applicable)
* - "path": The filesystem path where the cache has been written
*/
public function publishPages($urls) {
public function publishPages($urls)
{
// Save current stage and temporarily force it to Live
$oldStage = Versioned::current_stage();
Versioned::reading_stage("Live");
@ -169,7 +179,9 @@ class FilesystemPublisher extends StaticPublisher {
// Do we need to map these?
// Detect a numerically indexed arrays
if (is_numeric(join('', array_keys($urls)))) $urls = $this->urlsToPaths($urls);
if (is_numeric(join('', array_keys($urls)))) {
$urls = $this->urlsToPaths($urls);
}
// This can be quite memory hungry and time-consuming
// @todo - Make a more memory efficient publisher
@ -228,11 +240,17 @@ class FilesystemPublisher extends StaticPublisher {
Requirements::clear();
if($url == "") $url = "/";
if(Director::is_relative_url($url)) $url = Director::absoluteURL($url);
if ($url == "") {
$url = "/";
}
if (Director::is_relative_url($url)) {
$url = Director::absoluteURL($url);
}
$response = Director::test(str_replace('+', ' ', $url));
if (!$response) continue;
if (!$response) {
continue;
}
if ($response) {
$result[$origUrl]['statuscode'] = $response->getStatusCode();
@ -246,11 +264,15 @@ class FilesystemPublisher extends StaticPublisher {
$pageObject = null;
if ($response && is_object($response) && ((int)$response->getStatusCode())>=400) {
$pageObject = SiteTree::get_by_link($url);
if ($pageObject && $pageObject instanceof ErrorPage) $isErrorPage = true;
if ($pageObject && $pageObject instanceof ErrorPage) {
$isErrorPage = true;
}
}
// Skip any responses with a 404 status code unless it's the ErrorPage itself.
if (!$isErrorPage && is_object($response) && $response->getStatusCode()=='404') continue;
if (!$isErrorPage && is_object($response) && $response->getStatusCode()=='404') {
continue;
}
// Generate file content
// PHP file caching will generate a simple script from a template
@ -289,13 +311,11 @@ class FilesystemPublisher extends StaticPublisher {
}
if (!$isErrorPage) {
$files[$origUrl] = array(
'Content' => $content,
'Folder' => dirname($path).'/',
'Filename' => basename($path),
);
} else {
// Generate a static version of the error page with a standardised name, so they can be plugged
@ -306,7 +326,6 @@ class FilesystemPublisher extends StaticPublisher {
'Folder' => dirname($path).'/',
'Filename' => "error-$code.html",
);
}
}
@ -347,7 +366,8 @@ class FilesystemPublisher extends StaticPublisher {
*
* @return string
*/
protected function generatePHPCacheFile($content, $age, $lastModified, $contentType) {
protected function generatePHPCacheFile($content, $age, $lastModified, $contentType)
{
$template = file_get_contents(STATIC_MODULE_DIR . '/code/CachedPHPPage.tmpl');
return str_replace(
array('**MAX_AGE**', '**LAST_MODIFIED**', '**CONTENT**', '**CONTENT_TYPE**'),
@ -364,7 +384,8 @@ class FilesystemPublisher extends StaticPublisher {
*
* @return string
*/
protected function generatePHPCacheRedirection($destination) {
protected function generatePHPCacheRedirection($destination)
{
$template = file_get_contents(STATIC_MODULE_DIR . '/code/CachedPHPRedirection.tmpl');
return str_replace(
@ -377,7 +398,8 @@ class FilesystemPublisher extends StaticPublisher {
/**
* @return string
*/
public function getDestDir() {
public function getDestDir()
{
return BASE_PATH . '/' . $this->destFolder;
}
@ -388,7 +410,8 @@ class FilesystemPublisher extends StaticPublisher {
*
* @return array
*/
public function getExistingStaticCacheFiles() {
public function getExistingStaticCacheFiles()
{
$cacheDir = BASE_PATH . '/' . $this->destFolder;
$urlMapper = array_flip($this->urlsToPaths($this->owner->allPagesToCache()));

View File

@ -8,7 +8,8 @@
*
* @package staticpublisher
*/
class RsyncMultiHostPublisher extends FilesystemPublisher {
class RsyncMultiHostPublisher extends FilesystemPublisher
{
/**
* @config
@ -38,7 +39,8 @@ class RsyncMultiHostPublisher extends FilesystemPublisher {
*
* @param $targets An array of targets to publish to.
*/
public static function set_targets($targets) {
public static function set_targets($targets)
{
Deprecation::notice('3.2', 'Use the "RsyncMultiHostPublisher.targets" config setting instead');
Config::inst()->update('RsyncMultiHostPublisher', 'targets', $targets);
@ -50,25 +52,31 @@ class RsyncMultiHostPublisher extends FilesystemPublisher {
*
* @deprecated 3.2 Use the "RsyncMultiHostPublisher.excluded_folders" config setting instead
*/
public static function set_excluded_folders($folders) {
public static function set_excluded_folders($folders)
{
Deprecation::notice('3.2', 'Use the "RsyncMultiHostPublisher.excluded_folders" config setting instead');
Config::inst()->update('RsyncMultiHostPublisher', 'excluded_folders', $folders);
}
public function publishPages($urls) {
public function publishPages($urls)
{
parent::publishPages($urls);
$base = Director::baseFolder();
$framework = FRAMEWORK_DIR;
// Get variable that can turn off the rsync component of publication
if(isset($_GET['norsync']) && $_GET['norsync']) return;
if (isset($_GET['norsync']) && $_GET['norsync']) {
return;
}
$extraArg = "";
$excludedFolders = Config::inst()->get('RsyncMultiHostPublisher', 'excluded_folders');
if($excludedFolders) foreach($excludedFolders as $folder) {
if ($excludedFolders) {
foreach ($excludedFolders as $folder) {
$extraArg .= " --exclude " . escapeshellarg($folder);
}
}
$targets = Config::inst()->get('RsyncMultiHostPublisher', 'targets');
foreach ((array)$targets as $target) {

View File

@ -2,7 +2,8 @@
/**
* @package staticpublisher
*/
abstract class StaticPublisher extends DataExtension {
abstract class StaticPublisher extends DataExtension
{
/**
* Defines whether to output information about publishing or not. By
@ -49,18 +50,19 @@ abstract class StaticPublisher extends DataExtension {
/**
* @param array
*/
abstract function publishPages($pages);
abstract public function publishPages($pages);
/**
* @param array
*/
abstract function unpublishPages($pages);
abstract public function unpublishPages($pages);
/**
* @deprecated
* @param string
*/
public static function set_static_publisher_theme($theme) {
public static function set_static_publisher_theme($theme)
{
Deprecation::notice('1.0', 'Use the new config system. SSViewer.static_publisher_theme');
Config::inst()->update('StaticPublisher', 'static_publisher_theme', $theme);
@ -71,7 +73,8 @@ abstract class StaticPublisher extends DataExtension {
*
* @return string
*/
public static function static_publisher_theme() {
public static function static_publisher_theme()
{
Deprecation::notice('1.0', 'Use the new config system. SSViewer.static_publisher_theme');
return Config::inst()->get('StaticPublisher', 'static_publisher_theme');
@ -82,7 +85,8 @@ abstract class StaticPublisher extends DataExtension {
*
* @return boolean
*/
public static function echo_progress() {
public static function echo_progress()
{
Deprecation::notice('1.0', 'Use the new config system. SSViewer.static_publisher_theme');
return Config::inst()->get('StaticPublisher', 'echo_progress');
@ -92,7 +96,8 @@ abstract class StaticPublisher extends DataExtension {
* @deprecated
*
*/
public static function set_echo_progress($progress) {
public static function set_echo_progress($progress)
{
Deprecation::notice('1.0', 'Use the new config system. SSViewer.static_publisher_theme');
Config::inst()->get('StaticPublisher', 'echo_progress', $progress);
@ -103,7 +108,8 @@ abstract class StaticPublisher extends DataExtension {
*
* @param SiteTree
*/
public function onAfterPublish($original) {
public function onAfterPublish($original)
{
$this->republish($original);
}
@ -113,11 +119,13 @@ abstract class StaticPublisher extends DataExtension {
*
* Only called if the published content exists and has been modified.
*/
public function onRenameLinkedAsset($original) {
public function onRenameLinkedAsset($original)
{
$this->republish($original);
}
public function republish($original) {
public function republish($original)
{
if (Config::inst()->get('StaticPublisher', 'disable_realtime')) {
return;
}
@ -143,7 +151,9 @@ abstract class StaticPublisher extends DataExtension {
}
// Remove leading slashes from all URLs (apart from the homepage)
if(substr($url,-1) == '/' && $url != '/') $url = substr($url,0,-1);
if (substr($url, -1) == '/' && $url != '/') {
$url = substr($url, 0, -1);
}
$urls[$i] = $url;
}
@ -159,7 +169,8 @@ abstract class StaticPublisher extends DataExtension {
/**
* Get changes and hook into underlying functionality.
*/
public function onAfterUnpublish($page) {
public function onAfterUnpublish($page)
{
if (Config::inst()->get('StaticPublisher', 'disable_realtime')) {
return;
}
@ -186,7 +197,8 @@ abstract class StaticPublisher extends DataExtension {
* @param string $url
* @return array
*/
public function getMetadata($url) {
public function getMetadata($url)
{
return array(
'Cache generated on ' . date('Y-m-d H:i:s T (O)')
);

View File

@ -2,11 +2,13 @@
/**
* @package staticpublisher
*/
class RebuildStaticCacheTask extends BuildTask {
class RebuildStaticCacheTask extends BuildTask
{
private static $quiet = false;
public function run($request) {
public function run($request)
{
if (Config::inst()->get('RebuildStaticCacheTask', 'quiet')) {
Config::inst()->update('StaticPublisher', 'echo_progress', false);
} else {
@ -32,7 +34,8 @@ class RebuildStaticCacheTask extends BuildTask {
$this->rebuildCache($urls, true);
}
public function log($message = null) {
public function log($message = null)
{
$quiet = Config::inst()->get('RebuildStaticCacheTask', 'quiet');
if ($quiet) {
@ -48,8 +51,8 @@ class RebuildStaticCacheTask extends BuildTask {
* @param array $urls The URLs of pages to re-fetch and cache.
* @param bool $removeAll Remove all stale cache files (default TRUE).
*/
public function rebuildCache($urls, $removeAll = true) {
public function rebuildCache($urls, $removeAll = true)
{
if (!is_array($urls)) {
// $urls must be an array
user_error("Rebuild cache must be passed an array of urls. Make sure your allPagesToCache function returns an array", E_USER_ERROR);

View File

@ -3,9 +3,11 @@
/**
* @package staticpublisher
*/
class StaticExporterTask extends BuildTask {
class StaticExporterTask extends BuildTask
{
public function run($request) {
public function run($request)
{
$now = microtime(true);
$export = new StaticExporter();
@ -14,13 +16,17 @@ class StaticExporterTask extends BuildTask {
$quiet = $request->getVar('quiet');
$folder = $request->getVar('path');
if(!$folder) $folder = TEMP_FOLDER . '/static-export';
if (!$folder) {
$folder = TEMP_FOLDER . '/static-export';
}
$url = ($url) ? $url : Director::baseURL();
$symlink = ($sym != "false");
$quiet = ($quiet) ? $quiet : false;
if(!$quiet) printf("Exporting website with %s base URL... %s", $url, PHP_EOL);
if (!$quiet) {
printf("Exporting website with %s base URL... %s", $url, PHP_EOL);
}
$path = $export->doExport($url, $folder, $symlink, $quiet);
if (!$quiet) {

View File

@ -5,13 +5,15 @@
*
* @package staticpublisher
*/
class FilesystemPublisherTest extends SapphireTest {
class FilesystemPublisherTest extends SapphireTest
{
protected $usesDatabase = true;
protected $orig = array();
public function setUp() {
public function setUp()
{
parent::setUp();
SiteTree::add_extension("FilesystemPublisher('assets/FilesystemPublisherTest-static-folder/')");
@ -21,7 +23,8 @@ class FilesystemPublisherTest extends SapphireTest {
Config::inst()->update('FilesystemPublisher', 'domain_based_caching', false);
}
public function tearDown() {
public function tearDown()
{
parent::tearDown();
SiteTree::remove_extension("FilesystemPublisher('assets/FilesystemPublisherTest-static-folder/')");
@ -33,7 +36,8 @@ class FilesystemPublisherTest extends SapphireTest {
}
}
public function testUrlsToPathsWithRelativeUrls() {
public function testUrlsToPathsWithRelativeUrls()
{
$fsp = new FilesystemPublisher('.', 'html');
$this->assertEquals(
@ -55,7 +59,8 @@ class FilesystemPublisherTest extends SapphireTest {
);
}
public function testUrlsToPathsWithAbsoluteUrls() {
public function testUrlsToPathsWithAbsoluteUrls()
{
$fsp = new FilesystemPublisher('.', 'html');
$url = Director::absoluteBaseUrl();
@ -80,7 +85,8 @@ class FilesystemPublisherTest extends SapphireTest {
);
}
public function testUrlsToPathsWithDomainBasedCaching() {
public function testUrlsToPathsWithDomainBasedCaching()
{
$origDomainBasedCaching = Config::inst()->get('FilesystemPublisher', 'domain_based_caching');
Config::inst()->update('FilesystemPublisher', 'domain_based_caching', true);
@ -121,7 +127,8 @@ class FilesystemPublisherTest extends SapphireTest {
* is where extension instances are set up and subsequently used by
* {@link DataObject::defineMethods()}.
*/
public function testHasCalledParentConstructor() {
public function testHasCalledParentConstructor()
{
$fsp = new FilesystemPublisher('.', '.html');
$this->assertEquals($fsp->class, 'FilesystemPublisher');
@ -132,7 +139,8 @@ class FilesystemPublisherTest extends SapphireTest {
* correct theme when we need it. StaticPublishing needs to be able to
* retrieve a non-null theme at the time publishPages() is called.
*/
public function testStaticPublisherTheme(){
public function testStaticPublisherTheme()
{
// This will be the name of the default theme of this particular project
$default_theme = Config::inst()->get('SSViewer', 'theme');
@ -154,7 +162,8 @@ class FilesystemPublisherTest extends SapphireTest {
$this->assertNotEquals($current_theme, $default_theme, 'The static publisher theme overrides the custom theme');
}
public function testMenu2LinkingMode() {
public function testMenu2LinkingMode()
{
$this->logInWithPermission('ADMIN');
Config::inst()->update('SSViewer', 'theme', null);
@ -184,7 +193,8 @@ class FilesystemPublisherTest extends SapphireTest {
$this->assertEquals(trim($response->getBody()), "linkcurrent", "current page is level 2-2");
}
public function testContentTypeHTML() {
public function testContentTypeHTML()
{
SiteTree::remove_extension('FilesystemPublisher');
SiteTree::add_extension("FilesystemPublisher('assets/FilesystemPublisherTest-static-folder/', 'php')");
$l1 = new StaticPublisherTestPage();
@ -195,7 +205,8 @@ class FilesystemPublisherTest extends SapphireTest {
$this->assertEquals($response->getHeader('Content-Type'), 'text/html; charset=utf-8', 'Content-Type should be text/html; charset=utf-8');
}
public function testContentTypeJSON() {
public function testContentTypeJSON()
{
SiteTree::remove_extension('FilesystemPublisher');
SiteTree::add_extension("FilesystemPublisher('assets/FilesystemPublisherTest-static-folder/', 'php')");
$l1 = new StaticPublisherTestPage();
@ -210,18 +221,21 @@ class FilesystemPublisherTest extends SapphireTest {
/**
* @package staticpublisher
*/
class StaticPublisherTestPage extends Page implements TestOnly {
class StaticPublisherTestPage extends Page implements TestOnly
{
private static $allowed_children = array(
'StaticPublisherTestPage'
);
public function canPublish($member = null) {
public function canPublish($member = null)
{
return true;
}
public function getTemplate() {
public function getTemplate()
{
return STATIC_MODULE_DIR . '/tests/templates/StaticPublisherTestPage.ss';
}
}
@ -229,7 +243,8 @@ class StaticPublisherTestPage extends Page implements TestOnly {
/**
* @package staticpublisher
*/
class StaticPublisherTestPage_Controller extends Page_Controller implements TestOnly {
class StaticPublisherTestPage_Controller extends Page_Controller implements TestOnly
{
/**
*
@ -237,11 +252,10 @@ class StaticPublisherTestPage_Controller extends Page_Controller implements Test
*/
private static $allowed_actions = array('json');
public function json(SS_HTTPRequest $request) {
public function json(SS_HTTPRequest $request)
{
$response = new SS_HTTPResponse('{"firstName": "John"}');
$response->addHeader('Content-Type', 'application/json');
return $response;
}
}