2016-10-17 12:20:55 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Services;
|
|
|
|
|
|
|
|
use Illuminate\Support\Arr;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @property string $name
|
|
|
|
* @property string $description
|
2016-10-24 22:32:07 +08:00
|
|
|
* @property string $title
|
|
|
|
* @property array $author
|
2016-10-17 12:20:55 +08:00
|
|
|
*/
|
2019-03-22 11:13:21 +08:00
|
|
|
class Plugin
|
2016-10-17 12:20:55 +08:00
|
|
|
{
|
|
|
|
/**
|
2016-12-31 16:07:12 +08:00
|
|
|
* The full directory of this plugin.
|
2016-10-17 12:20:55 +08:00
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $path;
|
|
|
|
|
2016-12-31 16:07:12 +08:00
|
|
|
/**
|
|
|
|
* The directory name where the plugin installed.
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $dirname;
|
|
|
|
|
2016-10-17 12:20:55 +08:00
|
|
|
/**
|
|
|
|
* package.json of the package.
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected $packageInfo;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Whether the plugin is installed.
|
|
|
|
*
|
|
|
|
* @var bool
|
|
|
|
*/
|
|
|
|
protected $installed = true;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The installed version of the plugin.
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $version;
|
|
|
|
|
2016-10-24 22:32:07 +08:00
|
|
|
/**
|
|
|
|
* The namespace used by the plugin.
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $namespace;
|
|
|
|
|
2016-10-17 12:20:55 +08:00
|
|
|
/**
|
|
|
|
* Whether the plugin is enabled.
|
|
|
|
*
|
|
|
|
* @var bool
|
|
|
|
*/
|
|
|
|
protected $enabled = false;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param $path
|
|
|
|
* @param array $packageInfo
|
|
|
|
*/
|
|
|
|
public function __construct($path, $packageInfo)
|
|
|
|
{
|
|
|
|
$this->path = $path;
|
|
|
|
$this->packageInfo = $packageInfo;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
|
|
|
public function __get($name)
|
|
|
|
{
|
2017-01-18 22:57:15 +08:00
|
|
|
return $this->packageInfoAttribute(snake_case($name, '-'));
|
2016-10-17 12:20:55 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
|
|
|
public function __isset($name)
|
|
|
|
{
|
2017-01-18 22:57:15 +08:00
|
|
|
return isset($this->{$name}) || $this->packageInfoAttribute(snake_case($name, '-'));
|
2016-10-17 12:20:55 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
public function packageInfoAttribute($name)
|
|
|
|
{
|
|
|
|
return Arr::get($this->packageInfo, $name);
|
|
|
|
}
|
|
|
|
|
2017-01-08 16:05:54 +08:00
|
|
|
public function assets($relativeUri)
|
|
|
|
{
|
2019-03-21 19:45:52 +08:00
|
|
|
$baseUrl = config('plugins.url') ?: url('plugins');
|
2018-06-28 12:07:20 +08:00
|
|
|
|
2019-03-21 19:45:52 +08:00
|
|
|
return "$baseUrl/{$this->getDirname()}/assets/$relativeUri";
|
2017-01-08 16:05:54 +08:00
|
|
|
}
|
|
|
|
|
2016-10-17 12:20:55 +08:00
|
|
|
/**
|
|
|
|
* @param bool $installed
|
|
|
|
* @return Plugin
|
|
|
|
*/
|
|
|
|
public function setInstalled($installed)
|
|
|
|
{
|
|
|
|
$this->installed = $installed;
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2016-12-31 16:07:12 +08:00
|
|
|
public function getDirname()
|
|
|
|
{
|
|
|
|
return $this->dirname;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function setDirname($dirname)
|
|
|
|
{
|
|
|
|
$this->dirname = $dirname;
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2016-10-24 22:32:07 +08:00
|
|
|
public function getNameSpace()
|
|
|
|
{
|
|
|
|
return $this->namespace;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function setNameSpace($namespace)
|
|
|
|
{
|
|
|
|
$this->namespace = $namespace;
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getViewPath($name)
|
|
|
|
{
|
2017-01-17 21:41:20 +08:00
|
|
|
return $this->getViewPathByFileName("$name.tpl");
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getViewPathByFileName($filename)
|
|
|
|
{
|
|
|
|
return $this->path."/views/$filename";
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getConfigView()
|
|
|
|
{
|
|
|
|
return $this->hasConfigView() ? view()->file($this->getViewPathByFileName(Arr::get($this->packageInfo, 'config'))) : null;
|
2016-10-17 17:51:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
public function hasConfigView()
|
|
|
|
{
|
2017-01-17 21:41:20 +08:00
|
|
|
$filename = Arr::get($this->packageInfo, 'config');
|
|
|
|
|
|
|
|
return $filename && file_exists($this->getViewPathByFileName($filename));
|
2016-10-17 17:51:51 +08:00
|
|
|
}
|
|
|
|
|
2016-10-17 12:20:55 +08:00
|
|
|
/**
|
|
|
|
* @param string $version
|
|
|
|
* @return Plugin
|
|
|
|
*/
|
|
|
|
public function setVersion($version)
|
|
|
|
{
|
|
|
|
$this->version = $version;
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getVersion()
|
|
|
|
{
|
|
|
|
return $this->version;
|
|
|
|
}
|
|
|
|
|
2018-06-29 15:11:42 +08:00
|
|
|
/**
|
|
|
|
* @param array $require
|
|
|
|
* @return Plugin
|
|
|
|
*/
|
|
|
|
public function setRequirements($require)
|
|
|
|
{
|
|
|
|
$this->require = $require;
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function getRequirements()
|
|
|
|
{
|
|
|
|
return (array) $this->require;
|
|
|
|
}
|
|
|
|
|
2016-10-17 12:20:55 +08:00
|
|
|
/**
|
|
|
|
* @param bool $enabled
|
|
|
|
* @return Plugin
|
|
|
|
*/
|
|
|
|
public function setEnabled($enabled)
|
|
|
|
{
|
|
|
|
$this->enabled = $enabled;
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function isEnabled()
|
|
|
|
{
|
|
|
|
return $this->enabled;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getPath()
|
|
|
|
{
|
|
|
|
return $this->path;
|
|
|
|
}
|
|
|
|
}
|