silverstripe-framework/Dev/TaskRunner.php

154 lines
3.7 KiB
PHP
Raw Normal View History

<?php
2016-06-23 01:37:22 +02:00
namespace SilverStripe\Dev;
use SilverStripe\Control\Controller;
use SilverStripe\Control\Director;
use SilverStripe\Control\SS_HTTPRequest;
use SilverStripe\Core\ClassInfo;
use SilverStripe\Core\Convert;
use SilverStripe\Core\Injector\Injector;
2016-06-23 01:37:22 +02:00
use SilverStripe\Security\Permission;
use SilverStripe\Security\Security;
use ReflectionClass;
class TaskRunner extends Controller {
2014-08-15 08:53:05 +02:00
private static $url_handlers = array(
'' => 'index',
'$TaskName' => 'runTask'
);
2014-08-15 08:53:05 +02:00
private static $allowed_actions = array(
'index',
'runTask',
);
2014-08-15 08:53:05 +02:00
protected function init() {
parent::init();
$isRunningTests = (class_exists('SilverStripe\\Dev\\SapphireTest', false) && SapphireTest::is_running_test());
$canAccess = (
2014-08-15 08:53:05 +02:00
Director::isDev()
// We need to ensure that DevelopmentAdminTest can simulate permission failures when running
// "dev/tasks" from CLI.
|| (Director::is_cli() && !$isRunningTests)
|| Permission::check("ADMIN")
);
if(!$canAccess) {
Security::permissionFailure($this);
}
}
2014-08-15 08:53:05 +02:00
public function index() {
$tasks = $this->getTasks();
// Web mode
if(!Director::is_cli()) {
$renderer = new DebugView();
echo $renderer->renderHeader();
echo $renderer->renderInfo("SilverStripe Development Tools: Tasks", Director::absoluteBaseURL());
$base = Director::absoluteBaseURL();
2014-08-15 08:53:05 +02:00
echo "<div class=\"options\">";
echo "<ul>";
foreach($tasks as $task) {
echo "<li><p>";
echo "<a href=\"{$base}dev/tasks/" . $task['segment'] . "\">" . $task['title'] . "</a><br />";
echo "<span class=\"description\">" . $task['description'] . "</span>";
echo "</p></li>\n";
}
echo "</ul></div>";
echo $renderer->renderFooter();
// CLI mode
} else {
echo "SILVERSTRIPE DEVELOPMENT TOOLS: Tasks\n--------------------------\n\n";
foreach($tasks as $task) {
echo " * $task[title]: sake dev/tasks/" . $task['segment'] . "\n";
}
}
}
2014-08-15 08:53:05 +02:00
2016-06-23 01:37:22 +02:00
/**
* @param SS_HTTPRequest $request
*/
public function runTask($request) {
$name = $request->param('TaskName');
$tasks = $this->getTasks();
$title = function ($content) {
printf(Director::is_cli() ? "%s\n\n" : '<h1>%s</h1>', $content);
};
$message = function ($content) {
printf(Director::is_cli() ? "%s\n" : '<p>%s</p>', $content);
};
foreach ($tasks as $task) {
if ($task['segment'] == $name) {
$inst = Injector::inst()->create($task['class']);
$title(sprintf('Running Task %s', $inst->getTitle()));
if (!$inst->isEnabled()) {
$message('The task is disabled');
return;
}
$inst->run($request);
return;
}
}
2014-08-13 07:29:12 +02:00
$message(sprintf('The build task "%s" could not be found', Convert::raw2xml($name)));
}
/**
* @return array Array of associative arrays for each task (Keys: 'class', 'title', 'description')
*/
protected function getTasks() {
$availableTasks = array();
2014-08-15 08:53:05 +02:00
$taskClasses = ClassInfo::subclassesFor('SilverStripe\\Dev\\BuildTask');
// remove the base class
array_shift($taskClasses);
2014-08-15 08:53:05 +02:00
foreach($taskClasses as $class) {
if (!$this->taskEnabled($class)) {
continue;
}
$singleton = BuildTask::singleton($class);
2014-08-15 08:53:05 +02:00
$desc = (Director::is_cli())
? Convert::html2raw($singleton->getDescription())
: $singleton->getDescription();
2014-08-15 08:53:05 +02:00
$availableTasks[] = array(
'class' => $class,
'title' => $singleton->getTitle(),
'segment' => $singleton->config()->segment ?: str_replace('\\', '-', $class),
'description' => $desc,
);
}
2014-08-15 08:53:05 +02:00
return $availableTasks;
}
/**
* @param string $class
* @return boolean
*/
protected function taskEnabled($class) {
$reflectionClass = new ReflectionClass($class);
if ($reflectionClass->isAbstract()) {
return false;
} else if (!singleton($class)->isEnabled()) {
return false;
}
return true;
}
}