了解 opencart 加载程序

Understanding the opencart loader

研究 opencart loader 并试图了解它的工作原理。 loading/calling 个文件

的 opencart 加载器
<?php
final class Loader {
    private $registry;

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

    public function controller($route, $args = array()) {
        $action = new Action($route, $args);

        return $action->execute($this->registry);
    }

    public function model($model) {
        $file = DIR_APPLICATION . 'model/' . $model . '.php';
        $class = 'Model' . preg_replace('/[^a-zA-Z0-9]/', '', $model);

        if (file_exists($file)) {
            include_once($file);

            $this->registry->set('model_' . str_replace('/', '_', $model), new $class($this->registry));
        } else {
            trigger_error('Error: Could not load model ' . $file . '!');
            exit();
        }
    }

    public function view($template, $data = array()) {
        $file = DIR_TEMPLATE . $template;

        if (file_exists($file)) {
            extract($data);

            ob_start();

            require($file);

            $output = ob_get_contents();

            ob_end_clean();

            return $output;
        } else {
            trigger_error('Error: Could not load template ' . $file . '!');
            exit();
        }
    }

    public function library($library) {
        $file = DIR_SYSTEM . 'library/' . $library . '.php';

        if (file_exists($file)) {
            include_once($file);
        } else {
            trigger_error('Error: Could not load library ' . $file . '!');
            exit();
        }
    }

    public function helper($helper) {
        $file = DIR_SYSTEM . 'helper/' . $helper . '.php';

        if (file_exists($file)) {
            include_once($file);
        } else {
            trigger_error('Error: Could not load helper ' . $file . '!');
            exit();
        }
    }

    public function config($config) {
        $this->registry->get('config')->load($config);
    }

    public function language($language) {
        return $this->registry->get('language')->load($language);
    }
}

这是我正在看的部分

public function model($model) {
$file = DIR_APPLICATION . 'model/' . $model . '.php';
$class = 'Model' . preg_replace('/[^a-zA-Z0-9]/', '', $model);

if (file_exists($file)) {
    include_once($file);

    $this->registry->set('model_' . str_replace('/', '_', $model), new $class($this->registry));
} else {
    trigger_error('Error: Could not load model ' . $file . '!');
    exit();
}
}

这是我从上面的代码中得出的结论。当调用模型时(假设模型名称为 ModelA),$file 设置为 catalog/model/ModelA.php 并将 $class 设置为 ModelModelA 然后检查文件($ file) 存在,如果它包含它 (include_once ($file))。

我不明白的是这部分$this->registry->set('model_' . str_replace('/', '_', $model), new $class($this->registry));,我从中得出的结论是它正在尝试注册模型文件名,但是如何注册?

如果你看到 OC 的 index.php,则有一些注册表已完成,例如 $registry->set('db', $db)。但是这个加载器注册表让我感到困惑,我只得到第一部分 'model_' . str_replace('/', '_', $model) 将 'ModelA' 转换为 'Model_ModelA' 但是这个 new $class($this->registry) 做了什么......新 Model_ModelA($this->注册表)?

新 Model_ModelA($this->registry) 中的 $this->registry 是什么?

好的,在花了一整天的时间阅读了几篇文章后,我发现并通过一些测试确认了它的作用$this->registry->set('model_' . str_replace('/', '_', $model), new $class($this->registry));实际上是在注册表系统中注册模型

$registry->set(model_ModelA, new model_ModelA($this->registry))

很像其他人在 index.php 上注册。