Skip to content

Instantly share code, notes, and snippets.

@RobinRadic
Last active May 19, 2022 23:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RobinRadic/1c1e46622bf0602a79ba11c5af9f88e2 to your computer and use it in GitHub Desktop.
Save RobinRadic/1c1e46622bf0602a79ba11c5af9f88e2 to your computer and use it in GitHub Desktop.
plugin functions converter
<?php
namespace App\Converter;
use Anomaly\Streams\Platform\Addon\Addon;
use Anomaly\Streams\Platform\Addon\AddonCollection;
use Anomaly\Streams\Platform\StreamsPlugin;
use Illuminate\Contracts\Container\Container;
use Illuminate\Support\Str;
use Twig\TwigFunction;
class Converter
{
protected FunctionCollection $functions;
/**
* @param \Illuminate\Contracts\Container\Container $container
* @param \Anomaly\Streams\Platform\Addon\AddonCollection $addons
*/
public function __construct(
protected Container $container,
protected AddonCollection $addons
)
{
$this->functions = new FunctionCollection();
}
public function convert()
{
foreach ($this->addons as $addon) {
$this->processAddon($addon);
}
$this->processPlugin(StreamsPlugin::class);
return $this->functions;
}
protected function processFunction(TwigFunction $function)
{
$name = $function->getName();
if (Str::contains($name, '*')) {
$prefix = preg_replace('/[_\*]*$/', '', $name);
$this->functions->set($prefix, new ObjectFunctionCaller($function));
} else {
$this->functions->set($name, $function->getCallable());
}
}
protected function processAddon(Addon $addon)
{
$spclass = $addon->getServiceProvider();
if ( ! class_exists($spclass)) {
return;
}
/** @var \Anomaly\Streams\Platform\Addon\AddonServiceProvider $sp */
$sp = $this->container->make($spclass);
/** @var \Anomaly\Streams\Platform\Addon\Plugin\Plugin $plugin */
foreach ($sp->getPlugins() as $pluginClass) {
$this->processPlugin($pluginClass);
}
}
protected function processPlugin(string $pluginClass)
{
foreach ($this->getPluginFunctions($pluginClass) as $function) {
$this->processFunction($function);
}
}
protected function getPluginFunctions(string $pluginClass)
{
$plugin = $this->container->make($pluginClass);
return $plugin->getFunctions();
}
}
<?php
$converter = App::make(\App\Converter::class);
$functions = $converter->convert();
$functions->gravatar();
$functions->form->open();
<?php
namespace App\Converter;
use ArrayAccess;
use Illuminate\Contracts\Support\Arrayable;
/**
* @method string icon($type, $class = null)
* @property \Collective\Html\FormBuilder $form
*/
class FunctionCollection implements ArrayAccess, Arrayable
{
protected array $items = [];
public function set($name, $item)
{
$this->offsetSet($name, $item);
return $this;
}
public function getAll()
{
return $this->items;
}
public function setAll($items)
{
$this->items = $items;
return $this;
}
public function toArray()
{
return $this->items;
}
public function offsetExists(mixed $offset): bool
{
return isset($this->items[ $offset ]);
}
public function offsetGet(mixed $offset): mixed
{
return $this->items[ $offset ];
}
public function offsetSet(mixed $offset, mixed $value): void
{
$this->items[ $offset ] = $value;
}
public function offsetUnset(mixed $offset): void
{
unset($this->items[ $offset ]);
}
public function __get(string $name)
{
return $this->offsetget($name);
}
public function __set(string $name, $value): void
{
$this->offsetSet($name, $value);
}
public function __isset(string $name): bool
{
return $this->offsetExists($name);
}
public function __unset(string $name): void
{
$this->offsetUnset($name);
}
public function __call(string $name, array $arguments = [])
{
if(!$this->offsetExists($name)){
throw new \BadMethodCallException("$name does not exist");
}
$fn = $this->offsetGet($name);
return $fn(...$arguments);
}
}
<?php
namespace App\Converter;
use Twig\TwigFunction;
class ObjectFunctionCaller
{
public function __construct(protected TwigFunction $fn)
{
}
public function __call(string $name, array $arguments = [])
{
$callable = $this->fn->getCallable();
return call_user_func_array($callable, [$name, ...$arguments]);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment