Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
90.48% |
19 / 21 |
|
66.67% |
2 / 3 |
CRAP | |
0.00% |
0 / 1 |
ORM | |
90.48% |
19 / 21 |
|
66.67% |
2 / 3 |
10.09 | |
0.00% |
0 / 1 |
__construct | |
100.00% |
9 / 9 |
|
100.00% |
1 / 1 |
5 | |||
getRepository | |
81.82% |
9 / 11 |
|
0.00% |
0 / 1 |
4.10 | |||
getConnection | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 |
1 | <?php |
2 | |
3 | namespace Miniframe\ORM\Middleware; |
4 | |
5 | use Miniframe\Annotation\Service\AnnotationReader; |
6 | use Miniframe\Core\AbstractMiddleware; |
7 | use Miniframe\Core\Config; |
8 | use Miniframe\Core\Request; |
9 | use Miniframe\ORM\Annotation\Table; |
10 | use Miniframe\ORM\Engine\EngineInterface; |
11 | use Miniframe\ORM\Repository\GenericRepository; |
12 | |
13 | class ORM extends AbstractMiddleware |
14 | { |
15 | /** |
16 | * Reference to the database engine |
17 | * |
18 | * @var EngineInterface |
19 | */ |
20 | protected $engine; |
21 | |
22 | /** |
23 | * List of repositories |
24 | * |
25 | * @var GenericRepository[] |
26 | */ |
27 | protected $repositories = array(); |
28 | |
29 | /** |
30 | * Initializes the Miniframe ORM Middleware package |
31 | * |
32 | * @param Request $request Reference to the Request object. |
33 | * @param Config $config Reference to the Config object. |
34 | */ |
35 | public function __construct(Request $request, Config $config) |
36 | { |
37 | parent::__construct($request, $config); |
38 | |
39 | $engine = $config->get('orm', 'engine'); |
40 | if (!class_exists($engine) && class_exists('Miniframe\\ORM\\Engine\\' . $engine)) { |
41 | $engine = 'Miniframe\\ORM\\Engine\\' . $engine; |
42 | } elseif (!class_exists($engine)) { |
43 | throw new \RuntimeException('Engine ' . $engine . ' not available'); |
44 | } |
45 | if (!in_array(EngineInterface::class, class_implements($engine))) { |
46 | throw new \RuntimeException($engine . ' does not implement ' . EngineInterface::class); |
47 | } |
48 | $this->engine = new $engine($config); |
49 | } |
50 | |
51 | /** |
52 | * Returns the repository for a specific entity. |
53 | * |
54 | * @param string $entityClassName Fully qualified classname of the entity. |
55 | * |
56 | * @return GenericRepository |
57 | */ |
58 | public function getRepository(string $entityClassName): GenericRepository |
59 | { |
60 | if (!isset($this->repositories[$entityClassName])) { |
61 | $repository = (new AnnotationReader()) |
62 | ->getClass(new \ReflectionClass($entityClassName)) |
63 | ->getAnnotation(Table::class) |
64 | ->repository; |
65 | if (!$repository) { |
66 | $repository = GenericRepository::class; |
67 | } else { |
68 | if (!in_array(GenericRepository::class, class_parents($repository))) { |
69 | throw new \RuntimeException($repository . ' doesn\'t extend ' . GenericRepository::class); |
70 | } |
71 | } |
72 | $this->repositories[$entityClassName] = new $repository($this->engine, $entityClassName); |
73 | } |
74 | return $this->repositories[$entityClassName]; |
75 | } |
76 | |
77 | /** |
78 | * Returns the connection of the engine (result may differ per engine) |
79 | * |
80 | * @return mixed |
81 | */ |
82 | public function getConnection() |
83 | { |
84 | return $this->engine->getConnection(); |
85 | } |
86 | } |