Skip to content

Commit 112f2dc

Browse files
committed
feat: add support for automatic service init
1 parent e066463 commit 112f2dc

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

src/Commands/GenerateControllerCommand.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,9 @@ protected function handle()
4242

4343
$this->generateController($controllerFile, $controller, $modelName);
4444

45-
return $this->generateExtraFiles($modelName);
45+
$this->generateExtraFiles($modelName);
46+
47+
return 0;
4648
}
4749

4850
protected function generateController($controllerFile, $controller, $modelName): int

src/Controller.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,18 @@ class Controller
1616
public $request;
1717
public $response;
1818
public $view;
19+
protected $services = [];
1920

2021
public function __construct()
2122
{
2223
$this->request = new Http\Request;
2324
$this->response = new Http\Response;
25+
26+
if (count($this->services) > 0) {
27+
foreach ($this->services as $name => $value) {
28+
$this->services[$name] = make($value);
29+
}
30+
}
2431
}
2532

2633
/**
@@ -87,4 +94,15 @@ public function errors()
8794
'validation' => $this->request->errors()
8895
];
8996
}
97+
98+
public function __get(string $name)
99+
{
100+
if (isset($this->services[$name])) {
101+
return $this->services[$name];
102+
}
103+
104+
trigger_error("Undefined property: " . static::class . "::$" . $name, E_USER_WARNING);
105+
106+
return null;
107+
}
90108
}

0 commit comments

Comments
 (0)