michaeluno / php-classmap-generator
一个用于生成类映射的PHP类,以用于自动加载。
1.3.1
2022-02-22 16:20 UTC
Requires
- php: >=5.6.20
README
一个用于生成类映射的PHP类,以用于自动加载。
安装
Composer
使用Composer安装库,运行
composer require michaeluno/php-classmap-generator
用法
使用选项实例化类。
new \PHPClassMapGenerator\PHPClassMapGenerator( __DIR__, // base dir __DIR__ . '/_scandir', // scan dir path __DIR__ . '/class-map.php', // the result output file );
这会创建一个类似以下的类映射文件。
<?php $aClassMap = array( "Foo\FooClass" => CLASS_MAP_BASE_DIR_VAR . "/_scandir/FooClass.php", "Foo\FooClass_Base" => CLASS_MAP_BASE_DIR_VAR . "/_scandir/FooClass_Base.php", "Joe\JoeInterface" => CLASS_MAP_BASE_DIR_VAR . "/_scandir/interfaces/JoeInterface.php", "Bar\Bar" => CLASS_MAP_BASE_DIR_VAR . "/_scandir/traits/BarTrait.php", );
在包含映射文件之前,在您的PHP脚本中定义常量 CLASS_MAP_BASE_DIR_VAR
。
define( 'CLASS_MAP_BASE_DIR_VAR', __DIR__ );
或任何基础目录路径。
这个自动插入的字符串 CLASS_MAP_BASE_DIR_VAR
可以通过选项参数 base_dir_var
改为所需的任何字符串。更多详情请见第4个参数部分。
接口和特性也包含在内。
参数
该类接受四个参数。
1. (字符串) 基础目录路径
第一个参数接受基础目录路径。
这是必需的,因为扫描和收集的绝对路径是在脚本运行的系统上。然而,程序的实际用户根据他们的系统并不共享相同的绝对路径。这就是为什么基础目录路径将在输出中被替换为常量或变量。
2. (字符串|数组) 扫描目录路径
第二个参数接受要扫描的目录路径。对于多个路径,传递一个包含它们的数字索引数组。
示例
new \PHPClassMapGenerator\PHPClassMapGenerator( __DIR__, __DIR__ . '/scandir', __DIR__ . '/class-map.php', );
new \PHPClassMapGenerator\PHPClassMapGenerator( __DIR__, [ __DIR__ . '/scandir', __DIR__ . '/scandir2' ], __DIR__ . '/class-map.php', );
3. (字符串) 输出PHP文件路径。
设置一个路径,生成的列表将被写入。
4. 选项(可选)
此参数接受一个包含选项的数组。
output_buffer
: (布尔值) 是否输出缓冲区应打印。exclude_classes
: (数组) 要排除的类名数组。base_dir_var
: (字符串) 在包含路径之前前缀的变量或常量名称。默认:__DIR__
。output_var_name
: (字符串) 将映射数组分配到的变量字符串。默认:$aClassMap
。如果设置return
,则不会设置变量,但文件将返回生成的映射数组。do_in_constructor
: (布尔值) 是否在构造函数中执行操作。默认:true
。structure
: (字符串)CLASS
或PATH
。当设置为CLASS
时,生成的数组键由类名组成。当设置为PATH
时,生成的数组键由路径组成。默认:CLASS
。short_array_syntax
: (布尔值) 是否使用array()
或[]
进行数组声明。为[]
设置为true
。默认:false
。search
: (数组) 目录搜索选项的参数。allowed_extensions
: (数组) 要列出的允许文件扩展名。例如:[ 'php', 'inc' ]
exclude_dir_paths
: (数组) 要从列表中排除的目录路径。exclude_dir_names
: (数组) 要从列表中排除的目录基本名称。例如:[ 'temp', '_bak', '_del', 'lib', 'vendor', ]
exclude_file_names
: (数组) 要从列表中排除的文件名子串。例如:[ '.min' ]
exclude_substrings
: (数组) 要从列表中排除的路径子串。例如:[ '.min', '_del', 'temp', 'library', 'vendor' ]
is_recursive
: (布尔值) 是否扫描子目录。ignore_note_file_names
: (数组) 忽略提示文件名,告知解析器跳过目录。当解析目录中存在其中一个文件时,将跳过该目录。默认:[ 'ignore-class-map.txt' ]
,
comment_header
: (数组,可选) 在生成的文件顶部插入的注释头部text
: (字符串,可选) 要设置的注释头部path
: (字符串,可选) 从中提取注释的文件路径class
: (字符串,可选) 使用类的文档块作为头部注释的类名type
: (字符串,可选) 指示要收集的数据类型。接受的值有DOCBLOCK
,CONSTANT
。
示例
在脚本目录中生成类映射。
new \PHPClassMapGenerator\PHPClassMapGenerator( dirname( __DIR__ ), [ __DIR__ . '/includes', ], __DIR__ . '/class-map.php', [ 'output_buffer' => true, 'exclude_classes' => [ 'TestClass' ], 'output_var_name' => '$classMap', 'base_dir_var' => '\MyProject\Registry::$dirPath', 'search' => [ 'allowed_extensions' => [ 'php' ], 'exclude_dir_paths' => [ __DIR__ . '/includes/class/admin' ], 'exclude_dir_names' => [ '_del', '_bak' ], 'exclude_file_names' => [ 'test.php', 'uninsall.php' ], 'is_recursive' => true, ], ] );
不要写入文件
$_oGenerator = new \PHPClassMapGenerator\PHPClassMapGenerator( __DIR__, // base dir __DIR__ . '/_scandir', // scan dir name __DIR__ . '/class-map.php', [ 'do_in_constructor' => false, ] ); print_r( $_oGenerator->get() );
查找CSS文件
$_oGenerator = new \PHPClassMapGenerator\PHPClassMapGenerator( __DIR__, // base dir __DIR__ . '/_scandir', // scan dir name __DIR__ . '/class-map.php', [ 'output_var_name' => 'return', 'do_in_constructor' => false, 'structure' => 'PATH', 'search' => [ 'allowed_extensions' => [ 'css' ], 'ignore_note_file_names' => [ 'ignore-css-map.txt' ], 'exclude_file_names' => [ '.min.' ], ], ] ); print_r( $_oGenerator->get() );
要查找JavaScript文件,将 allowed_extensions
搜索参数中的 'css'
部分改为 'js'
。
捆绑实用工具自动加载器
此包包含一个自动加载器。
示例
包含类列表数组
$_aClassMap = [
"PHPClassMapGenerator\\PHPClassMapGenerator" => __DIR__ . "/PHPClassMapGenerator.php",
"PHPClassMapGenerator\\Autoload" => __DIR__ . "/autoload.php",
];
PHPClassMapGenerator\Utility\Autoload::set( $_aClassMap );
包含返回类列表数组的类映射文件
PHPClassMapGenerator\Utility\Autoload::set( include( __DIR__ . '/class-map.php' ) );
许可证
许可协议:MIT。详情请见:MIT许可证。