Last active
December 9, 2019 21:04
-
-
Save abbadon1334/5aa4b9bfca3c48534afdd2e6c59c6028 to your computer and use it in GitHub Desktop.
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 | |
use atk4\core\AppScopeTrait; | |
use atk4\core\DIContainerTrait; | |
use atk4\core\FactoryTrait; | |
use atk4\core\InitializerTrait; | |
use atk4\core\TrackableTrait; | |
use atk4\ui\CRUD; | |
trait PluginTrait | |
{ | |
use CollectionTrait; | |
use FactoryTrait; | |
public $_pluginTrait = true; | |
protected $plugins = []; | |
public function addPlugin($seed, $defaults = []): Plugin | |
{ | |
$plugin = $this->factory($seed, $defaults); | |
$plugin_classname = get_class($plugin); | |
/** To avoid proliferation of method in trait i leave this validations here */ | |
/** Validation is done in TargetComponent, before add to it */ | |
// validate added plugin | |
if (!is_a($plugin, Plugin::class, true)) { | |
throw new \atk4\core\Exception(['Error adding plugin, Plugin must a subclass of ComponentPlugin']); | |
} | |
if (!in_array(__CLASS__, $plugin->getPluggableTargets())) { | |
throw new \atk4\core\Exception([__CLASS__ . ' is not in the list of target components to use ' . $plugin_classname]); | |
} | |
$missing_plugins = array_diff($this->plugins, $plugin->getRequiredPlugins()); | |
if (!empty($missing_plugins)) { | |
throw new \atk4\core\Exception([$plugin_classname . ' require plugins : ' . implode(', ', $missing_plugins)]); | |
} | |
$this->_addIntoCollection($plugin->alias ?? $plugin_classname, $plugin, 'plugins'); | |
$plugin->activate(); | |
return $plugin; | |
} | |
public function getPlugin(string $alias): ?Plugin | |
{ | |
return $this->_hasInCollection($alias, 'plugins'); | |
} | |
/** | |
* at the moment i think remove and deactivation can be revaluated | |
* | |
* deactivate needs a check of the the other plugins that required the plugin being deactivated | |
* remove is the same sh... | |
*/ | |
} | |
abstract class Plugin | |
{ | |
use AppScopeTrait; | |
use InitializerTrait { | |
init as _init; | |
} | |
use TrackableTrait; | |
use DIContainerTrait; | |
public $alias; | |
protected $required_plugins = []; | |
protected $pluggable_targets = []; | |
public function __construct($defaults = []) | |
{ | |
$this->setDefaults($defaults); | |
} | |
public function getPluggableTargets(): array | |
{ | |
return $this->pluggable_targets; | |
} | |
public function getRequiredPlugins(): array | |
{ | |
return $this->required_plugins; | |
} | |
abstract function activate(); | |
} | |
// possible implementation | |
class CRUDPluggable extends CRUD | |
{ | |
use PluginTrait; | |
} | |
$crud = new CRUDPluggable(); | |
class PluginSearch extends Plugin | |
{ | |
protected $required_plugins = [ | |
'plugin_menu' | |
]; | |
protected $pluggable_targets = [ | |
\atk4\ui\CRUD::class | |
]; | |
protected $fields = []; | |
public function activate() | |
{ | |
$this->owner->menu->addItem(['search', 'icon' => 'search'])->on('click', function ($g) { | |
/* | |
you have $this->app and $this->owner here, $this->owner is already validated. | |
*/ | |
}); | |
} | |
} | |
$crud->addPlugin([PluginSearch::class, 'fields' => ['name', 'title', 'phone']]); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment