Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove sharing the request obj #15

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 49 additions & 32 deletions ModuleJson.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
<?php
namespace Level2\Router\Config;
class ModuleJson implements \Level2\Router\Rule {
private $dice;
private $moduleDir;
private $configFile;
private $moduleNames;
private $jsonLoader;
private $request;

public function __construct(\Dice\Dice $dice, \Level2\Core\Request $request, $moduleDir = 'Modules', $configFile = 'manifest.json') {
Expand Down Expand Up @@ -41,7 +40,7 @@ public function find(array $route) {

private function getRouteDir($moduleName) {
$files = glob($this->moduleDir . '/*');
$match = preg_grep('/' . $this->moduleDir . '\/' . $moduleName . '/i', $files);
$match = preg_grep('/^' . $this->moduleDir . '\/' . $moduleName . '$/i', $files);
return array_values($match)[0] ?? false;
}

Expand All @@ -53,35 +52,51 @@ public function getConfig($route) {
return $this->getRouteModuleFile($file);
}

private function getRouteModuleFile($file) {
if (file_exists($file)) {
$config = json_decode(str_replace('"./', '"' . dirname($file) . '/', file_get_contents($file)), true);

// Extend property
if (isset($route['extend'])) {
$extended = $this->getRouteModuleFile($directory . DIRECTORY_SEPARATOR . $route['extend']);
$config = array_merge($extended, $config);
}
return $config;
}
else return false;
}
private function getRouteModuleFile($file) {
if (file_exists($file)) {
$config = json_decode(str_replace('"./', '"' . dirname($file) . '/', file_get_contents($file)), true);

// Extend property
if (isset($config['extend'])) {
$extended = $this->getRouteModuleFile($config['extend']);
$config = $this->mergeConfig($config, $extended);
}
return $config;
}
else return false;
}

private function mergeConfig($orig, $ext) {
foreach (['GET', 'POST', 'conditions'] as $index) {
if (isset($orig[$index]) || isset($ext[$index])) $orig[$index] = array_merge($ext[$index], $orig[$index] ?? []);
}
return $orig;
}

private function getModel($settings, $name = '') {
if (class_exists($settings['instanceOf'])) {
$this->dice = $this->dice->addRule('$Model_' . $name, $settings);
$model = $this->dice->create('$Model_' . $name, [], []);
}
else {
$model = $this->dice->create($settings['instanceOf']);
}
return $model;
}

private function getRoute($routeSettings, $route) {
$this->dice->addRule('$View', $routeSettings['view']);

if (isset($routeSettings['model'])) {
$this->dice->addRule('$Model', $routeSettings['model']);
$model = $this->dice->create('$Model', [], [$this->request]);
}
else if (isset($routeSettings['models'])) {
$model = [];
foreach ($routeSettings['models'] as $name => $diceRule) {
$this->dice->addRule('$Model_' . $name, $diceRule);
$model[$name] = $this->dice->create('$Model_' . $name, [], [$this->request]);
}
}
else $model = null;
$this->dice = $this->dice->addRule('$View', $routeSettings['view']);

if (isset($routeSettings['model'])) {
$model = $this->getModel($routeSettings['model']);
}
else if (isset($routeSettings['models'])) {
$model = [];
foreach ($routeSettings['models'] as $name => $diceRule) {
$model[$name] = $this->getModel($diceRule, $name);
}
}
else $model = null;

if (isset($routeSettings['controller'])) {

Expand All @@ -94,8 +109,10 @@ private function getRoute($routeSettings, $route) {

$controllerRule['call'] = [];

$controllerRule['call'][] = [$action, $route];
$this->dice->addRule('$controller', $controllerRule);
$controllerRule['call'][] = [$action, $route, function ($return) use (&$model) {
if (is_object($return)) $model = $return;
}];
$this->dice = $this->dice->addRule('$controller', $controllerRule);
if (is_array($model)) {
$controller = $this->dice->create('$Controller', [], array_merge(array_values($model), [$this->request]));
}
Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
],
"require": {
"php": ">=5.5.0",
"level-2/router" : "dev-master"
"level-2/router" : "dev-master",
"level-2/dice" : "^4.0"
},
"autoload": {
"classmap": ["./"]
Expand Down