Skip to content

Commit

Permalink
Merge pull request #1844 from bolt/check-for-extensions-in-twig
Browse files Browse the repository at this point in the history
Make possible to check if an extension is present, directly in Twig
  • Loading branch information
bobdenotter authored Sep 14, 2020
2 parents 9dff59f + 506d747 commit cd740ca
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 2 deletions.
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();
}
}

0 comments on commit cd740ca

Please sign in to comment.