pablo-sg-pacheco/wp-namespace-autoloader
Composer 安装命令:
composer require pablo-sg-pacheco/wp-namespace-autoloader
包简介
A PHP autoloader class that follows the WordPress coding standards 2.0 and proposed 3.0 for class/interface/trait names and filenames
关键字:
README 文档
README
A PHP autoloader class that follows the WordPress coding standards 2.0 applying PSR-4 specification and optionally supports proposed 3.0
Description
Namespaces and autoloaders are cool and help organizing your code. With these features you don't have to worry about including and requiring php files manually ever again and your code gets organized in folders.
This is a PSR-4 autoloader implementation following WordPress naming conventions 2.0 and proposed WordPress naming conventions 3.0
It means I'm doing these things:
- Converting classes filenames to lowercase
- Replacing underscores on class filenames by hyphens
- Prepending 'class-' before the final class name
- [Optional]: Prepending 'interface-' before the final interface name
- [Optional]: Prepending 'trait-' before the final trait name
Note
- Required PHP Version is PHP 5.4
Installation
You just have to require it just like a composer default dependency. You may have to use preferred-install as dist so you will be able to commit files as .git files will not be created
"require": { "pablo-sg-pacheco/wp-namespace-autoloader": "dev-master" }, "config": { "preferred-install": "dist" }
Usage
Firstly, load the composer dependencies like you are used to
<?php require __DIR__ . '/vendor/autoload.php';
Now you have to initialize it and you are good to go
<?php use Pablo_Pacheco\WP_Namespace_Autoloader\WP_Namespace_Autoloader; $autoloader = new WP_Namespace_Autoloader( array( 'directory' => __DIR__, // Directory of your project. It can be your theme or plugin. Defaults to __DIR__ (probably your best bet). 'namespace_prefix' => 'My_Project', // Main namespace of your project. E.g My_Project\Admin\Tests should be My_Project. Defaults to the namespace of the instantiating file. 'classes_dir' => 'src', // (optional). It is where your namespaced classes are located inside your project. If your classes are in the root level, leave this empty. If they are located on 'src' folder, write 'src' here 'prepend_class' => true, // (optional). Default true, prepends class- before the final class name 'prepend_interface' => true, // (optional). Default false, prepends interface- before the final interface name 'prepend_trait' => true, // (optional). Default false, prepends trait- before the final trait name ) ); $autoloader->init();
Now comes the cool part! If you have a simple class located on your_projct_root_folder\Admin_Pages\class-main-page.php like this, you can instantiate it and it's going to work
<?php namespace My_Project\Admin_Pages; class Main_Page{ }
Or if you have a simple interface located on your_projct_root_folder\Admin_Pages\interface-init.php
<?php namespace My_Project\Admin_Pages; interface Init { }
And you have a simple class implementing that interface located on your_projct_root_folder\Admin_Pages\class-main-page.php you can instantiate it and it's going to work
<?php namespace My_Project\Admin_Pages; class Main_Page implements Init { }
Or if you have a simple trait located on your_projct_root_folder\Admin_Pages\trait-my-trait.php
<?php namespace My_Project\Admin_Pages; trait My_Trait { }
And you have a simple class using that trait located on your_projct_root_folder\Admin_Pages\class-main-page.php you can instantiate it and it's going to work
<?php namespace My_Project\Admin_Pages; class Main_Page { use My_Trait }
Parameters
| Parameter | Default value | Description |
|---|---|---|
| directory | null |
Path of your project. Probably use __DIR__ here |
| namespace_prefix | null |
Namespace prefix of your project |
| classes_dir | array( '.', 'vendor' ) |
Relative path of the directory containing all your classes. Accepts string or array of strings. Defaults to directory parameter and the vendor subdirectory. (optional). |
| lowercase | array('file') |
If you want to lowercase just the file or folders too. It accepts an array with two possible values: 'file', 'folders'. |
| underscore_to_hyphen | array('file') |
If you want to convert underscores to hyphens. It accepts an array with two possible values: 'file', 'folders'. |
| prepend_class | true |
If you want to prepend 'class-' before files |
| prepend_interface | false |
If you want to prepend 'interface-' before files |
| prepend_trait | false |
If you want to prepend 'trait-' before files |
This class has parameters that make it flexible enough to fit any kind of project.
Examples
- Lowercases all folders using
lowercase => array('file','folders') - Converts underscores to hyphens on folders too with
underscore_to_hyphen => array('file','folders') - Doesn't prepend class before file with
prepend_class => false
pablo-sg-pacheco/wp-namespace-autoloader 适用场景与选型建议
pablo-sg-pacheco/wp-namespace-autoloader 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 34.44k 次下载、GitHub Stars 达 42, 最近一次更新时间为 2017 年 05 月 05 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「autoload」 「wordpress」 「namespace」 「wp」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 pablo-sg-pacheco/wp-namespace-autoloader 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 pablo-sg-pacheco/wp-namespace-autoloader 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 pablo-sg-pacheco/wp-namespace-autoloader 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Environment processor and contexts autoloader
Simple autoloader that will autoload classes or traits with namespace prefix
MvcCore - Extension - Debug - Tracy - Panel Session - render and add into tracy debug panel $_SESSION content and \MvcCore\Session namespaces content.
Flexible storage solution for end-user data
Extension to autoload modules dynamically
Monolog cascade extension allows to configure loggers for a specific namespace/class (log4j style)
统计信息
- 总下载量: 34.44k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 42
- 点击次数: 18
- 依赖项目数: 2
- 推荐数: 0
其他信息
- 授权协议: GPLv2
- 更新时间: 2017-05-05