Skip to content

Commit

Permalink
quick entity access (#5709)
Browse files Browse the repository at this point in the history
* quick entity access

* added usage

* Add DI

* Move Usage

---------

Co-authored-by: Moshe Weitzman <[email protected]>
  • Loading branch information
chx and weitzman authored Jul 19, 2023
1 parent cd27cb2 commit bc91708
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 1 deletion.
2 changes: 2 additions & 0 deletions docs/repl.md
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
The [php:cli command](commands/php_cli.md) is interactive PHP REPL with your bootstrapped site (remote or local). It’s a Drupal code playground. You can do quick code experimentation, grab some data, or run Drush commands. This can also help with debugging certain issues. See [this blog post](http://blog.damiankloip.net/2015/drush-php) for an introduction. Run `help` for a list of commands.

Entity classes are available without their namespace. For example, Node::load() works instead of Drupal\Node\entity\Noad::load().
25 changes: 24 additions & 1 deletion src/Commands/core/CliCommands.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace Drush\Commands\core;

use Consolidation\AnnotatedCommand\AnnotatedCommand;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drush\Attributes as CLI;
use Drush\Commands\DrushCommands;
use Drush\Drush;
Expand All @@ -17,12 +17,27 @@
use Psy\Configuration;
use Psy\VersionUpdater\Checker;
use Drush\Boot\DrupalBootLevels;
use Symfony\Component\DependencyInjection\ContainerInterface;

final class CliCommands extends DrushCommands
{
const DOCS_REPL = 'docs:repl';
const PHP = 'php:cli';

public function __construct(protected EntityTypeManagerInterface $entityTypeManager)
{
parent::__construct();
}

public static function create(ContainerInterface $container): self
{
$commandHandler = new static(
$container->get('entity_type.manager'),
);

return $commandHandler;
}

/**
* Drush's PHP Shell.
*/
Expand All @@ -42,6 +57,7 @@ public function docs(): void
#[CLI\Option(name: 'version-history', description: 'Use command history based on Drupal version. Default is per site.')]
#[CLI\Option(name: 'cwd', description: 'A directory to change to before launching the shell. Default is the project root directory')]
#[CLI\Topics(topics: [self::DOCS_REPL])]
#[CLI\Usage(name: '$node = Node::load(1)', description: 'Entity classes are available without their namespace. For example, Node::load(1) works instead of Drupal\Node\entity\Node::load(1).')]
#[CLI\Bootstrap(level: DrupalBootLevels::FULL)]
public function cli(array $options = ['version-history' => false, 'cwd' => self::REQ]): void
{
Expand Down Expand Up @@ -98,6 +114,13 @@ public function cli(array $options = ['version-history' => false, 'cwd' => self:
chdir($options['cwd']);
}

// Make entities available with short class names.
foreach ($this->entityTypeManager->getDefinitions() as $definition) {
$class = $definition->getClass();
$parts = explode('\\', $class);
class_alias($class, array_pop($parts));
}

$shell->run();
}

Expand Down
1 change: 1 addition & 0 deletions src/Commands/core/EntityCommands.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ final class EntityCommands extends DrushCommands

public function __construct(protected EntityTypeManagerInterface $entityTypeManager)
{
parent::__construct();
}

public static function create(ContainerInterface $container): self
Expand Down

0 comments on commit bc91708

Please sign in to comment.