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

127 lines
2.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
2019-03-22 11:13:21 +08:00
class Plugin
2016-10-17 12:20:55 +08:00
{
2019-12-08 23:58:44 +08:00
const README_FILES = [
'README.md',
'readme.md',
'README.MD',
];
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;
/**
* package.json of the package.
*
* @var array
*/
2019-08-13 23:06:28 +08:00
protected $manifest;
2016-10-24 22:32:07 +08:00
2016-10-17 12:20:55 +08:00
/**
* Whether the plugin is enabled.
*
* @var bool
*/
protected $enabled = false;
2019-08-13 23:06:28 +08:00
public function __construct(string $path, array $manifest)
2016-10-17 12:20:55 +08:00
{
$this->path = $path;
2019-08-13 23:06:28 +08:00
$this->manifest = $manifest;
2016-10-17 12:20:55 +08:00
}
2019-07-02 22:22:05 +08:00
public function __get(string $name)
2016-10-17 12:20:55 +08:00
{
2019-08-13 23:06:28 +08:00
return $this->getManifestAttr(Str::snake($name, '-'));
2016-10-17 12:20:55 +08:00
}
2019-08-13 23:06:28 +08:00
public function getManifest()
2016-10-17 12:20:55 +08:00
{
2019-08-13 23:06:28 +08:00
return $this->manifest;
2016-10-17 12:20:55 +08:00
}
2019-08-13 23:06:28 +08:00
public function getManifestAttr(string $name, $default = null)
2019-08-12 17:37:52 +08:00
{
2019-08-13 23:06:28 +08:00
return Arr::get($this->manifest, $name, $default);
2019-08-12 17:37:52 +08:00
}
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');
2019-08-13 23:06:28 +08:00
return "$baseUrl/{$this->name}/assets/$relativeUri?v=".$this->version;
2016-12-31 16:07:12 +08:00
}
2019-12-08 23:58:44 +08:00
public function getReadme()
{
return Arr::first(self::README_FILES, function ($filename) {
return file_exists($this->path.'/'.$filename);
});
}
public function hasConfig(): bool
{
return $this->hasConfigClass() || $this->hasConfigView();
}
public function hasConfigClass(): bool
{
return Arr::has($this->manifest, 'enchants.config');
}
public function getConfigClass()
{
$name = Arr::get($this->manifest, 'enchants.config');
return Str::start($name, $this->manifest['namespace'].'\\');
}
2019-08-13 23:06:28 +08:00
public function getViewPath(string $filename): string
2017-01-17 21:41:20 +08:00
{
return $this->path."/views/$filename";
}
public function getConfigView()
{
return $this->hasConfigView()
2019-08-13 23:06:28 +08:00
? view()->file($this->getViewPath(Arr::get($this->manifest, '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
{
2019-08-13 23:06:28 +08:00
$filename = Arr::get($this->manifest, 'config', 'config.blade.php');
2016-10-17 17:51:51 +08:00
2019-08-13 23:06:28 +08:00
return $filename && file_exists($this->getViewPath($filename));
2018-06-29 15:11:42 +08:00
}
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;
}
}