Last active
May 19, 2022 23:54
-
-
Save RobinRadic/1c1e46622bf0602a79ba11c5af9f88e2 to your computer and use it in GitHub Desktop.
plugin functions converter
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$converter = App::make(\App\Converter::class); | |
$functions = $converter->convert(); | |
$functions->gravatar(); | |
$functions->form->open(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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