-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRepository.php
More file actions
104 lines (59 loc) · 2.29 KB
/
Repository.php
File metadata and controls
104 lines (59 loc) · 2.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
<?php namespace Quallsbenson\Repository;
use Quallsbenson\Utility\Object\ObjectResolver;
use Quallsbenson\Utility\Object\ObjectWrapper;
use Quallsbenson\Repository\Database\DatabaseManagerInterface;
abstract class Repository implements RepositoryInterface{
protected $models = array(),
$modelResolver,
$databaseManager,
$services = array();
public function setModelResolver(ObjectResolver $resolver){
$this->modelResolver = $resolver;
return $this;
}
public function getModel($name){
if(isset($this->models[$name]))
return $this->models[$name];
$model = $this->modelResolver->resolve($name);
if(!$model)
throw new \Exception("Could Not Resolve Model: " .$name ." from given namespace(s)");
//connect to database if not connected
if(!$this->databaseManager->isConnected())
$this->databaseManager->connect();
return $this->models[$name] = $model->getInstance();
}
public function setDatabaseManager(DatabaseManagerInterface $manager){
$this->databaseManager = $manager;
}
public function getDatabaseManager(){
if(!$this->databaseManager->isConnected())
$this->databaseManager->connect();
return $this->databaseManager;
}
public function db(){
return $this->getDatabaseManager();
}
public function addService(array $services){
$this->services = array_merge($this->services, ObjectWrapper::wrap($services) );
}
public function getService($serviceName){
if(!$this->hasService($serviceName))
throw new \Exception('attempted to call non-existent Serice ' .$serviceName);
return $this->services[$serviceName];
}
public function hasService($serviceName){
return isset($this->services[$serviceName]);
}
public function __call($name, $param){
if($this->hasService($name)){
//if method is a service name, we assume that user was calling a service
$service = $this->getService($name);
//if parameters passed, return singleton instance
if(count($param) > 0)
return call_user_func_array(array($service, 'getSingleton'), $param);
//else just return service and let user decide how to instantiate
return $service;
}
throw new \Exception('Call to undefined method: '.__CLASS__.'::' .$name);
}
}