Skip to content

Commit f5341be

Browse files
authored
Merge pull request #11 from niden-code/T4-container
T4 container
2 parents b646d23 + b0b96da commit f5341be

File tree

21 files changed

+1631
-10
lines changed

21 files changed

+1631
-10
lines changed

.github/workflows/main.yml

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ env:
1919
fail-fast: true
2020

2121
# PHP extensions required by Composer
22-
EXTENSIONS: json, mbstring, pdo, pdo_mysql
22+
EXTENSIONS: json, mbstring, pcov, pdo, pdo_mysql
2323

2424
permissions: { }
2525
jobs:
@@ -62,9 +62,9 @@ jobs:
6262
run: |
6363
composer cs
6464
65-
# - name: "PHPStan"
66-
# run: |
67-
# composer analyze
65+
- name: "PHPStan"
66+
run: |
67+
composer analyze
6868
6969
unit-tests:
7070
needs: phpcs
@@ -114,16 +114,17 @@ jobs:
114114
shell: bash
115115
run: |
116116
cp config/.env.ci .env
117+
mkdir -p tests/_output/coverage/
117118
118-
- name: "Run Unit Tests"
119+
- name: "Run Migrations"
119120
if: always()
120121
run: |
121-
composer test-unit
122+
composer migrate
122123
123-
- name: "Run Migrations"
124+
- name: "Run Unit Tests"
124125
if: always()
125126
run: |
126-
composer migrate
127+
composer test-unit-coverage
127128
128129
- name: SonarCloud Scan
129130
uses: SonarSource/sonarqube-scan-action@v5
@@ -140,3 +141,4 @@ jobs:
140141
-Dsonar.sourceEncoding=UTF-8
141142
-Dsonar.language=php
142143
-Dsonar.tests=tests/
144+
-Dsonar.php.coverage.reportPaths=tests/_output/cov.xml

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@
33
vendor
44
composer.lock
55
.env
6+
tests/_output

composer.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@
5858
"cs": "vendor/bin/phpcs --standard=phpcs.xml",
5959
"cs-fix": "vendor/bin/phpcbf --standard=phpcs.xml",
6060
"migrate": "vendor/bin/phinx migrate",
61-
"test-unit": "vendor/bin/phpunit -c phpunit.xml.dist --display-all-issues"
61+
"test-unit": "vendor/bin/phpunit -c phpunit.xml.dist --display-all-issues",
62+
"test-unit-coverage": "vendor/bin/phpunit -c phpunit.xml.dist --coverage-clover tests/_output/cov.xml --display-all-issues",
63+
"test-unit-coverage-html": "vendor/bin/phpunit -c phpunit.xml.dist --coverage-html tests/_output/coverage --display-all-issues"
6264
}
6365
}

phpunit.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
<?php
22

3+
declare(strict_types=1);
4+
35
# This file is part of Phalcon.
46
#
57
# (c) Phalcon Team <[email protected]>
68
#
79
# For the full copyright and license information, please view
810
# the LICENSE file that was distributed with this source code.
911

10-
declare(strict_types=1);
1112

1213
ini_set('xdebug.mode', 'coverage');
1314

public/index.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* This file is part of the Phalcon API.
7+
*
8+
* (c) Phalcon Team <[email protected]>
9+
*
10+
* For the full copyright and license information, please view
11+
* the LICENSE file that was distributed with this source code.
12+
*/
13+
14+
15+
use Phalcon\Api\Domain\Services\Container;
16+
use Phalcon\Api\Domain\Services\Environment\EnvManager;
17+
18+
require dirname(__DIR__) . '/vendor/autoload.php';
19+
20+
$container = new Container();
21+
22+
///** @var array<array-key, string> $providers */
23+
//$providers = require_once EnvManager::appPath('/config/providers.php');
24+
25+
//$application = new Api($container, $providers);
26+
//
27+
//$application->setup()->run();
File renamed without changes.

src/Domain/Services/Container.php

Lines changed: 248 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,248 @@
1+
<?php
2+
3+
/**
4+
* This file is part of the Phalcon API.
5+
*
6+
* (c) Phalcon Team <[email protected]>
7+
*
8+
* For the full copyright and license information, please view
9+
* the LICENSE file that was distributed with this source code.
10+
*/
11+
12+
declare(strict_types=1);
13+
14+
namespace Phalcon\Api\Domain\Services;
15+
16+
use Phalcon\Api\Domain\Services\Environment\EnvManager;
17+
use Phalcon\Api\Domain\Services\Exceptions\InvalidConfigurationArguments;
18+
use Phalcon\Cache\AdapterFactory;
19+
use Phalcon\Cache\Cache;
20+
use Phalcon\DataMapper\Pdo\Connection;
21+
use Phalcon\Di\Di;
22+
use Phalcon\Di\Service;
23+
use Phalcon\Encryption\Security;
24+
use Phalcon\Events\Manager as EventsManager;
25+
use Phalcon\Filter\FilterFactory;
26+
use Phalcon\Http\Message\Request;
27+
use Phalcon\Http\Message\Response;
28+
use Phalcon\Logger\Adapter\Stream;
29+
use Phalcon\Logger\Logger;
30+
use Phalcon\Mvc\Router;
31+
use Phalcon\Storage\SerializerFactory;
32+
33+
use function array_merge;
34+
use function sprintf;
35+
36+
class Container extends Di
37+
{
38+
/** @var string */
39+
public const APPLICATION = 'application';
40+
/** @var string */
41+
public const CACHE = 'cache';
42+
/** @var string */
43+
public const CONNECTION = 'connection';
44+
/** @var string */
45+
public const EVENTS_MANAGER = 'eventsManager';
46+
/** @var string */
47+
public const FILTER = 'filter';
48+
/** @var string */
49+
public const LOGGER = 'logger';
50+
/** @var string */
51+
public const REQUEST = 'request';
52+
/** @var string */
53+
public const RESPONSE = 'response';
54+
/** @var string */
55+
public const ROUTER = 'router';
56+
/** @var string */
57+
public const SECURITY = 'security';
58+
/** @var string */
59+
public const TIME = 'time';
60+
61+
/**
62+
* @throws InvalidConfigurationArguments
63+
*/
64+
public function __construct()
65+
{
66+
/** @var array<string, Service> $services */
67+
$services = $this->services;
68+
69+
$this->services = array_merge(
70+
[
71+
self::LOGGER => $this->getServiceLogger(),
72+
self::CACHE => $this->getServiceCache(),
73+
self::CONNECTION => $this->getServiceConnection(),
74+
self::EVENTS_MANAGER => $this->getServiceEventsManger(),
75+
self::FILTER => $this->getServiceFilter(),
76+
self::REQUEST => $this->getServiceRequest(),
77+
self::RESPONSE => $this->getServiceResponse(),
78+
self::ROUTER => $this->getServiceRouter(),
79+
self::SECURITY => $this->getServiceSecurity(),
80+
],
81+
$services
82+
);
83+
84+
parent::__construct();
85+
}
86+
87+
/**
88+
* @return Service
89+
* @throws InvalidConfigurationArguments
90+
*/
91+
private function getServiceCache(): Service
92+
{
93+
$adapter = EnvManager::getString('CACHE_ADAPTER', 'redis');
94+
$options = EnvManager::getCacheOptions();
95+
return new Service(
96+
function () use ($adapter, $options) {
97+
return new Cache(
98+
(new AdapterFactory(new SerializerFactory()))
99+
->newInstance($adapter, $options)
100+
);
101+
},
102+
true
103+
);
104+
}
105+
106+
/**
107+
* @return Service
108+
*/
109+
private function getServiceConnection(): Service
110+
{
111+
return new Service(
112+
function () {
113+
$dbname = EnvManager::getString('DB_NAME', 'phalcon');
114+
$host = EnvManager::getString('DB_HOST', 'rest-db');
115+
$password = EnvManager::getString('DB_PASSWORD', 'secret');
116+
$port = (int)EnvManager::get('DB_PORT', 3306);
117+
$username = EnvManager::getString('DB_USER', 'phalcon');
118+
$encoding = 'utf8';
119+
$queries = ['SET NAMES utf8mb4'];
120+
$dsn = sprintf(
121+
"mysql:host=%s;dbname=%s;charset=%s;port=%s",
122+
$host,
123+
$dbname,
124+
$encoding,
125+
$port
126+
);
127+
128+
return new Connection(
129+
$dsn,
130+
$username,
131+
$password,
132+
[],
133+
$queries
134+
);
135+
},
136+
true
137+
);
138+
}
139+
140+
/**
141+
* @return Service
142+
*/
143+
private function getServiceEventsManger(): Service
144+
{
145+
return new Service(
146+
function () {
147+
$em = new EventsManager();
148+
$em->enablePriorities(true);
149+
150+
return $em;
151+
}
152+
);
153+
}
154+
155+
/**
156+
* @return Service
157+
*/
158+
private function getServiceFilter(): Service
159+
{
160+
return new Service(
161+
function () {
162+
return (new FilterFactory())->newInstance();
163+
},
164+
true
165+
);
166+
}
167+
168+
/**
169+
* @return Service
170+
*/
171+
private function getServiceLogger(): Service
172+
{
173+
return new Service(
174+
function () {
175+
$fileName = EnvManager::getString('USER_LOG_FILENAME', 'rest');
176+
$logPath = EnvManager::getString('USER_LOG_PATH', 'storage/logs');
177+
$logFile = EnvManager::appPath($logPath)
178+
. '/' . $fileName . '.log';
179+
180+
return new Logger(
181+
$fileName,
182+
[
183+
'main' => new Stream($logFile),
184+
]
185+
);
186+
},
187+
true
188+
);
189+
}
190+
191+
/**
192+
* @return Service
193+
*/
194+
private function getServiceRequest(): Service
195+
{
196+
return new Service(
197+
[
198+
'className' => Request::class,
199+
],
200+
true
201+
);
202+
}
203+
204+
/**
205+
* @return Service
206+
*/
207+
private function getServiceResponse(): Service
208+
{
209+
return new Service(
210+
[
211+
'className' => Response::class,
212+
],
213+
true
214+
);
215+
}
216+
217+
/**
218+
* @return Service
219+
*/
220+
private function getServiceRouter(): Service
221+
{
222+
return new Service(
223+
[
224+
'className' => Router::class,
225+
'arguments' => [
226+
[
227+
'type' => 'parameter',
228+
'value' => false,
229+
]
230+
]
231+
],
232+
true
233+
);
234+
}
235+
236+
/**
237+
* @return Service
238+
*/
239+
private function getServiceSecurity(): Service
240+
{
241+
return new Service(
242+
[
243+
'className' => Security::class,
244+
],
245+
true
246+
);
247+
}
248+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
/**
4+
* This file is part of the Phalcon API.
5+
*
6+
* (c) Phalcon Team <[email protected]>
7+
*
8+
* For the full copyright and license information, please view
9+
* the LICENSE file that was distributed with this source code.
10+
*/
11+
12+
declare(strict_types=1);
13+
14+
namespace Phalcon\Api\Domain\Services\Environment\Adapter;
15+
16+
use Phalcon\Api\Domain\Services\Environment\EnvManager;
17+
use Phalcon\Api\Domain\Services\Exceptions\InvalidConfigurationArguments;
18+
19+
/**
20+
* Interface for Env adapters
21+
*
22+
* @phpstan-import-type TSettings from EnvManager
23+
*/
24+
interface AdapterInterface
25+
{
26+
/**
27+
* @param array<string, string> $options
28+
*
29+
* @return TSettings
30+
* @throws InvalidConfigurationArguments
31+
*/
32+
public function load(array $options): array;
33+
}

0 commit comments

Comments
 (0)