nazonohito51/require-path-fixer

修复 require/include 路径

v0.2.1 2018-05-15 07:16 UTC

This package is auto-updated.

Last update: 2024-08-27 14:39:25 UTC


README

传统的 PHP 项目有大量的 require 语句 (require/require_once/include/include_once)。由于它们不统一,因此很难收集它们。这个库会搜索所有的 require 语句并将它们修改成统一的代码。

Latest Stable Version

// before
require_once 'path/to/file.php';
require_once __DIR__ . '/path/to/file.php';
require_once dirname(__FILE__).'/path/to/file.php';
require_once('../app_root/path/to/file.php');
require_once      (   'path/' .
     'to/'                                  .         'file.php'     );
require 'path/to/file.php';
include_once 'path/to/file.php';
include 'path/to/file.php';
require_once UNKNOWN_CONSTANT . 'path/to/file.php';

// after
require_once APP_ROOT . '/path/to/file.php';
require_once APP_ROOT . '/path/to/file.php';
require_once APP_ROOT . '/path/to/file.php';
require_once APP_ROOT . '/path/to/file.php';
require_once APP_ROOT . '/path/to/file.php';
require APP_ROOT . '/path/to/file.php';
include_once APP_ROOT . '/path/to/file.php';
include APP_ROOT . '/path/to/file.php';
require_once UNKNOWN_CONSTANT . 'path/to/file.php';

安装

composer require "nazonohito51/require-path-fixer"

使用

require_once __DIR__. '/vendor/autoload.php';
$fixer = new \RequirePathFixer\Fixer($inspectDirPath);   // It is strongly recommended that $inspectDirPath be a repository root.

$fixer->report($inspectDirPath, "APP_ROOT");    // Only reporting.
$fixer->fix($inspectDirPath, "APP_ROOT");       // Fix all files.
// The first argument of these methods is the base path of the modified statement.
// The second argument is a constant or method representing the base path.
// ex: "APP_ROOT", "Config::get('app.root')"

高级使用

定义常量和变量

以下语句将不会被替换,因为不知道 COMMON_DIR 路径是什么。

require_once COMMON_DIR . '/path/to/file.php';

如果有要替换的常量或变量,请将其传递到以下方法。

$fixer->addConstant('COMMON_DIR', '/path/to/common/dir/');   // COMMON_DIR will be replaced to '/path/to/common/dir/'
$fixer->addVariable('$smartyDir', '/path/to/smarty/dir/');   // $smartyDir will be replaced to '/path/to/smarty/dir/'

限制修改目标

如果你有不想修改的文件或目录,请使用 addBlackList()

$fixer->addBlackList($inspectDirPath . '/vendor');
$fixer->addBlackList($inspectDirPath . '/tests');

或者,如果你想只修改仓库中的某些文件,请使用 addWhiteList()addBlackList()addWhiteList() 可以同时使用。

$fixer->addWhiteList($inspectDirPath . '/app');

定义当前目录

在 PHP 中,以下语句(以 '.' 或 '..' 开头)用于从当前目录确定路径。

require_once './path/to/file.php';

详细信息请参考 PHP 手册。如果你想定义当前目录,请使用 setWorkingDir()。当当前目录被定义后,这个库会从当前目录解析上述语句。然而,原始的当前目录会根据入口点改变,并且还会使用 chdir() 动态改变。请谨慎使用。

$fixer->setWorkingDir($inspectDirPath . '/public');

定义 include_path

在 PHP 中,以下语句(相对路径)用于从 include_path 确定路径。

require_once 'path/to/file.php';

如果你想定义 include_path,请使用 setIncludePath()。当 include_path 被定义后,这个库会从 include_path 解析上述语句。然而,原始的 include_path 会使用 set_include_path() 动态改变。请谨慎使用。

$fixer->setIncludePath(".:{$inspectDirPath}/app");

关于分析逻辑

这个库将所有语句分类为七种类型并转换它们。它们的类型有 absolutevariablerelativeuniqueworking_dirinclude_pathunexpected

absolute

如果路径是绝对路径,将其修复为新基准路径的路径。

// before
require_once __DIR__ . '/hoge/app_root/path/to/file.php';
require_once (dirname(__FILE__).'/../app_root/' .'path/to/file.php');

// after
require_once APP_ROOT . '/path/to/file.php';
require_once APP_ROOT . '/path/to/file.php';

variable

如果路径无法解析,例如未知变量或常量,则不转换。

// before
require_once UNKNOWN_CONSTANT . 'path/to/file.php';
require_once $unknownVariable . 'path/to/file.php';

// after
require_once UNKNOWN_CONSTANT . 'path/to/file.php';
require_once $unknownVariable . 'path/to/file.php';

relative

如果路径是相对路径,基本上它只在运行时确定,因此无法替换。但这个库试图猜测路径。这是 uniqueworking_dirinclude_path。然而,如果无法猜测,则不会转换。这是 relative

// before
require_once 'path/to/file.php';
require_once './path/to/file.php';
require_once '../path/to/file.php';
require_once COMMON_DIR . '/path/to/file.php';   // COMMON_DIR is './app_root/common'

// after
require_once 'path/to/file.php';
require_once './path/to/file.php';
require_once '../path/to/file.php';
require_once COMMON_DIR . '/path/to/file.php';

unique

在相对路径中,如果仓库中只有一个文件与语句中的路径匹配,这个库将将其转换为该文件的路径。

// before
require_once 'path/to/file.php';    // There is only one file that matches '/path\/to\/file\.php$/'.
require_once './hoge/fuga.php';    // There is only one file that matches '/hoge\/fuga\.php$/'.
require_once '../foo/bar.php';    // There was no file that matched the '/foo\/bar\.php$/', or there were multiple files.

// after
require_once APP_ROOT . '/path/to/file.php';
require_once APP_ROOT . '/path/to/hoge/fuga.php';
require_once '../foo/bar.php';

working_dir

在以 '.' 或 '..' 开头的相对路径中,如果通过 setWorkingDir() 定义了当前目录,这个库将从当前目录解析路径。并且这个库不会通过 unique 猜测。

$fixer->setWorkingDir($inspectDirPath . '/public');

// before
require_once './hoge/fuga.php';
require_once '../foo/bar.php';
require_once COMMON_DIR . '/path/to/file.php';   // COMMON_DIR is './app_root/common'

// after
require_once APP_ROOT . '/public/hoge/fuga.php';
require_once APP_ROOT . '/foo/bar.php';
require_once APP_ROOT . '/common/path/to/file.php';

include_path

在不以 '.' 或 '..' 开头的相对路径中,如果通过 setIncludePath() 定义了 include_path,这个库将从 include_path 解析路径。并且这个库不会通过 unique 猜测。

$fixer->setIncludePath('.:' . $inspectDirPath . '/common');   // '.' will be replace current directory, but if it is not defined, '.' will be ignored

// before
require_once 'hoge/fuga.php';
require_once 'foo/bar.php';

// after
require_once APP_ROOT . '/common/hoge/fuga.php';
require_once APP_ROOT . '/common/foo/bar.php';

unexpected

如果语句不属于任何类型,它将是这种类型。