我正在为由nodejs外部进程运行的计划作业制作一个小框架。我想使用自动加载器,但由于某种原因,数据无法到达它。我还使用了名称空间。下面是我的文件夹结构:
Library
|_ Core
|_ JobConfig.php
|_ Utilities
|_ Loader.php
|_ Scheduled
|_ SomeJob.php
|_ Config.php
我的Config.php
只有一些定义和我的Loader.php
的一个实例。
Loader.php如下:
public function __constructor()
{
spl_autoload_register(array($this, 'coreLoader'));
spl_autoload_register(array($this, 'utilitiesLoader'));
}
private function coreLoader($class)
{
echo $class;
return true;
}
private function utilitiesLoader($lass)
{
echo $class;
return true;
}
因此,对于我的SomeJob.php
,我包括Config.php
,然后在失败时尝试实例化JobConfig.php。我的名称空间看起来像LibraryCoreJobConfig
等等。我不确定如果没有引导能力,这是否是最好的方法。但是在加载器失败之前,我没有看到它的回显。
我尝试了@Layne的建议,但没有用。我仍然得到一个没有找到的类,似乎类没有进入spl堆栈。下面是代码
如果您实际以与使用目录结构相同的方式使用名称空间,那么这应该相当容易。
<?php
namespace Library {
spl_autoload_register('LibraryAutoloader::default_autoloader');
class Autoloader {
public static function default_autoloader($class) {
$class = ltrim($class, '\');
$file = __DIR__ . '/../';
if ($lastNsPos = strrpos($class, '\')) {
$namespace = substr($class, 0, $lastNsPos);
$class = substr($class, $lastNsPos + 1);
$file .= str_replace('\', '/', $namespace) . '/';
}
$file .= $class . '.php';
include $file;
}
}
}
将其放入Library目录中,并在更高级别上要求它。希望我没有把它搞砸,没有测试它
编辑:固定路径
使用这个类来索引您的项目,只需将WP_CONTENT_DIR
替换为另一个目录级别来扫描php文件,这个类自动包含文件:
<?php
class autoloader
{
public static function instance()
{
static $instance = false;
if( $instance === false )
{
// Late static binding
$instance = new static();
}
return $instance;
}
/**
* @param $dir_level directory level is for file searching
* @param $php_files_json_name name of the file who all the PHP files will be stored inside it
*/
private function export_php_files($dir_level, $php_files_json_name)
{
$filePaths = array(mktime());
/**Get all files and directories using iterator.*/
$iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($dir_level));
foreach ($iterator as $path) {
if (is_string(strval($path)) and pathinfo($path, PATHINFO_EXTENSION) == 'php') {
$filePaths[] = strval($path);
}
}
/**Encode and save php files dir in a local json file */
$fileOpen = fopen($dir_level . DIRECTORY_SEPARATOR . $php_files_json_name, 'w');
fwrite($fileOpen, json_encode($filePaths));
fclose($fileOpen);
}
/**
* @param $php_files_json_address json file contains all the PHP files inside it
* @param $class_file_name name of the class that was taken from @spl_autoload_register_register plus .php extension
* @return bool Succeeding end of work
*/
private function include_matching_files($php_files_json_address, $class_file_name)
{
static $files;
$inc_is_done = false;
if ($files == null) {
$files = json_decode(file_get_contents($php_files_json_address), false);
}
/**Include matching files here.*/
foreach ($files as $path) {
if (stripos($path, $class_file_name) !== false) {
require_once $path;
$inc_is_done = true;
}
}
return $inc_is_done;
}
/**
* @param $dir_level directory level is for file searching
* @param $class_name name of the class that was taken from @spl_autoload_register
* @param bool $try_for_new_files Try again to include new files, that this feature is @true in development mode
* it will renew including file each time after every 30 seconds @see $refresh_time.
* @return bool Succeeding end of work
*/
public function request_system_files($dir_level, $class_name, $try_for_new_files = false)
{
$php_files_json = 'phpfiles.json';
$php_files_json_address = $dir_level . DIRECTORY_SEPARATOR . $php_files_json;
$class_file_name = $class_name . '.php';
$files_refresh_time = 30;
/**Include required php files.*/
if (is_file($php_files_json_address)) {
$last_update = json_decode(file_get_contents($php_files_json_address), false)[0];
if ((mktime() - intval($last_update)) < $files_refresh_time || !$try_for_new_files) {
return $this->include_matching_files($php_files_json_address, $class_file_name);
}
}
$this->export_php_files($dir_level, $php_files_json);
return $this->include_matching_files($php_files_json_address, $class_file_name);
}
/**
* Make constructor private, so nobody can call "new Class".
*/
private function __construct()
{
}
/**
* Make clone magic method private, so nobody can clone instance.
*/
private function __clone()
{
}
/**
* Make sleep magic method private, so nobody can serialize instance.
*/
private function __sleep()
{
}
/**
* Make wakeup magic method private, so nobody can unserialize instance.
*/
private function __wakeup()
{
}
}
/**
* Register autoloader.
*/
try {
spl_autoload_register(function ($className) {
$autoloader = autoloader::instance();
return $autoloader->request_system_files(WP_CONTENT_DIR, $className, true);
});
} catch (Exception $e) {
var_dump($e);
die;
}