axy/syspaths
Composer 安装命令:
composer require axy/syspaths
包简介
Specifying paths within the system
关键字:
README 文档
README
Specifying paths within the system.
- The library does not require any dependencies (except composer packages).
- Tested on PHP 5.4+, PHP 7, HHVM (on Linux), PHP 5.5 (on Windows).
- Install:
composer require axy/syspaths. - License: MIT.
Documentation
The library allows to store of the directories structure within a certain system. Inside an application for example.
/* 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
Define paths in one place makes it easy to change the structure.
API
The library provides a single class axy\syspaths\Paths.
The constructor:
Paths::__construct(string $root [, array $patterns]);
The only required arguments is $root - the root directory of the system.
$patterns
$patterns specifies a list of sub-paths.
It can be specified via the constructor (as in the example above).
Or by overriding:
/** * @property-read string $www * @property-read string $templates */ class MyPaths extends Paths { protected $patterns = [ 'htdocs' => 'www', 'templates' => 'templates', ]; }
Now available autocomplete in IDE.
Or you can define the property $patterns and also specify the argument of the constructor.
In this case these two list recursively merged.
For example, define a system class and redefine some paths for tests.
Access to paths
Via magic methods.
$paths->templates; // /www/example.loc/templates $paths->root; // /www/example.loc isset($paths->htdocs); // TRUE $paths->htdocs = 'new-www'; // Exception: Paths is read-only
Patterns
[
'www' => 'www',
'images' => ':www:/i',
'icons' => ':images:/icons',
'logo' => ':icons:/logo.gif',
'tmp' => '/tmp',
]
If a path pattern begins with a colon then it is a link to the other path. Such links are processed recursively (circular references are not tracked):
$paths->icons; // /www/example.loc/www/i/icons/logo.gif
If a path pattern begins with / then it is an absolute path:
$paths->tmp; // /tmp
Other patterns is relative to the root.
www is equivalent to :root:/www.
Nested paths
[
'htdocs' => 'www',
'templates' => [
'root' => 'templates',
'layouts' => 'layouts',
'admin' => ':layouts:/admin',
'profile' => ':admin:/profile.twig',
],
]
An array defines a nested object Paths.
$paths->templates->profile; // /www/example.loc/templates/layouts/admin/profile.twig
For Paths-objects defined __toString():
$paths->templates.'/mails'; // /www/example.loc/templates/mails
The nested array must contains the field root.
It may be link: 'root' => ':htdocs:/templates'.
The nested array may contains the field __classname for the class of the nested path.
By defaults it is 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', ]; }
Now we have the full auto-complete: $app->paths->templates->profile.
Links can consist of several components:
[
'templates' => [
'root' => 'templates',
'layouts' => 'layouts',
'admin' => ':layouts:/admin',
],
'profileTemplate' => ':templates.admin:/profile.twig',
]
Creating paths
$paths->templates->layouts.'/tpl.twig';
Or via the method create (or __invoke):
$paths->create(':templates.layouts:/tpl.twig'); $paths(':templates.layouts:/tpl.twig');
Paths::create($pattern, $real = false);
If specified the second argument $real executed realpath() for the result.
If the path is not exists then returned NULL.
Exceptions
In the namespace axy\syspaths\errors.
RequirePatterns-$patternsnot defined nor in the property, nor in the constructor.PatternNotFound- where access via__getor in a link form other pattern.InvalidPattern- not closed link, a nested array is not containsroot,__classnamein not exists and etc.
axy/syspaths 适用场景与选型建议
axy/syspaths 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 43 次下载、GitHub Stars 达 0, 最近一次更新时间为 2015 年 10 月 26 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「path」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 axy/syspaths 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 axy/syspaths 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 axy/syspaths 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
JSONPath implementation for querying and updating JSON objects with support for json flattening into a table
The InstantConfigurationCopy module provides easy way to copy configuration field information for admin in back office Magento 2.
simple agnostic package for IO releted (files, dir, etc..)
Collections of Path generators for Spatie laravel-medialibrary pacakge
Cross platform path normalization and comparison helpers for PHP 8.4+.
Path manipulation library
统计信息
- 总下载量: 43
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 13
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2015-10-26