soundasleep / component-discovery
Composer 的 PHP 组件发现
Requires (Dev)
- soundasleep/component-tests: dev-master
This package is auto-updated.
Last update: 2024-09-14 08:50:49 UTC
README
component-discovery 是一个支持 Composer 的 PHP 脚本,用于定义 PHP 组件的类型,然后可以自动加载这些类型,这些类型由您的解决依赖项生成。
组件实例通过键/类实例映射进行定义。为了方便使用,通过 Composer 加载的组件应在它们的 composer.json
中定义自己的自动加载映射;然而,您仍然可以定义自己的 spl_autoload_register
函数。
配置
首先在项目的 composer.json
中将 component-discovery
包括为需求,然后运行 composer update
将其安装到项目中
{ "require": { "soundasleep/component-discovery": "dev-master" } }
现在在项目中创建一个 discovery.json
文件,以定义要发现的组件类型
{ "components": { "currencies": "currencies.json", "jobs": "jobs.json" }, "src": "vendor/*/*", "dest": "inc/components" }
component-discovery 将在所有 src
文件夹中查找定义文件(例如 currencies.json
),以查找键和类映射。例如,在您的 vendor/my/package/currencies.json
{ "btc": "\\Currency\\Bitcoin", "ltc": "\\Currency\\Litecoin" }
请注意,您需要在类名中的命名空间字符进行转义。现在您可以在构建脚本中或手动调用 component-discovery。
您也可以将组件文件定义为数组,而不是字典,数组的值也将用作每个值的键。
[ "\\Currency\\Bitcoin", "\\Currency\\Litecoin" ]
构建
运行生成脚本,无论是通过构建脚本还是手动,给定一个根目录
php -f vendor/soundasleep/component-discovery/generate.php .
这将生成 src
目录下的各种文件,这些文件提供了发现类型、键和类实例之间的所有运行时映射。
例如,这可以生成以下包含文件 inc/components/Currencies.php
,基于您在 vendor/
中通过 Composer 加载的组件,列出所有 Currency 组件
<?php /** * @generated by component-discovery DO NOT EDIT */ namespace DiscoveredComponents; class Currencies extends \ComponentDiscovery\Base { function getKeys() { return ["btc", "ltc", "nmc"]; } function getInstance($key, $config = false) { switch ($key) { case "btc": return new \Currency\Bitcoin($config); case "ltc": return new \Currency\Litecoin($config); case "nmc": return new \Currency\Namecoin($config); default: throw new \ComponentDiscovery\DiscoveryException("Could not find any Currencies with key '\$key'"); } } function getAllInstances($config = false) { return ["btc" => new \Currency\Bitcoin($config), "ltc" => new \Currency\Litecoin($config), "nmc" => new \Currency\Namecoin($config)]; } } ?>
使用
例如,列出之前示例中加载的所有货币
require("inc/components/Currencies.php"); $discovery = new DiscoveredComponents/Currencies(); print_r($discovery->getKeys());
根据键加载货币
require("inc/components/Currencies.php"); $discovery = new DiscoveredComponents/Currencies(); $currency = $discovery->getInstance("btc"); echo "btc = " . $currency->getName();
创建更复杂的映射
您还可以为要发现的每种组件类型创建更复杂的定义
{ "components": { "currencies": { "file": "currencies.json", "instanceof": "\\Openclerk\\Currencies\\Currency", "maps": { "getKeyForAbbr": "getAbbr" }, "masks": { "getCryptocurrencies": "isCryptocurrency", "getFiatCurrencies": "isFiat", "getCommodityCurrencies": "isCommodity" }, "lists": { "getAbbrs": "getAbbr" }, "instances": { "getBalanceCurrencies": "\\Openclerk\\Currencies\\BalanceableCurrency" } } } }
instanceof
:检查每个组件中找到的每个类是否是给定类或接口的实例maps
:创建返回每个类上此方法返回值的函数的函数masks
:创建返回所有类返回true
的键的列表的函数lists
:创建返回每个类上此方法返回值的列表的函数instances
:创建返回所有类是给定类或接口实例的键的列表的函数
例如
static function getKeyForAbbr($input) { switch ($input) { case "BTC": return "btc"; default: throw new \ComponentDiscovery\DiscoveryException("Could not find any matching getKeyForAbbr for '$input'"); } } static function getCryptocurrencies() { return array("btc"); } static function getFiatCurrencies() { return array(); } static function getCommodityCurrencies() { return array(); } static function getAbbrs() { return array("btc" => "BTC"); } static function getBalanceCurrencies() { return array("btc"); }
待办事项
- 更多文档,特别是默认的
discovery.json
参数 - 使用 component-discovery 的示例项目
- 创建
grunt
任务grunt-php-component-discovery
来封装手动 PHP 命令 - 发布 0.1 版本