Merge pull request #9223 from nupplaphil/task/process_class
[friendica.git/.git] / static / dependencies.config.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2020, Friendica
4  *
5  * @license GNU AGPL version 3 or any later version
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU Affero General Public License as
9  * published by the Free Software Foundation, either version 3 of the
10  * License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU Affero General Public License for more details.
16  *
17  * You should have received a copy of the GNU Affero General Public License
18  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
19  *
20  * The configuration defines "complex" dependencies inside Friendica
21  * So this classes shouldn't be simple or their dependencies are already defined here.
22  *
23  * This kind of dependencies are NOT required to be defined here:
24  *   - $a = new ClassA(new ClassB());
25  *   - $a = new ClassA();
26  *   - $a = new ClassA(Configuration $configuration);
27  *
28  * This kind of dependencies SHOULD be defined here:
29  *   - $a = new ClassA();
30  *     $b = $a->create();
31  *
32  *   - $a = new ClassA($creationPassedVariable);
33  *
34  */
35
36 use Dice\Dice;
37 use Friendica\App;
38 use Friendica\Core\Cache;
39 use Friendica\Core\Config;
40 use Friendica\Core\L10n;
41 use Friendica\Core\Lock\ILock;
42 use Friendica\Core\Process;
43 use Friendica\Core\Session\ISession;
44 use Friendica\Core\StorageManager;
45 use Friendica\Database\Database;
46 use Friendica\Factory;
47 use Friendica\Model\Storage\IStorage;
48 use Friendica\Model\User\Cookie;
49 use Friendica\Network;
50 use Friendica\Util;
51 use Psr\Log\LoggerInterface;
52
53 return [
54         '*'                             => [
55                 // marks all class result as shared for other creations, so there's just
56                 // one instance for the whole execution
57                 'shared' => true,
58         ],
59         '$basepath'                     => [
60                 'instanceOf'      => Util\BasePath::class,
61                 'call'            => [
62                         ['getPath', [], Dice::CHAIN_CALL],
63                 ],
64                 'constructParams' => [
65                         dirname(__FILE__, 2),
66                         $_SERVER
67                 ]
68         ],
69         Util\BasePath::class         => [
70                 'constructParams' => [
71                         dirname(__FILE__, 2),
72                         $_SERVER
73                 ]
74         ],
75         Util\ConfigFileLoader::class => [
76                 'shared'          => true,
77                 'constructParams' => [
78                         [Dice::INSTANCE => '$basepath'],
79                 ],
80         ],
81         Config\Cache::class          => [
82                 'instanceOf' => Factory\ConfigFactory::class,
83                 'call'       => [
84                         ['createCache', [], Dice::CHAIN_CALL],
85                 ],
86         ],
87         App\Mode::class              => [
88                 'call' => [
89                         ['determineRunMode', [true, $_SERVER], Dice::CHAIN_CALL],
90                         ['determine', [], Dice::CHAIN_CALL],
91                 ],
92         ],
93         Config\IConfig::class                   => [
94                 'instanceOf' => Factory\ConfigFactory::class,
95                 'call'       => [
96                         ['createConfig', [], Dice::CHAIN_CALL],
97                 ],
98         ],
99         \Friendica\Core\PConfig\IPConfig::class => [
100                 'instanceOf' => Factory\ConfigFactory::class,
101                 'call'       => [
102                         ['createPConfig', [], Dice::CHAIN_CALL],
103                 ]
104         ],
105         Database::class                         => [
106                 'constructParams' => [
107                         [Dice::INSTANCE => \Psr\Log\NullLogger::class],
108                         $_SERVER,
109                 ],
110         ],
111         /**
112          * Creates the App\BaseURL
113          *
114          * Same as:
115          *   $baseURL = new App\BaseURL($configuration, $_SERVER);
116          */
117         App\BaseURL::class             => [
118                 'constructParams' => [
119                         $_SERVER,
120                 ],
121         ],
122         App\Page::class => [
123                 'constructParams' => [
124                         [Dice::INSTANCE => '$basepath'],
125                 ],
126         ],
127         /**
128          * Create a Logger, which implements the LoggerInterface
129          *
130          * Same as:
131          *   $loggerFactory = new Factory\LoggerFactory();
132          *   $logger = $loggerFactory->create($channel, $configuration, $profiler);
133          *
134          * Attention1: We can use DICE for detecting dependencies inside "chained" calls too
135          * Attention2: The variable "$channel" is passed inside the creation of the dependencies per:
136          *    $app = $dice->create(App::class, [], ['$channel' => 'index']);
137          *    and is automatically passed as an argument with the same name
138          */
139         LoggerInterface::class          => [
140                 'instanceOf' => Factory\LoggerFactory::class,
141                 'constructParams' => [
142                         'index',
143                 ],
144                 'call'       => [
145                         ['create', ['index'], Dice::CHAIN_CALL],
146                 ],
147         ],
148         '$devLogger'                    => [
149                 'instanceOf' => Factory\LoggerFactory::class,
150                 'constructParams' => [
151                         'dev',
152                 ],
153                 'call'       => [
154                         ['createDev', [], Dice::CHAIN_CALL],
155                 ]
156         ],
157         Cache\ICache::class             => [
158                 'instanceOf' => Factory\CacheFactory::class,
159                 'call'       => [
160                         ['create', [], Dice::CHAIN_CALL],
161                 ],
162         ],
163         Cache\IMemoryCache::class       => [
164                 'instanceOf' => Factory\CacheFactory::class,
165                 'call'       => [
166                         ['create', [], Dice::CHAIN_CALL],
167                 ],
168         ],
169         ILock::class                    => [
170                 'instanceOf' => Factory\LockFactory::class,
171                 'call'       => [
172                         ['create', [], Dice::CHAIN_CALL],
173                 ],
174         ],
175         App\Arguments::class => [
176                 'instanceOf' => App\Arguments::class,
177                 'call' => [
178                         ['determine', [$_SERVER, $_GET], Dice::CHAIN_CALL],
179                 ],
180         ],
181         App\Module::class => [
182                 'instanceOf' => App\Module::class,
183                 'call' => [
184                         ['determineModule', [], Dice::CHAIN_CALL],
185                 ],
186         ],
187         Process::class => [
188                 'constructParams' => [
189                         [Dice::INSTANCE => '$basepath'],
190                         getmypid(),
191                 ],
192         ],
193         App\Router::class => [
194                 'constructParams' => [
195                         $_SERVER,
196                         __DIR__ . '/routes.config.php',
197                         null
198                 ],
199         ],
200         L10n::class => [
201                 'constructParams' => [
202                         $_SERVER, $_GET
203                 ],
204         ],
205         ISession::class => [
206                 'instanceOf' => Factory\SessionFactory::class,
207                 'call' => [
208                         ['createSession', [$_SERVER], Dice::CHAIN_CALL],
209                         ['start', [], Dice::CHAIN_CALL],
210                 ],
211         ],
212         Cookie::class => [
213                 'constructParams' => [
214                         $_SERVER, $_COOKIE
215                 ],
216         ],
217         IStorage::class => [
218                 'instanceOf' => StorageManager::class,
219                 'call' => [
220                         ['getBackend', [], Dice::CHAIN_CALL],
221                 ],
222         ],
223         Network\IHTTPRequest::class => [
224                 'instanceOf' => Network\HTTPRequest::class,
225         ]
226 ];