diff --git a/_config/dev.yml b/_config/dev.yml index 4c1636bc4b5..ba535253a6f 100644 --- a/_config/dev.yml +++ b/_config/dev.yml @@ -2,6 +2,9 @@ Name: DevelopmentAdmin --- SilverStripe\Dev\DevelopmentAdmin: + commands: + config: 'SilverStripe\Dev\HybridExecution\Command\DevConfig' + 'config:audit': 'SilverStripe\Dev\HybridExecution\Command\DevConfigAudit' registered_controllers: build: controller: SilverStripe\Dev\DevBuildController @@ -13,10 +16,6 @@ SilverStripe\Dev\DevelopmentAdmin: tasks: 'See a list of build tasks to run' confirm: controller: SilverStripe\Dev\DevConfirmationController - config: - controller: Silverstripe\Dev\DevConfigController - links: - config: 'View the current config, useful for debugging' SilverStripe\Dev\CSSContentParser: disable_xml_external_entities: true diff --git a/bin/sake b/bin/sake new file mode 100755 index 00000000000..c9a74a8f709 --- /dev/null +++ b/bin/sake @@ -0,0 +1,21 @@ +#!/usr/bin/env php +addCommands([ + // probably do this inside the sake app itself though + // TODO: + // - flush + // - navigate (use HTTPRequest and spin off a "web" request from CLI) +]); +$sake->run(); diff --git a/cli-script.php b/cli-script.php deleted file mode 100755 index 879b2de6546..00000000000 --- a/cli-script.php +++ /dev/null @@ -1,35 +0,0 @@ -handle($request); - -$response->output(); diff --git a/composer.json b/composer.json index 8c31c99f12b..03f98562265 100644 --- a/composer.json +++ b/composer.json @@ -19,7 +19,7 @@ } ], "bin": [ - "sake" + "bin/sake" ], "require": { "php": "^8.3", @@ -36,12 +36,14 @@ "psr/container": "^1.1 || ^2.0", "psr/http-message": "^1", "sebastian/diff": "^4.0", + "sensiolabs/ansi-to-html": "^1.2", "silverstripe/config": "^3", "silverstripe/assets": "^3", "silverstripe/vendor-plugin": "^2", "sminnee/callbacklist": "^0.1.1", "symfony/cache": "^6.1", "symfony/config": "^6.1", + "symfony/console": "^7.0", "symfony/dom-crawler": "^6.1", "symfony/filesystem": "^6.1", "symfony/http-foundation": "^6.1", @@ -85,6 +87,7 @@ }, "autoload": { "psr-4": { + "SilverStripe\\Cli\\": "src/Cli/", "SilverStripe\\Control\\": "src/Control/", "SilverStripe\\Control\\Tests\\": "tests/php/Control/", "SilverStripe\\Core\\": "src/Core/", diff --git a/sake b/sake deleted file mode 100755 index 59103445b54..00000000000 --- a/sake +++ /dev/null @@ -1,119 +0,0 @@ -#!/usr/bin/env bash - -# Check for an argument -if [ ${1:-""} = "" ]; then - echo "SilverStripe Sake - -Usage: $0 (command-url) (params) -Executes a SilverStripe command" - exit 1 -fi - -command -v which >/dev/null 2>&1 -if [ $? -ne 0 ]; then - echo "Error: sake requires the 'which' command to operate." >&2 - exit 1 -fi - -# find the silverstripe installation, looking first at sake -# bin location, but falling back to current directory -sakedir=`dirname $0` -directory="$PWD" -if [ -f "$sakedir/cli-script.php" ]; then - # Calling sake from vendor/silverstripe/framework/sake - framework="$sakedir" - base="$sakedir/../../.." -elif [ -f "$sakedir/../silverstripe/framework/cli-script.php" ]; then - # Calling sake from vendor/bin/sake - framework="$sakedir/../silverstripe/framework" - base="$sakedir/../.." -elif [ -f "$directory/vendor/silverstripe/framework/cli-script.php" ]; then - # Vendor framework (from base) if sake installed globally - framework="$directory/vendor/silverstripe/framework" - base=. -elif [ -f "$directory/framework/cli-script.php" ]; then - # Legacy directory (from base) if sake installed globally - framework="$directory/framework" - base=. -else - echo "Can't find cli-script.php in $sakedir" - exit 1 -fi - -# Find the PHP binary -for candidatephp in php php5; do - if [ "`which $candidatephp 2>/dev/null`" -a -f "`which $candidatephp 2>/dev/null`" ]; then - php=`which $candidatephp 2>/dev/null` - break - fi -done -if [ "$php" = "" ]; then - echo "Can't find any php binary" - exit 2 -fi - -################################################################################################ -## Installation to /usr/bin - -if [ "$1" = "installsake" ]; then - echo "Installing sake to /usr/local/bin..." - rm -rf /usr/local/bin/sake - cp $0 /usr/local/bin - exit 0 -fi - -################################################################################################ -## Process control - -if [ "$1" = "-start" ]; then - if [ "`which daemon`" = "" ]; then - echo "You need to install the 'daemon' tool. In debian, go 'sudo apt-get install daemon'" - exit 1 - fi - - if [ ! -f $base/$2.pid ]; then - echo "Starting service $2 $3" - touch $base/$2.pid - pidfile=`realpath $base/$2.pid` - - outlog=$base/$2.log - errlog=$base/$2.err - - echo "Logging to $outlog" - - sake=`realpath $0` - base=`realpath $base` - - # if third argument is not explicitly given, copy from second argument - if [ "$3" = "" ]; then - url=$2 - else - url=$3 - fi - - processname=$2 - - daemon -n $processname -r -D $base --pidfile=$pidfile --stdout=$outlog --stderr=$errlog $sake $url - else - echo "Service $2 seems to already be running" - fi - exit 0 -fi - -if [ "$1" = "-stop" ]; then - pidfile=$base/$2.pid - if [ -f $pidfile ]; then - echo "Stopping service $2" - - kill -KILL `cat $pidfile` - unlink $pidfile - else - echo "Service $2 doesn't seem to be running." - fi - exit 0 -fi - -################################################################################################ -## Basic execution - -"$php" "$framework/cli-script.php" "${@}" diff --git a/src/Cli/ArrayCommandLoader.php b/src/Cli/ArrayCommandLoader.php new file mode 100644 index 00000000000..ec518106d3d --- /dev/null +++ b/src/Cli/ArrayCommandLoader.php @@ -0,0 +1,52 @@ + + */ + private array $loaders = []; + + public function __construct(array $loaders) + { + $this->loaders = $loaders; + } + + public function get(string $name): Command + { + foreach ($this->loaders as $loader) { + if ($loader->has($name)) { + return $loader->get($name); + } + } + throw new CommandNotFoundException("Can't find command $name"); + } + + public function has(string $name): bool + { + foreach ($this->loaders as $loader) { + if ($loader->has($name)) { + return true; + } + } + return false; + } + + public function getNames(): array + { + $names = []; + foreach ($this->loaders as $loader) { + $names = array_merge($names, $loader->getNames()); + } + return array_unique($names); + } +} diff --git a/src/Cli/DevCommandLoader.php b/src/Cli/DevCommandLoader.php new file mode 100644 index 00000000000..ff11e39fbc8 --- /dev/null +++ b/src/Cli/DevCommandLoader.php @@ -0,0 +1,89 @@ +init(); + $name = $this->deAlias($name); + if (!$this->has($name)) { + throw new CommandNotFoundException("Can't find command $name"); + } + /** @var HybridCommand $commandClass */ + $commandClass = $this->commands[$name]; + $hybridCommand = $commandClass::create(); + // Use the name that was passed into the method instead of fetching from the hybrid command + // because it includes the full namespace. + $command = new Command($name); + $command->setAliases([$this->makeAlias($name)]); + $command->setDescription($hybridCommand->getDescription()); + $command->setCode(function (InputInterface $input, OutputInterface $output) use ($hybridCommand) { + $hybridOutput = HybridOutput::create( + HybridOutput::CONTEXT_CONSOLE, + $output->getVerbosity(), + $output->isDecorated(), + $output + ); + // TODO make the title look a lil nicer + $hybridOutput->writeln([$hybridCommand->getTitle(), '--------']); + return $hybridCommand->run($input, $hybridOutput); + }); + return $command; + } + + public function has(string $name): bool + { + $this->init(); + if (array_key_exists($name, $this->commands)) { + return true; + } + var_dump($name); + return array_key_exists($this->deAlias($name), $this->commands); + } + + public function getNames(): array + { + $this->init(); + return array_keys($this->commands); + } + + private function init(): void + { + if (!empty($this->commands)) { + return; + } + $commands = DevelopmentAdmin::singleton()->getCommands(); + foreach ($commands as $name => $class) { + if (!$class::canRunInBrowser()) { + unset($commands[$name]); + } + } + $this->commands = $commands; + } + + private function deAlias(string $name): string + { + return str_replace('/', ':', $name); + } + + private function makeAlias(string $name): string + { + return str_replace(':', '/', $name); + } +} diff --git a/src/Cli/InjectorCommandLoader.php b/src/Cli/InjectorCommandLoader.php new file mode 100644 index 00000000000..3bcb1a5f1ee --- /dev/null +++ b/src/Cli/InjectorCommandLoader.php @@ -0,0 +1,32 @@ +setCommandLoader(new ArrayCommandLoader([ + new InjectorCommandLoader(), + new DevCommandLoader(), + ])); + } + + public function getVersion(): string + { + return VersionProvider::singleton()->getVersion(); + } + + public function run(?InputInterface $input = null, ?OutputInterface $output = null): int + { + $input ??= new ArgvInput(); + + $skipDatabase = $input->hasParameterOption('--no-database', true); + if ($skipDatabase) { + DB::set_conn(new NullDatabase()); + } + + // // Build request and detect flush + // $request = CLIRequestBuilder::createFromEnvironment(); + + // Instantiate the kernel + // TODO: Replace with a single CliKernel implementation + // TODO: Figure out how to get this earlier, so we can use config/injection to build commands list + $this->kernel = $skipDatabase + ? new DatabaselessKernel(BASE_PATH) + : new CoreKernel(BASE_PATH); + // $app = new HTTPApplication($kernel); + // $response = $app->handle($request); + + // $response->output(); + + try { + $flush = $input->hasParameterOption('--flush', true); + // TODO boot the kernel earlier so we can use config/injection to build commands list + // Probably means figuring out a way to flush post-boot?? + // How does symfony itself handle that? + $this->kernel->boot($flush); + return parent::run($input, $output); + } finally { + $this->kernel->shutdown(); + } + } + + protected function getDefaultInputDefinition(): InputDefinition + { + $definition = parent::getDefaultInputDefinition(); + $definition->addOptions([ + new InputOption('--no-database', null, InputOption::VALUE_NONE, 'Run the command without connecting to the database'), + new InputOption('--flush', null, InputOption::VALUE_NONE, 'Flush the cache before running the command'), + ]); + return $definition; + } +} diff --git a/src/Core/Injector/Injector.php b/src/Core/Injector/Injector.php index afb909a5b84..a0deace9635 100644 --- a/src/Core/Injector/Injector.php +++ b/src/Core/Injector/Injector.php @@ -972,7 +972,7 @@ public function unregisterObjects($types) * @param bool $asSingleton If set to false a new instance will be returned. * If true a singleton will be returned unless the spec is type=prototype' * @param array $constructorArgs Args to pass in to the constructor. Note: Ignored for singletons - * @return T|mixed Instance of the specified object + * @return T Instance of the specified object */ public function get($name, $asSingleton = true, $constructorArgs = []) { diff --git a/src/Dev/DevConfigController.php b/src/Dev/DevConfigController.php index 03c53281056..2539a1de7a3 100644 --- a/src/Dev/DevConfigController.php +++ b/src/Dev/DevConfigController.php @@ -18,7 +18,6 @@ */ class DevConfigController extends Controller implements PermissionProvider { - /** * @var array */ @@ -157,7 +156,7 @@ public function canInit(): bool || Permission::check(static::config()->get('init_permissions')) ); } - + public function providePermissions(): array { return [ @@ -169,31 +168,4 @@ public function providePermissions(): array ], ]; } - - /** - * Returns all the keys of a multi-dimensional array while maintining any nested structure - * - * @param array $array - * @param int $maxdepth - * @param int $depth - * @param array $arrayKeys - * @return array - */ - private function array_keys_recursive($array, $maxdepth = 20, $depth = 0, $arrayKeys = []) - { - if ($depth < $maxdepth) { - $depth++; - $keys = array_keys($array ?? []); - - foreach ($keys as $key) { - if (!is_array($array[$key])) { - continue; - } - - $arrayKeys[$key] = $this->array_keys_recursive($array[$key], $maxdepth, $depth); - } - } - - return $arrayKeys; - } } diff --git a/src/Dev/DevelopmentAdmin.php b/src/Dev/DevelopmentAdmin.php index 752ae82dd64..bb4dec34f74 100644 --- a/src/Dev/DevelopmentAdmin.php +++ b/src/Dev/DevelopmentAdmin.php @@ -3,6 +3,7 @@ namespace SilverStripe\Dev; use Exception; +use LogicException; use SilverStripe\Control\Controller; use SilverStripe\Control\Director; use SilverStripe\Control\HTTPRequest; @@ -10,52 +11,67 @@ use SilverStripe\Core\ClassInfo; use SilverStripe\Core\Config\Config; use SilverStripe\Core\Injector\Injector; -use SilverStripe\Dev\Deprecation; +use SilverStripe\Dev\HybridExecution\Command\HybridCommand; +use SilverStripe\Dev\HybridExecution\HybridOutput; use SilverStripe\ORM\DatabaseAdmin; use SilverStripe\Security\Permission; use SilverStripe\Security\PermissionProvider; use SilverStripe\Security\Security; use SilverStripe\Versioned\Versioned; +use Symfony\Component\Console\Input\ArrayInput; /** * Base class for development tools. * - * Configured in framework/_config/dev.yml, with the config key registeredControllers being - * used to generate the list of links for /dev. + * Configured via the `commands` and `controllers` configuration properties */ class DevelopmentAdmin extends Controller implements PermissionProvider { - private static $url_handlers = [ '' => 'index', 'build/defaults' => 'buildDefaults', 'generatesecuretoken' => 'generatesecuretoken', - '$Action' => 'runRegisteredController', + '$Action' => 'runRegisteredAction', ]; private static $allowed_actions = [ 'index', 'buildDefaults', - 'runRegisteredController', + 'runRegisteredAction', 'generatesecuretoken', ]; /** - * Controllers for dev admin views + * Commands for dev admin views. + * + * Register any HybridCommand classes that you want to be under the `/dev/*` HTTP + * route and in the `dev:*` CLI namespace. + * + * Any namespaced commands will be nested under the `dev:*` CLI namespace, e.g + * `dev:my-namespace:command-two` + * + * Namespaces are also converted to URL segments for HTTP requests, e.g + * `dev/my-namspace/command-two` * * e.g [ - * 'urlsegment' => [ - * 'controller' => 'SilverStripe\Dev\DevelopmentAdmin', - * 'links' => [ - * 'urlsegment' => 'description', - * ... - * ] - * ] + * 'command-one' => 'App\HybridExecution\CommandOne', + * 'my-namespace:command-two' => 'App\HybridExecution\MyNamespace\CommandTwo', * ] + */ + private static array $commands = []; + + /** + * Controllers for dev admin views. + * + * This is for HTTP-only controllers routed under `/dev/*` which + * cannot be managed via CLI (e.g. an interactive GraphQL IDE). + * For most purposes, register a hybrid command under $commands instead. * - * @var array + * e.g [ + * 'urlsegment' => 'App\Dev\MyHttpOnlyController', + * ] */ - private static $registered_controllers = []; + private static array $controllers = []; /** * Assume that CLI equals admin permissions @@ -82,7 +98,7 @@ protected function init() if (static::config()->get('deny_non_cli') && !Director::is_cli()) { return $this->httpError(404); } - + if (!$this->canViewAll() && empty($this->getLinks())) { Security::permissionFailure($this); return; @@ -96,123 +112,170 @@ protected function init() } } + /** + * Renders the main /dev menu in the browser + */ public function index() { - $links = $this->getLinks(); - // Web mode - if (!Director::is_cli()) { - $renderer = DebugView::create(); - echo $renderer->renderHeader(); - echo $renderer->renderInfo("SilverStripe Development Tools", Director::absoluteBaseURL()); - $base = Director::baseURL(); - - echo '