Понимание загрузчика OpenCart

Изучаю загрузчик opencart и пытаюсь понять, как он работает. Загрузчик 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), для файла $ устанавливается значение catalog / model / ModelA.php, а для класса $ — ModelModelA, затем проверяется, существует ли файл ($ file) и включает ли он это (include_once ($ file)).

Что я не понимаю, это часть $this->registry->set('model_' . str_replace('/', '_', $model), new $class($this->registry));Что я делаю из этого, так это то, что он пытается зарегистрировать имя файла модели, но как?

Если вы видите index.php в OC, вам нужно выполнить несколько действий: $registry->set('db', $db), Но этот реестр загрузчиков меня смущает, я получаю только первую часть 'model_' . str_replace('/', '_', $model) который преобразует «ModelA» в «Model_ModelA», но что это делает new $class($this->registry) сделать … new Model_ModelA ($ this-> Registry)?

Что такое $ this-> реестр в новой Model_ModelA ($ this-> registry)?

0

Решение

Хорошо, проведя целый день и пройдя несколько статей, я нашел и подтвердил с помощью нескольких тестов, что это делает $this->registry->set('model_' . str_replace('/', '_', $model), new $class($this->registry)); на самом деле регистрирует модель как таковую в системе реестра

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

совсем как другие, зарегистрированные на index.php.

1

Другие решения

Других решений пока нет …