axy / syspaths
指定系统内的路径
0.1.1
2015-11-10 07:17 UTC
Requires
- php: >=5.4.0
- axy/errors: ~1.0.2
This package is auto-updated.
Last update: 2024-09-08 02:45:59 UTC
README
指定系统内的路径。
- 该库不依赖任何其他依赖(除了composer包)。
- 在PHP 5.4+、PHP 7、HHVM(Linux上)、PHP 5.5(Windows上)上进行了测试。
- 安装:
composer require axy/syspaths
。 - 许可证:MIT。
文档
该库允许存储特定系统内的目录结构。例如,在一个应用程序内部。
/* App init. __DIR__ is /www/example.loc */ use axy\syspaths\Paths; $paths = new Paths(__DIR__, [ 'htdocs' => 'www', 'source' => 'src', 'templates' => 'templates', ]); /* Further */ $tplFN = $app->paths->templates.'/default.twig'; // /www/example.loc/templates/default.twig
在单个地方定义路径可以轻松地更改结构。
API
该库提供了一个类 axy\syspaths\Paths
。
构造函数
Paths::__construct(string $root [, array $patterns]);
唯一必需的参数是 $root
- 系统的根目录。
$patterns
$patterns
指定子路径的列表。可以通过构造函数指定(如上例所示)。或者通过重写
/** * @property-read string $www * @property-read string $templates */ class MyPaths extends Paths { protected $patterns = [ 'htdocs' => 'www', 'templates' => 'templates', ]; }
现在在IDE中可用自动完成。
或者您可以定义属性 $patterns
,并指定构造函数的参数。在这种情况下,这两个列表递归合并。例如,定义一个系统类并重新定义一些测试路径。
访问路径
通过魔术方法。
$paths->templates; // /www/example.loc/templates $paths->root; // /www/example.loc isset($paths->htdocs); // TRUE $paths->htdocs = 'new-www'; // Exception: Paths is read-only
模式
[ 'www' => 'www', 'images' => ':www:/i', 'icons' => ':images:/icons', 'logo' => ':icons:/logo.gif', 'tmp' => '/tmp', ]
如果路径模式以冒号开头,则它是指向其他路径的链接。此类链接递归处理(不跟踪循环引用)
$paths->icons; // /www/example.loc/www/i/icons/logo.gif
如果路径模式以 /
开头,则它是绝对路径
$paths->tmp; // /tmp
其他模式相对于根。 www
等同于 :root:/www
。
嵌套路径
[ 'htdocs' => 'www', 'templates' => [ 'root' => 'templates', 'layouts' => 'layouts', 'admin' => ':layouts:/admin', 'profile' => ':admin:/profile.twig', ], ]
数组定义嵌套的Paths对象。
$paths->templates->profile; // /www/example.loc/templates/layouts/admin/profile.twig
对于定义的Paths对象 __toString()
$paths->templates.'/mails'; // /www/example.loc/templates/mails
嵌套数组必须包含 root
字段。它可以是一个链接: 'root' => ':htdocs:/templates'
。
嵌套数组可以包含 __classname
字段,用于嵌套路径的类。默认情况下是 axy\syspaths\Paths
。
/** * @property-read string $htdocs * @property-read TemplatesPaths $templates */ class MyPaths extends Paths { $patterns = [ 'htdocs' => 'www', 'templates' => [ 'root' => 'templates', '__classname' => 'TemplatesPaths', ], ] } /** * @property-read string $layout * @property-read string $admin * @property-read string $profile */ class TemplatesPaths extends Paths { $patterns = [ 'layouts' => 'layouts', 'admin' => ':layouts:/admin', 'profile' => ':admin:/profile.twig', ]; }
现在我们有完整的自动完成: $app->paths->templates->profile
。
链接可以由多个组件组成
[ 'templates' => [ 'root' => 'templates', 'layouts' => 'layouts', 'admin' => ':layouts:/admin', ], 'profileTemplate' => ':templates.admin:/profile.twig', ]
创建路径
$paths->templates->layouts.'/tpl.twig';
或者通过方法 create
(或 __invoke
)
$paths->create(':templates.layouts:/tpl.twig'); $paths(':templates.layouts:/tpl.twig');
Paths::create($pattern, $real = false);
如果指定了第二个参数 $real
,则对结果执行 realpath()
。如果路径不存在,则返回 NULL
。
异常
在命名空间 axy\syspaths\errors
中。
RequirePatterns
-$patterns
未在属性中定义,也未在构造函数中定义。PatternNotFound
- 通过__get
访问或以其他模式形式链接时。InvalidPattern
- 链接未关闭,嵌套数组不包含root
,__classname
不存在等。