codebjorn / mjolnir
WordPress 工具框架
Requires
- php: >=7.1
- eftec/bladeone: ^3.52
- league/container: ^3.3
Requires (Dev)
- phpunit/phpunit: ^7.0
README
Mjölnir, WordPress 工具框架
Mjölnir 是一个小巧的工具框架,它提供了使用 PHP 最好的工具和可能性来创建 WordPress 插件和主题。此包符合 PSR-1、PSR-2、PSR-4 和 PSR-11。
如果您认为这种方法不起作用,请提出问题,让我们讨论一下:)
已实现的方案
要查看已实现的方案,请检查官方模板
预要求
在继续之前
-
我建议您阅读PHP League Container的文档。
Mjölnir 使用它来为您的应用程序提供依赖注入。 -
此外,如果您将使用默认模板引擎,请查看BladeOne的文档
要求
此框架的要求如下:
- PHP 7.1+
- Composer
安装
您可以通过 composer 安装框架
composer require codebjorn/mjolnir
将 stubs
文件夹中的存根复制到您的文件夹中,如果需要,重命名 app
文件夹,并更新 {{namespace}}
为应用程序命名空间
简而言之
框架的基座是依赖注入容器,它存储了一些工具类,用于:配置、渲染、异常处理、钩子、块以及您添加和解析的所有其他服务,之后将您的服务添加到钩子中。
例如,您有一个添加一些 WP 功能的类 PostTypes.php
,在 ServiceProvider
中解析所有依赖关系,然后将其应用于钩子
工具
为了解释工具是如何工作的,让我们看看每个具有工具的命名空间
Admin
Admin 命名空间包含用于在管理端添加新功能的工具
Option.php
是 Option Api 相关函数的包装器,示例
\Mjolnir\Admin\Option::get('someOption'); \Mjolnir\Admin\Option::update('someOption', 'someContent');
Page.php
是创建管理页面的所有函数的包装器,示例
\Mjolnir\Admin\Page::dashboard('Page', 'Page', 'read', 'page'); \Mjolnir\Admin\Page::management('Page', 'Page', 'read', 'page');
Content
Content 命名空间包含用于创建 WordPress 拥有的不同内容解决方案的工具
PostType.php
允许您轻松创建不同的自定义文章类型,示例
$books = \Mjolnir\Content\PostType::make('Books') ->supports(['title', 'editor']) ->public(true) ->showInRest(true); $books->register(); //Call register to exit creating of arguments
Taxonomy.php
允许您轻松创建不同的分类法,示例
$countries = \Mjolnir\Content\Taxonomy::make('Countries') ->postTypes(['books']) ->public(true) ->showInRest(true); $countries->register(); //Call register to exit creating of arguments
Shortcode.php
是与 Shortcode Api 相关函数的包装器,示例
\Mjolnir\Content\Shortcode::add('hello', [$this, 'hello']); \Mjolnir\Content\Shortcode::do('[hello]');
Database
Database 命名空间包含数据库交互的工具
Query.php
是 WP Query 参数的包装器
$metaQuery = new \Mjolnir\Database\Parameter\Meta([ new \Mjolnir\Database\Parameter\MetaArgument('key', 'value', 'NUMERIC', '='), new \Mjolnir\Database\Parameter\MetaArgument('key', 'value', 'DATE', '>'), ]); $tax = new \Mjolnir\Database\Parameter\Tax(null, [ new \Mjolnir\Database\Parameter\TaxArgument('taxonomy', 'field', 'term1'), new \Mjolnir\Database\Parameter\TaxArgument('taxonomy', 'field', 'term2'), ]); $posts = new \Mjolnir\Database\Query(); $posts->postType('posts') ->meta($metaQuery) ->tax($tax) ->pagination(5); $posts->make(); // return new WP_Query $posts->get(); // returns Collection $posts->getRaw(); //return Array
Routing
Routing 命名空间包含与 WordPress 路由系统(如 API)相关的工具
Api.php
是与所有与 REST API 相关的函数的包装器,示例
\Mjolnir\Routing\Api::make('/namespace', '/users') ->get([$this, 'getUsers'], '_return_true') ->post([$this, 'postUsers'], [$this, 'isAdmin']);
Support
Support 命名空间包含帮助您处理数据的工具
Arr.php
是允许您操作数组的工具类,示例
$first = \Mjolnir\Support\Arr::first($array); $key = \Mjolnir\Support\Arr::get($array, 'key');
Collection.php
是用于处理数据数组的类,示例
$collection = \Mjolnir\Support\Collection::make($array); $filtered = $collection->where('key', 'value'); $reversed = $filtered->reverse();
Is.php
是用于确定变量类型的函数的包装器,示例
\Mjolnir\Support\Is::file($value); \Mjolnir\Support\Is::str($value); \Mjolnir\Support\Is::int($value);
Str.php
是用于操作字符串的类,示例
$string = \Mjolnir\Support\Str::make('some string'); $reversed = $string->flip(); $contains = $string->has('some');
Utils
Utils 命名空间包含用于处理插件和主题的工具
Enqueue.php
是与 enqueue 相关函数的包装器,示例
\Mjolnir\Utils\Enqueue::style('theme-style', 'folder/style.css', [], '1.0.0', 'all'); \Mjolnir\Utils\Enqueue::script('theme-script', 'folder/script.css', [], '1.0.0', true); \Mjolnir\Utils\Enqueue::all();
Post.php
是 WP_Post 的包装器,提供了更好的 API 来获取帖子,示例
$currentPost = \Mjolnir\Utils\Post::current(); $post = \Mjolnir\Utils\Post::get(1); $postId = $post->getId(); $postSlug = $post->getSlug();
Theme.php
是与主题相关的函数包装器,例如:
\Mjolnir\Utils\Theme::support('feature'); \Mjolnir\Utils\Theme::textDomain('domain', 'path'); \Mjolnir\Utils\Theme::mod()->set('item', 'value'); \Mjolnir\Utils\Theme::mod()->get('item');
门面
门面是API,允许您创建一个类,该类将从容器中解析类,检查 stubs/app/Facades
以查看默认门面
Action.php
提供对操作钩子的访问,例如
{{Namespace}}\Facades\Action::do('hook'); {{Namespace}}\Facades\Action::add('hook', [$this, 'function']); {{Namespace}}\Facades\Action::group('hook') ->add([$this, 'function']) ->add(SomeClass::class) ->add(function () { echo "something todo"; });
Filter.php
提供对过滤器钩子的访问,例如
{{Namespace}}\Facades\Filter::apply('filter'); {{Namespace}}\Facades\Filter::add('filter', [$this, 'function']); {{Namespace}}\Facades\Filter::group('filter') ->add([$this, 'function']) ->add(SomeClass::class) ->add(function () { echo "something todo"; });
Block.php
提供对与区块相关的类的访问,例如
{{Namespace}}\Facades\Block::add('namespace', 'name'); {{Namespace}}\Facades\Block::exists('namespace/name'); {{Namespace}}\Facades\Block::group('namespace') ->add('name') ->add('name');
Config.php
提供对配置文件的访问,例如
{{Namespace}}\Facades\Config::get('configFile.index.anotherIndex'); {{Namespace}}\Facades\Config::get('app.view.folder');
View.php
提供渲染视图文件的访问,例如
{{Namespace}}\Facades\View::render('books.single'); {{Namespace}}\Facades\View::render('books/single.blade.php');
测试
//TODO
变更日志
请参阅 变更日志 以获取有关最近更改的更多信息。
贡献
请参阅 贡献指南 以获取详细信息。
安全
如果您发现任何安全相关的问题,请通过电子邮件 quotesun@gmail.com 而不是使用问题跟踪器。
致谢
许可证
MIT许可证(MIT)。请参阅 许可证文件 以获取更多信息。