Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhance entity finder #87

Merged
merged 8 commits into from
Jan 6, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"symfony/cache": "^3.4|^4.4|^5.0",
"symfony/config": "^3.4|^4.4|^5.0",
"symfony/deprecation-contracts": "^1.0 || ^2.0 || ^3.0",
"symfony/event-dispatcher-contracts": "^1.0 || ^2.0 || ^3.0",
"symfony/filesystem": "^3.4|^4.4|^5.0",
"symfony/http-foundation": "^4.4 || ^5.0",
"symfony/http-kernel": "^3.4|^4.4|^5.0",
Expand Down
91 changes: 50 additions & 41 deletions src/Command/EntityFinderCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,16 @@
use Contao\Controller;
use Contao\CoreBundle\Framework\ContaoFramework;
use Contao\Database;
use Contao\DC_Table;
use Contao\LayoutModel;
use Contao\ModuleModel;
use Contao\PageModel;
use Contao\ThemeModel;
use Doctrine\DBAL\Connection;
use HeimrichHannot\UtilsBundle\EntityFinder\EntityFinderHelper;
use HeimrichHannot\UtilsBundle\EntityFinder\Finder;
use HeimrichHannot\UtilsBundle\Event\ExtendEntityFinderEvent;
use HeimrichHannot\UtilsBundle\Util\Utils;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
Expand All @@ -31,29 +34,30 @@ class EntityFinderCommand extends Command
{
protected static $defaultName = 'huh:utils:entity_finder';

/** @var ContaoFramework */
private $contaoFramework;
/**
* @var EventDispatcherInterface
*/
private $eventDispatcher;
/**
* @var Connection
*/
private $connection;
/**
* @var EntityFinderHelper
*/
private $entityFinderHelper;

public function __construct(ContaoFramework $contaoFramework, EventDispatcherInterface $eventDispatcher, Connection $connection, EntityFinderHelper $entityFinderHelper)
private ContaoFramework $contaoFramework;
private EventDispatcherInterface $eventDispatcher;
private Connection $connection;
private EntityFinderHelper $entityFinderHelper;
private Utils $utils;
private Finder $finder;

public function __construct(
ContaoFramework $contaoFramework,
EventDispatcherInterface $eventDispatcher,
Connection $connection,
EntityFinderHelper $entityFinderHelper,
Utils $utils,
Finder $finder
)
{
parent::__construct();

$this->contaoFramework = $contaoFramework;
$this->eventDispatcher = $eventDispatcher;
$this->connection = $connection;
$this->entityFinderHelper = $entityFinderHelper;
$this->utils = $utils;
$this->finder = $finder;
}

protected function configure()
Expand Down Expand Up @@ -159,10 +163,10 @@ private function findEntity(string $table, $id, array &$parents, bool $onlyText
if ($element) {
$parents[] = ['table' => $element->ptable, 'id' => $element->pid];

return 'Content Element: '.($GLOBALS['TL_LANG']['CTE'][$element->type][0] ?? $element->type).' (ID: '.$element->id.', Type: '.$element->type.')';
return 'Content Element: ' . ($GLOBALS['TL_LANG']['CTE'][$element->type][0] ?? $element->type) . ' (ID: ' . $element->id . ', Type: ' . $element->type . ')';
}

return 'Content Element not found: ID '.$id;
return 'Content Element not found: ID ' . $id;

case ArticleModel::getTable():
$element = ArticleModel::findByPk($id);
Expand All @@ -179,10 +183,10 @@ private function findEntity(string $table, $id, array &$parents, bool $onlyText
}
}

return 'Article: '.$element->title.' (ID: '.$element->id.')';
return 'Article: ' . $element->title . ' (ID: ' . $element->id . ')';
}

return 'Article not found: ID '.$id;
return 'Article not found: ID ' . $id;

case ModuleModel::getTable():
if ($onlyText) {
Expand All @@ -195,39 +199,53 @@ private function findEntity(string $table, $id, array &$parents, bool $onlyText
$this->findFrontendModuleParents($element, $parents, $id);
}

return 'Frontend module: '.($GLOBALS['TL_LANG']['FMD'][$element->type][0] ?? $element->type).' (ID: '.$element->id.', Type: '.$element->type.')';
return 'Frontend module: ' . ($GLOBALS['TL_LANG']['FMD'][$element->type][0] ?? $element->type) . ' (ID: ' . $element->id . ', Type: ' . $element->type . ')';
}

return 'Frontend module not found: ID '.$id;
return 'Frontend module not found: ID ' . $id;

case LayoutModel::getTable():
$layout = LayoutModel::findById($id);

if ($layout) {
$parents[] = ['table' => ThemeModel::getTable(), 'id' => $layout->pid];

return 'Layout: '.html_entity_decode($layout->name).' (ID: '.$layout->id.')';
return 'Layout: ' . html_entity_decode($layout->name) . ' (ID: ' . $layout->id . ')';
}

return 'Layout not found: ID '.$id;
return 'Layout not found: ID ' . $id;

case ThemeModel::getTable():
$theme = ThemeModel::findByPk($id);

if ($theme) {
return '<options=bold>Theme: '.$theme->name.'</> (ID: '.$theme->id.')';
return '<options=bold>Theme: ' . $theme->name . '</> (ID: ' . $theme->id . ')';
}

return 'Theme not found: ID '.$id;
return 'Theme not found: ID ' . $id;

case PageModel::getTable():
$page = PageModel::findByPk($id);

if ($page) {
return '<options=bold>Page: '.$page->title.'</> (ID: '.$page->id.', Type: '.$page->type.', DNS: '.$page->getFrontendUrl().' )';
return '<options=bold>Page: ' . $page->title . '</> (ID: ' . $page->id . ', Type: ' . $page->type . ', DNS: ' . $page->getFrontendUrl() . ' )';
}

return 'Page not found: ID '.$id;
return 'Page not found: ID ' . $id;
}

$element = $this->finder->find($table, $id);
if ($element) {
if ($onlyText) {
return $element->getDescription();
}
if (null === $element->getParents()) {
return null;
}
foreach ($element->getParents()($element->getTable(), $element->getId()) as $parent) {
$parents[] = ['table' => $parent['table'], 'id' => $parent['id']];
}
return null;
}

return null;
Expand All @@ -253,19 +271,10 @@ private function createText(string $table, $id): string

private function runExtendEntityFinderEvent(string $table, $id, array $parents, bool $onlyText = false): ExtendEntityFinderEvent
{
/* @var ExtendEntityFinderEvent $event */
if (is_subclass_of($this->eventDispatcher, 'Symfony\Contracts\EventDispatcher\EventDispatcherInterface')) {
$event = $this->eventDispatcher->dispatch(
new ExtendEntityFinderEvent($table, $id, $parents, [], $this->entityFinderHelper, $onlyText),
ExtendEntityFinderEvent::class
);
} else {
/** @noinspection PhpParamsInspection */
$event = $this->eventDispatcher->dispatch(
ExtendEntityFinderEvent::class,
new ExtendEntityFinderEvent($table, $id, $parents, [], $this->entityFinderHelper, $onlyText)
);
}
$event = $this->eventDispatcher->dispatch(
new ExtendEntityFinderEvent($table, $id, $parents, [], $this->entityFinderHelper, $onlyText),
ExtendEntityFinderEvent::class
);

return $event;
}
Expand Down
45 changes: 45 additions & 0 deletions src/EntityFinder/Element.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

namespace HeimrichHannot\UtilsBundle\EntityFinder;

class Element
{
private int $id;
private string $table;
private ?string $description;
private ?iterable $parents;

/**
* @param int $id
* @param string $table
* @param string|null $description
* @param \Traversable|null $parents A closure that returns an iterator of parent elements
*/
public function __construct(int $id, string $table, ?string $description = null, ?iterable $parents = null)
{
$this->id = $id;
$this->table = $table;
$this->description = $description;
$this->parents = $parents;
}

public function getId(): int
{
return $this->id;
}

public function getTable(): string
{
return $this->table;
}

public function getDescription(): ?string
{
return $this->description;
}

public function getParents(): ?iterable
{
return $this->parents;
}
}
55 changes: 50 additions & 5 deletions src/EntityFinder/EntityFinderHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

use Contao\ContentModel;
use Contao\Database;
use Contao\Model;
use Contao\Model\Collection;
use Contao\ModuleModel;
use Contao\StringUtil;
Expand All @@ -20,14 +21,16 @@

class EntityFinderHelper
{
/**
* @var Utils
*/
private $utils;
private Utils $utils;
private Connection $connection;

public function __construct(Utils $utils)
public function __construct(
Utils $utils,
Connection $connection
)
{
$this->utils = $utils;
$this->connection = $connection;
}

/**
Expand Down Expand Up @@ -106,4 +109,46 @@ public function findContentElementByInserttag(string $type, string $field, strin

return $result->fetchEach('id');
}

/**
* @param string $table
* @param int|string $idOrAlias
* @return object|Model|null
* @throws \Doctrine\DBAL\Exception
*/
public function fetchModelOrData(string $table, $idOrAlias): ?object
{
/** @var class-string<Model> $modelClass */
$modelClass = Model::getClassFromTable($table);

if (!$modelClass || !class_exists($modelClass)) {
if (!$this->connection->createSchemaManager()->tablesExist([$table])) {
return null;
}
if (is_string($idOrAlias)) {
$result = $this->connection->executeQuery("SELECT * FROM $table WHERE alias=?", [$idOrAlias]);
if ($result->rowCount() === 0) {
return null;
}
return (object) $result->fetchAssociative();
}
if (is_numeric($idOrAlias)) {
if ($idOrAlias != (int) $idOrAlias) {
return null;
}

$result = $this->connection->executeQuery("SELECT * FROM $table WHERE id=?", [(int)$idOrAlias]);
if ($result->rowCount() === 0) {
return null;
}
$element = (object) $result->fetchAssociative();
$element->getTable = function() use ($table) {
return $table;
};
return $element;
}
}

return $modelClass::findByIdOrAlias($idOrAlias);
}
}
Loading
Loading