Skip to content

Commit

Permalink
Revert "Fix up makeEntitiesAvailableWithShortClassNames for php:cli (#…
Browse files Browse the repository at this point in the history
…6221) (…" (#6224)

This reverts commit e6979ef.
  • Loading branch information
weitzman authored Feb 7, 2025
1 parent e6979ef commit c158e23
Showing 1 changed file with 13 additions and 39 deletions.
52 changes: 13 additions & 39 deletions src/Commands/core/CliCommands.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@

namespace Drush\Commands\core;

use Drupal\Core\Entity\EntityTypeBundleInfoInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Entity\EntityTypeRepositoryInterface;
use Drush\Attributes as CLI;
use Drush\Commands\DrushCommands;
use Drush\Drush;
Expand All @@ -26,19 +24,15 @@ final class CliCommands extends DrushCommands
const PHP = 'php:cli';

public function __construct(
protected EntityTypeManagerInterface $entityTypeManager,
protected EntityTypeRepositoryInterface $entityTypeRepository,
protected EntityTypeBundleInfoInterface $entityTypeBundleInfo
protected EntityTypeManagerInterface $entityTypeManager
) {
parent::__construct();
}

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

return $commandHandler;
Expand All @@ -64,7 +58,6 @@ public function docs(): void
#[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\Usage(name: '$node = NodeArticle::load(1)', description: 'Entity bundles classes are also available without their namespace. For example, NodeArticle::load(1) works instead of Drupal\node_article\entity\NodeArticle::load(1).')]
#[CLI\Usage(name: '$paragraph = Paragraph::loadRevision(1)', description: 'Also, a loadRevision static method is made available for easier load of revisions.')]
#[CLI\Bootstrap(level: DrupalBootLevels::FULL)]
public function cli(array $options = ['version-history' => false, 'cwd' => self::REQ]): void
Expand Down Expand Up @@ -315,43 +308,24 @@ protected function getPhpKeywords(): array

public function makeEntitiesAvailableWithShortClassNames(): void
{
// The entity type repository stores a map from class name to entity
// type id, sneak our short classes in there.
$classNameEntityTypeMapReflection = (new \ReflectionObject($this->entityTypeRepository))->getProperty('classNameEntityTypeMap');
$classNameEntityTypeMap = $classNameEntityTypeMapReflection->getValue($this->entityTypeRepository);
foreach ($this->entityTypeManager->getDefinitions() as $entityTypeId => $definition) {
$classNameEntityTypeMap = $this->createShortClassForEntityClass($definition->getClass(), $entityTypeId, $classNameEntityTypeMap);
foreach ($this->entityTypeBundleInfo->getAllBundleInfo() as $bundles) {
foreach ($bundles as $info) {
if (isset($info['class'])) {
$classNameEntityTypeMap = $this->createShortClassForEntityClass($info['class'], $entityTypeId, $classNameEntityTypeMap);
}
}
foreach ($this->entityTypeManager->getDefinitions() as $definition) {
$class = $definition->getClass();
$reflectionClass = new \ReflectionClass($class);
$parts = explode('\\', $class);
$end = end($parts);
// https://github.com/drush-ops/drush/pull/5729 and https://github.com/drush-ops/drush/issues/5730.
if ($reflectionClass->isAbstract() || $reflectionClass->isFinal() || class_exists($end)) {
continue;
}
}
$classNameEntityTypeMapReflection->setValue($this->entityTypeRepository, $classNameEntityTypeMap);
}

public function createShortClassForEntityClass(string $class, string $entityTypeId, array $classNameEntityTypeMap): array
{
$reflectionClass = new \ReflectionClass($class);
$parts = explode('\\', $class);
$end = end($parts);
// https://github.com/drush-ops/drush/pull/5729, https://github.com/drush-ops/drush/issues/5730
// and https://github.com/drush-ops/drush/issues/5899.
if ($reflectionClass->isFinal() || $reflectionClass->isAbstract() || class_exists($end)) {
return $classNameEntityTypeMap;
}
$classNameEntityTypeMap[$end] = $entityTypeId;
// Make it possible to easily load revisions.
eval(sprintf('class %s extends %s {
// Make it possible to easily load revisions.
eval(sprintf('class %s extends %s {
public static function loadRevision($id) {
$entity_type_repository = \Drupal::service("entity_type.repository");
$entity_type_manager = \Drupal::entityTypeManager();
$storage = $entity_type_manager->getStorage($entity_type_repository->getEntityTypeFromClass(static::class));
return $storage->loadRevision($id);
}
}', $end, $class));
return $classNameEntityTypeMap;
}
}
}

0 comments on commit c158e23

Please sign in to comment.