khalyomede/expose

此包已废弃,不再维护。未建议替代包。

通过函数公开您的类以加快开发速度

v1.0.0 2018-02-27 22:00 UTC

This package is auto-updated.

Last update: 2022-02-01 13:12:24 UTC


README

由于与PHP核心功能相关的安全问题,此包现在已废弃(更多信息请点击此链接)。

Expose

PHP from Packagist Packagist Packagist

通过函数公开您的类以加快开发。

$collection = new Collection(['Dreamer', 'Thunderbird', 'Eclipse']);

$items = $collection->all();

Expose::make('Collection', 'collect');

$items = collect(['Dreamer', 'Thunderbird', 'Eclipse'])->all();

摘要

先决条件

  • PHP 版本 >= 7.0.0

安装

在您的项目文件夹中

composer require khalyomede/expose:1.*

使用示例

所有示例均位于 /example 文件夹中。

以下示例假设存在以下类

class Collection {
  protected $items;

  public function __construct($items) {
    $this->items = $items;
  }

  public function all() {
    return $this->items;
  }
}

class Log {
  protected $message;

  public function __construct($message) {
    $this->message = $message;
  }

  public function error() {
    echo sprintf('Error: %s', $this->message) . PHP_EOL;
  }
}

示例 1:公开一个简单的类

use Khalyomede\Expose;

Expose::make('Collection', 'collection');

// or 

Expose::make(Collection::class, 'collection');

$items = collection(['Thunderbird', 'Polaris', 'Eclipse'])->all();

print_($items);

将显示

Array 
(
  [0] => Thunderbird
  [1] => Polaris
  [2] => Eclipse
)

示例 2:同时公开多个类

use Khalyomede\Expose;

Expose::make([
  [Collection::class, 'collector'],
  [Log::class, 'logging'] // beware of existing functions!
]);

$items = collector(['Thunderbird', 'Polaris', 'Eclipse'])->all();

print_r($items);

logging("duplicate entry 'Dreamer' on line 9")->error();

将显示

Array
(
  [0] => Thunderbird
  [1] => Polaris
  [2] => Eclipse
)
duplicate entry 'Dreamer' on line 9

方法定义

make()

使用别名和快捷方式公开类,并创建相关函数。

public static function make(string $class_name, string $function_name): void
public static function make(array $exposures): void

异常

InvalidArgumentException:

  • 当使用两个参数时,第一个参数不是字符串
  • 当使用两个参数时,第二个参数不是字符串
  • 当函数名已存在时
  • 当使用单个参数时,参数不是数组
  • 当使用单个参数时,参数不是数组数组
  • 当使用单个参数时,参数不是包含字符串的数组数组
  • 当使用单个参数时,参数的数组数组包含空行

注意

此函数还可以公开静态类。

MIT 许可证

Expose

版权 © 2018 Khalyomede

在此,任何人免费获得此软件及其相关文档副本(“软件”)的副本,并有权在不受限制的情况下处理该软件,包括但不限于使用、复制、修改、合并、发布、分发、再许可和/或出售软件副本,并允许向软件提供软件的人这样做,但需遵守以下条件

上述版权声明和本许可声明应包含在软件的所有副本或主要部分中。

本软件按照“原样”提供,不提供任何形式的保证,无论是明确的还是隐含的,包括但不限于对适销性、特定用途适用性和非侵权的保证。在任何情况下,作者或版权所有者都不应对任何索赔、损害或其他责任负责,无论这些责任是基于合同、侵权或其他原因,这些责任源于、涉及或与软件的使用或其他方式有关。