'index', '$TaskName' => 'runTask' ); private static $allowed_actions = array( 'index', 'runTask', ); protected function init() { parent::init(); $allowAllCLI = DevelopmentAdmin::config()->get('allow_all_cli'); $canAccess = ( Director::isDev() // We need to ensure that DevelopmentAdminTest can simulate permission failures when running // "dev/tasks" from CLI. || (Director::is_cli() && $allowAllCLI) || Permission::check("ADMIN") ); if (!$canAccess) { Security::permissionFailure($this); } } 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(); echo "
"; 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"; } } } /** * Runs a BuildTask * @param HTTPRequest $request */ public function runTask($request) { $name = $request->param('TaskName'); $tasks = $this->getTasks(); $title = function ($content) { printf(Director::is_cli() ? "%s\n\n" : '%s
', $content); }; foreach ($tasks as $task) { if ($task['segment'] == $name) { /** @var BuildTask $inst */ $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; } } $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(); $taskClasses = ClassInfo::subclassesFor(BuildTask::class); // remove the base class array_shift($taskClasses); foreach ($taskClasses as $class) { if (!$this->taskEnabled($class)) { continue; } $singleton = BuildTask::singleton($class); $desc = (Director::is_cli()) ? Convert::html2raw($singleton->getDescription()) : $singleton->getDescription(); $availableTasks[] = array( 'class' => $class, 'title' => $singleton->getTitle(), 'segment' => $singleton->config()->segment ?: str_replace('\\', '-', $class), 'description' => $desc, ); } return $availableTasks; } /** * @param string $class * @return boolean */ protected function taskEnabled($class) { $reflectionClass = new ReflectionClass($class); if ($reflectionClass->isAbstract()) { return false; } elseif (!singleton($class)->isEnabled()) { return false; } return true; } }