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

Make possible to check if an extension is present, directly in Twig #1844

Merged
merged 1 commit into from
Sep 14, 2020
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
4 changes: 2 additions & 2 deletions src/Command/ExtensionsListCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,14 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$rows = [];

foreach ($extensions as $extension) {
$rows[] = [$extension->getClass(), $extension->getName()];
$rows[] = [$extension->getComposerPackage()->getName(), $extension->getClass(), $extension->getName()];
}

$io = new SymfonyStyle($input, $output);

if (! empty($rows)) {
$io->text('Currently installed extensions:');
$io->table(['Class', 'Extension name'], $rows);
$io->table(['Package name', 'Class', 'Extension name'], $rows);
} else {
$io->caution('No installed extensions could be found');
}
Expand Down
61 changes: 61 additions & 0 deletions src/Twig/ExtensionExtension.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php

declare(strict_types=1);

namespace Bolt\Twig;

use Bolt\Extension\ExtensionRegistry;
use Tightenco\Collect\Support\Collection;
use Twig\Extension\AbstractExtension;
use Twig\TwigFunction;

/**
* Widget functionality Twig extension.
*/
class ExtensionExtension extends AbstractExtension
{
/** @var ExtensionRegistry */
private $registry;

public function __construct(ExtensionRegistry $registry)
{
$this->registry = $registry;
}

/**
* {@inheritdoc}
*/
public function getFunctions(): array
{
return [
new TwigFunction('extension_exists', [$this, 'extensionExists']),
new TwigFunction('extensions', [$this, 'getExtensions']),
];
}

public function getExtensions(): Collection
{
$extensions = $this->registry->getExtensions();

$rows = [];

foreach ($extensions as $extension) {
$rows[] = [
'package' => $extension->getComposerPackage()->getName(),
'class' => $extension->getClass(),
'name' => $extension->getName(),
];
}

return new Collection($rows);
}

public function extensionExists(string $name): bool
{
$extensions = $this->getExtensions();

return $extensions->where('package', $name)->isNotEmpty() ||
$extensions->where('class', $name)->isNotEmpty() ||
$extensions->where('name', $name)->isNotEmpty();
}
}