blessing-skin-server/app/Services/Plugin.php

186 lines
3.6 KiB
PHP
Raw Normal View History

2016-10-17 12:20:55 +08:00
<?php
2019-07-02 22:22:05 +08:00
declare(strict_types=1);
2016-10-17 12:20:55 +08:00
namespace App\Services;
use Illuminate\Support\Arr;
2019-07-02 22:22:05 +08:00
use Illuminate\Support\Str;
2016-10-17 12:20:55 +08:00
/**
* @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;
2019-07-02 22:22:05 +08:00
public function __construct(string $path, array $packageInfo)
2016-10-17 12:20:55 +08:00
{
$this->path = $path;
$this->packageInfo = $packageInfo;
}
2019-07-02 22:22:05 +08:00
public function __get(string $name)
2016-10-17 12:20:55 +08:00
{
2019-07-02 22:22:05 +08:00
return $this->packageInfoAttribute(Str::snake($name, '-'));
2016-10-17 12:20:55 +08:00
}
2019-07-02 22:22:05 +08:00
public function __isset(string $name)
2016-10-17 12:20:55 +08:00
{
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
}
2019-07-02 22:22:05 +08:00
public function packageInfoAttribute(string $name)
2016-10-17 12:20:55 +08:00
{
return Arr::get($this->packageInfo, $name);
}
2019-07-02 22:22:05 +08:00
public function assets(string $relativeUri): string
2017-01-08 16:05:54 +08:00
{
2019-03-21 19:45:52 +08:00
$baseUrl = config('plugins.url') ?: url('plugins');
return "$baseUrl/{$this->getDirname()}/assets/$relativeUri?v=".$this->version;
2017-01-08 16:05:54 +08:00
}
2019-07-02 22:22:05 +08:00
public function setInstalled(bool $installed): self
2016-10-17 12:20:55 +08:00
{
$this->installed = $installed;
return $this;
}
2019-07-02 22:22:05 +08:00
public function getDirname(): string
2016-12-31 16:07:12 +08:00
{
return $this->dirname;
}
2019-07-02 22:22:05 +08:00
public function setDirname(string $dirname): self
2016-12-31 16:07:12 +08:00
{
$this->dirname = $dirname;
return $this;
}
2019-08-12 14:35:36 +08:00
public function getNamespace(): string
2016-10-24 22:32:07 +08:00
{
return $this->namespace;
}
2019-07-02 22:22:05 +08:00
public function setNameSpace(string $namespace): self
2016-10-24 22:32:07 +08:00
{
$this->namespace = $namespace;
return $this;
}
2019-07-02 22:22:05 +08:00
public function getViewPathByFileName(string $filename): string
2017-01-17 21:41:20 +08:00
{
return $this->path."/views/$filename";
}
public function getConfigView()
{
return $this->hasConfigView()
? view()->file($this->getViewPathByFileName(Arr::get($this->packageInfo, 'config', 'config.blade.php')))
: null;
2016-10-17 17:51:51 +08:00
}
2019-07-02 22:22:05 +08:00
public function hasConfigView(): bool
2016-10-17 17:51:51 +08:00
{
$filename = Arr::get($this->packageInfo, 'config', 'config.blade.php');
2017-01-17 21:41:20 +08:00
return $filename && file_exists($this->getViewPathByFileName($filename));
2016-10-17 17:51:51 +08:00
}
2019-07-02 22:22:05 +08:00
public function setVersion(string $version): self
2016-10-17 12:20:55 +08:00
{
$this->version = $version;
return $this;
}
2019-07-02 22:22:05 +08:00
public function getVersion(): string
2016-10-17 12:20:55 +08:00
{
return $this->version;
}
2019-07-02 22:22:05 +08:00
public function setRequirements(array $require): self
2018-06-29 15:11:42 +08:00
{
$this->require = $require;
return $this;
}
2019-07-02 22:22:05 +08:00
public function getRequirements(): array
2018-06-29 15:11:42 +08:00
{
return (array) $this->require;
}
2019-07-02 22:22:05 +08:00
public function setEnabled(bool $enabled): self
2016-10-17 12:20:55 +08:00
{
$this->enabled = $enabled;
return $this;
}
2019-07-02 22:22:05 +08:00
public function isEnabled(): bool
2016-10-17 12:20:55 +08:00
{
return $this->enabled;
}
2019-07-02 22:22:05 +08:00
public function getPath(): string
2016-10-17 12:20:55 +08:00
{
return $this->path;
}
}