1 <?php
2
3 /*
4 * This file is part of the ICanBoogie package.
5 *
6 * (c) Olivier Laviale <olivier.laviale@gmail.com>
7 *
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
10 */
11
12 namespace ICanBoogie\ActiveRecord;
13
14 use ICanBoogie\Core;
15
16 class Hooks
17 {
18 /**
19 * Synthesizes the `activerecord_connections` config from `activerecord` fragments.
20 *
21 * @param array $fragments
22 *
23 * @return array
24 */
25 static public function synthesize_connections_config(array $fragments)
26 {
27 $config = [];
28
29 foreach ($fragments as $fragment)
30 {
31 if (empty($fragment['connections']))
32 {
33 continue;
34 }
35
36 $config = array_merge($config, $fragment['connections']);
37 }
38
39 return $config;
40 }
41
42 /*
43 * Prototypes
44 */
45
46 /**
47 * Returns the connections accessor.
48 *
49 * @return ActiveRecord\Connections
50 */
51 static public function core_lazy_get_connections(Core $core)
52 {
53 return new Connections($core->configs['activerecord_connections'] ?: []);
54 }
55
56 /**
57 * Getter for the "primary" database connection.
58 *
59 * @return Database
60 */
61 static public function core_lazy_get_db(Core $core)
62 {
63 return $core->connections['primary'];
64 }
65
66 /**
67 * Returns the ActiveRecord cache associated with the model.
68 *
69 * @param Model $model
70 *
71 * @return \ICanBoogie\ActiveRecord\RuntimeCache
72 */
73 static public function model_lazy_get_activerecord_cache(Model $model)
74 {
75 return new RunTimeActiveRecordCache($model);
76 }
77 }