Ai
1 Star 0 Fork 0

sintall.com/laravel-addons

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
Environment.php 3.63 KB
一键复制 编辑 原始数据 按行查看 历史
sintall.com 提交于 2017-12-07 15:20 +08:00 . dd
<?php
namespace Sinta\Laravel\Addons;
use UnexpectedValueException;
use Illuminate\Contracts\Foundation\Application as ContractApplication;
class Environment
{
protected $addons = null;
protected $spacePaths = [];
protected $app;
public function __construct(ContractApplication $app)
{
$this->app = $app;
$this->makeAddonsPaths();
}
private function makeAddonsPaths()
{
$addonsDirPath = $this->path();
$this->spacePaths[''] = $addonsDirPath;
foreach ($this->app['config']->get('addon.additional_paths', []) as $name => $path) {
$this->addSpace($name, $path);
}
}
public function addSpace($name, $path)
{
$this->spacePaths[$name] = $this->app->basePath().'/'.$path;
}
public function path($name = null)
{
if ($name !== null) {
return $this->path().'/'.$name;
} else {
return $this->app->basePath().'/'.$this->app['config']->get('addon.path', 'addons');
}
}
public function spacePath($space, $name = null)
{
$path = $space ? array_get($this->spacePaths, $space) : $this->path();
if ($path === null) {
throw new UnexpectedValueException("addon space '{$space}' is not found.");
}
if ($name !== null) {
return $path.'/'.$name;
} else {
return $path;
}
}
public function exists($name)
{
if ($this->existsOnSpace(null, $name)) {
return true;
}
foreach ($this->spacePaths as $space => $path) {
if ($this->existsOnSpace($space, $name)) {
return true;
}
}
return false;
}
public function existsOnSpace($space, $name)
{
return is_dir($this->spacePath($space, $name));
}
public function classToPath($relativeClassName)
{
return str_replace('\\', '/', $relativeClassName).'.php';
}
public function pathToClass($relativePath)
{
if (strpos($relativePath, '/') !== false) {
$relativePath = dirname($relativePath).'/'.basename($relativePath, '.php');
} else {
$relativePath = basename($relativePath, '.php');
}
return str_replace('/', '\\', $relativePath);
}
public function loadAddons()
{
$files = $this->app['files'];
$ignore_pattern = $this->app['config']->get('addon.ignore_pattern', '/^@/');
$name_pattern = $this->app['config']->get('addon.name_pattern', '/^(.+)$/');
$addons = [];
foreach ($this->spacePaths as $path) {
// make directory
if (!$files->exists($path)) {
$files->makeDirectory($path);
}
// load addons
foreach ($files->directories($path) as $dir) {
// test ignore pattern
if (preg_match($ignore_pattern, basename($dir))) {
continue;
}
// test name pattern
if (! preg_match($name_pattern, basename($dir), $matches)) {
continue;
}
$name = count($matches) >= 2 ? $matches[1] : $matches[0];
$addon = Addon::create($this->app, $name, $dir);
$addons[$addon->name()] = $addon;
}
}
return $addons;
}
public function addons()
{
if ($this->addons === null) {
$this->addons = $this->loadAddons();
}
return $this->addons;
}
public function addon($name)
{
return array_get($this->addons(), $name ?: '', null);
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/limary_admin/laravel-extension.git
git@gitee.com:limary_admin/laravel-extension.git
limary_admin
laravel-extension
laravel-addons
master

搜索帮助