winter/modules/system/classes/PluginBase.php

247 lines
6.2 KiB
PHP
Raw Normal View History

2014-05-14 23:24:20 +10:00
<?php namespace System\Classes;
use Illuminate\Support\ServiceProvider as ServiceProviderBase;
use ReflectionClass;
use SystemException;
use Yaml;
use Backend;
2014-05-14 23:24:20 +10:00
/**
* Plugin base class
*
* @package october\system
* @author Alexey Bobkov, Samuel Georges
*/
class PluginBase extends ServiceProviderBase
2014-05-14 23:24:20 +10:00
{
/**
* @var boolean
*/
protected $loadedYamlConfigration = false;
/**
* @var array Plugin dependencies
*/
public $require = [];
/**
* @var boolean Determine if this plugin should have elevated privileges.
*/
public $elevated = false;
/**
* @var boolean Determine if this plugin should be loaded (false) or not (true).
*/
public $disabled = false;
2014-05-14 23:24:20 +10:00
/**
* Returns information about this plugin, including plugin name and developer name.
*
* @return array
* @throws SystemException
2014-05-14 23:24:20 +10:00
*/
public function pluginDetails()
{
$thisClass = get_class($this);
$configuration = $this->getConfigurationFromYaml(sprintf('Plugin configuration file plugin.yaml is not '.
'found for the plugin class %s. Create the file or override pluginDetails() '.
'method in the plugin class.', $thisClass));
if (!array_key_exists('plugin', $configuration)) {
throw new SystemException('The plugin configuration file plugin.yaml should contain the "plugin" section: %s.', $thisClass);
}
return $configuration['plugin'];
}
2014-05-14 23:24:20 +10:00
/**
* Register method, called when the plugin is first registered.
*
* @return void
2014-05-14 23:24:20 +10:00
*/
2014-10-18 11:58:50 +02:00
public function register()
{
}
2014-05-14 23:24:20 +10:00
/**
* Boot method, called right before the request route.
*
* @return array
2014-05-14 23:24:20 +10:00
*/
2014-10-18 11:58:50 +02:00
public function boot()
{
}
2014-05-14 23:24:20 +10:00
/**
* Registers CMS markup tags introduced by this plugin.
*
* @return array
2014-05-14 23:24:20 +10:00
*/
public function registerMarkupTags()
{
return [];
}
/**
* Registers any front-end components implemented in this plugin.
*
* @return array
2014-05-14 23:24:20 +10:00
*/
public function registerComponents()
{
return [];
}
/**
* Registers back-end navigation items for this plugin.
*
* @return array
2014-05-14 23:24:20 +10:00
*/
public function registerNavigation()
{
$configuration = $this->getConfigurationFromYaml();
if (array_key_exists('navigation', $configuration)) {
$navigation = $configuration['navigation'];
if (is_array($navigation)) {
array_walk_recursive($navigation, function(&$item, $key){
if ($key === 'url') {
$item = Backend::url($item);
}
});
}
return $navigation;
}
2014-05-14 23:24:20 +10:00
}
/**
* Registers any back-end permissions used by this plugin.
*
* @return array
2014-05-14 23:24:20 +10:00
*/
public function registerPermissions()
{
$configuration = $this->getConfigurationFromYaml();
if (array_key_exists('permissions', $configuration)) {
return $configuration['permissions'];
}
2014-05-14 23:24:20 +10:00
}
/**
* Registers any back-end configuration links used by this plugin.
*
* @return array
2014-05-14 23:24:20 +10:00
*/
public function registerSettings()
{
return [];
}
/**
* Registers scheduled tasks that are executed on a regular basis.
*
* @param string $schedule
* @return void
*/
public function registerSchedule($schedule)
{
}
2014-05-14 23:24:20 +10:00
/**
* Registers any report widgets provided by this plugin.
* The widgets must be returned in the following format:
2014-06-16 08:48:26 +10:00
* [
* 'className1'=>[
2015-06-24 09:14:44 -03:00
* 'label' => 'My widget 1',
2014-06-16 08:48:26 +10:00
* 'context' => ['context-1', 'context-2'],
* ],
2014-06-16 08:48:26 +10:00
* 'className2' => [
2015-06-24 09:14:44 -03:00
* 'label' => 'My widget 2',
2014-06-16 08:48:26 +10:00
* 'context' => 'context-1'
2014-05-14 23:24:20 +10:00
* ]
* ]
* @return array
2014-05-14 23:24:20 +10:00
*/
public function registerReportWidgets()
{
return [];
}
/**
* Registers any form widgets implemented in this plugin.
* The widgets must be returned in the following format:
* ['className1' => 'alias'],
* ['className2' => 'anotherAlias']
* @return array
2014-05-14 23:24:20 +10:00
*/
public function registerFormWidgets()
{
return [];
}
/**
* Registers any mail templates implemented by this plugin.
* The templates must be returned in the following format:
* ['acme.blog::mail.welcome' => 'This is a description of the welcome template'],
* ['acme.blog::mail.forgot_password' => 'This is a description of the forgot password template'],
* @return array
*/
public function registerMailTemplates()
{
return [];
}
2014-05-14 23:24:20 +10:00
/**
* Registers a new console (artisan) command
*
* @param string $key The command name
* @param string $class The command class
2014-05-14 23:24:20 +10:00
* @return void
*/
public function registerConsoleCommand($key, $class)
{
$key = 'command.'.$key;
2014-10-18 11:58:50 +02:00
$this->app[$key] = $this->app->share(function ($app) use ($class) {
2014-05-14 23:24:20 +10:00
return new $class;
});
$this->commands($key);
}
/**
* Read configuration from YAML file
*
* @param string|null $exceptionMessage
* @return array|bool
* @throws SystemException
*/
protected function getConfigurationFromYaml($exceptionMessage = null)
{
if ($this->loadedYamlConfigration !== false) {
return $this->loadedYamlConfigration;
}
$reflection = new ReflectionClass(get_class($this));
$yamlFilePath = dirname($reflection->getFileName()).'/plugin.yaml';
if (!file_exists($yamlFilePath)) {
if ($exceptionMessage) {
throw new SystemException($exceptionMessage);
}
else {
$this->loadedYamlConfigration = [];
}
}
else {
$this->loadedYamlConfigration = Yaml::parse(file_get_contents($yamlFilePath));
if (!is_array($this->loadedYamlConfigration)) {
throw new SystemException('Invalid format of the plugin configuration file: %s. The file should define an array.', $yamlFilePath);
}
}
return $this->loadedYamlConfigration;
}
2014-10-18 11:58:50 +02:00
}