Version: current

     1 <?php
     2 /**
     3  * @package sapphire
     4  * @subpackage dev
     5  */
     6 class TaskRunner extends Controller {
     7
     8     static $url_handlers = array(
     9         '' => 'index',
    10         '$TaskName' => 'runTask'
    11     );
    12
    13     function init() {
    14         parent::init();
    15
    16         $canAccess = (Director::isDev() || Director::is_cli() || Permission::check("ADMIN"));
    17         if(!$canAccess) return Security::permissionFailure($this);
    18     }
    19
    20     function index() {
    21         $tasks = $this->getTasks();
    22
    23         // Web mode
    24         if(!Director::is_cli()) {
    25             $renderer = new DebugView();
    26             $renderer->writeHeader();
    27             $renderer->writeInfo("Sapphire Development Tools: Tasks", Director::absoluteBaseURL());
    28             $base = Director::absoluteBaseURL();
    29
    30             echo "<ul>";
    31             foreach($tasks as $task) {
    32                 echo "<li>";
    33                 echo "<a href=\"{$base}dev/tasks/" . $task['class'] . "\">" . $task['title'] . "</a><br />";
    34                 echo "<span class=\"description\">" . $task['description'] . "</span>";
    35                 echo "</li>\n";
    36             }
    37             echo "</ul>";
    38
    39             $renderer->writeFooter();
    40         // CLI mode
    41         } else {
    42             echo "SAPPHIRE DEVELOPMENT TOOLS: Tasks\n--------------------------\n\n";
    43             foreach($tasks as $task) {
    44                 echo " * $task: sake dev/tasks/" . $task['class'] . "\n";
    45             }
    46         }
    47     }
    48
    49     function runTask($request) {
    50         $taskName = $request->param('TaskName');
    51         if (class_exists($taskName) && is_subclass_of($taskName, 'BuildTask')) {
    52             $title = singleton($taskName)->getTitle();
    53             if(Director::is_cli()) echo "Running task '$title'...\n\n";
    54             elseif(!Director::is_ajax()) echo "<h1>Running task '$title'...</h1>\n";
    55
    56             $task = new $taskName();
    57             if ($task->isEnabled()) $task->run($request);
    58             else echo "<p>{$title} is disabled</p>";
    59         } else {
    60             echo "Build task '$taskName' not found.";
    61             if(class_exists($taskName)) echo "  It isn't a subclass of BuildTask.";
    62             echo "\n";
    63         }
    64     }
    65
    66     /**
    67      * @return array Array of associative arrays for each task (Keys: 'class', 'title', 'description')
    68      */
    69     protected function getTasks() {
    70         $availableTasks = array();
    71
    72         $taskClasses = ClassInfo::subclassesFor('BuildTask');
    73         // remove the base class
    74         array_shift($taskClasses);
    75
    76         if($taskClasses) foreach($taskClasses as $class) {
    77             if(!singleton($class)->isEnabled()) continue;
    78             $desc = (Director::is_cli()) ? Convert::html2raw(singleton($class)->getDescription()) : singleton($class)->getDescription();
    79             $availableTasks[] = array(
    80                 'class' => $class,
    81                 'title' => singleton($class)->getTitle(),
    82                 'description' => $desc,
    83             );
    84         }
    85
    86         return $availableTasks;
    87     }
    88
    89 }
    90
    91 ?>
    92

[Raise a SilverStripe Framework issue/bug](https://github.com/silverstripe/silverstripe-framework/issues/new)
- [Raise a SilverStripe CMS issue/bug](https://github.com/silverstripe/silverstripe-cms/issues/new)
- Please use the Silverstripe Forums to ask development related questions. -