Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 10 additions & 8 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ env:
fail-fast: true

# PHP extensions required by Composer
EXTENSIONS: json, mbstring, pdo, pdo_mysql
EXTENSIONS: json, mbstring, pcov, pdo, pdo_mysql

permissions: { }
jobs:
Expand Down Expand Up @@ -62,9 +62,9 @@ jobs:
run: |
composer cs

# - name: "PHPStan"
# run: |
# composer analyze
- name: "PHPStan"
run: |
composer analyze

unit-tests:
needs: phpcs
Expand Down Expand Up @@ -114,16 +114,17 @@ jobs:
shell: bash
run: |
cp config/.env.ci .env
mkdir -p tests/_output/coverage/

- name: "Run Unit Tests"
- name: "Run Migrations"
if: always()
run: |
composer test-unit
composer migrate

- name: "Run Migrations"
- name: "Run Unit Tests"
if: always()
run: |
composer migrate
composer test-unit-coverage

- name: SonarCloud Scan
uses: SonarSource/sonarqube-scan-action@v5
Expand All @@ -140,3 +141,4 @@ jobs:
-Dsonar.sourceEncoding=UTF-8
-Dsonar.language=php
-Dsonar.tests=tests/
-Dsonar.php.coverage.reportPaths=tests/_output/cov.xml
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@
vendor
composer.lock
.env
tests/_output
4 changes: 3 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@
"cs": "vendor/bin/phpcs --standard=phpcs.xml",
"cs-fix": "vendor/bin/phpcbf --standard=phpcs.xml",
"migrate": "vendor/bin/phinx migrate",
"test-unit": "vendor/bin/phpunit -c phpunit.xml.dist --display-all-issues"
"test-unit": "vendor/bin/phpunit -c phpunit.xml.dist --display-all-issues",
"test-unit-coverage": "vendor/bin/phpunit -c phpunit.xml.dist --coverage-clover tests/_output/cov.xml --display-all-issues",
"test-unit-coverage-html": "vendor/bin/phpunit -c phpunit.xml.dist --coverage-html tests/_output/coverage --display-all-issues"
}
}
3 changes: 2 additions & 1 deletion phpunit.php
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
<?php

declare(strict_types=1);

# This file is part of Phalcon.
#
# (c) Phalcon Team <[email protected]>
#
# For the full copyright and license information, please view
# the LICENSE file that was distributed with this source code.

declare(strict_types=1);

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

Expand Down
27 changes: 27 additions & 0 deletions public/index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

declare(strict_types=1);

/**
* This file is part of the Phalcon API.
*
* (c) Phalcon Team <[email protected]>
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/


use Phalcon\Api\Domain\Services\Container;
use Phalcon\Api\Domain\Services\Environment\EnvManager;

require dirname(__DIR__) . '/vendor/autoload.php';

$container = new Container();

///** @var array<array-key, string> $providers */
//$providers = require_once EnvManager::appPath('/config/providers.php');

//$application = new Api($container, $providers);
//
//$application->setup()->run();
File renamed without changes.
248 changes: 248 additions & 0 deletions src/Domain/Services/Container.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,248 @@
<?php

/**
* This file is part of the Phalcon API.
*
* (c) Phalcon Team <[email protected]>
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/

declare(strict_types=1);

namespace Phalcon\Api\Domain\Services;

use Phalcon\Api\Domain\Services\Environment\EnvManager;
use Phalcon\Api\Domain\Services\Exceptions\InvalidConfigurationArguments;
use Phalcon\Cache\AdapterFactory;
use Phalcon\Cache\Cache;
use Phalcon\DataMapper\Pdo\Connection;
use Phalcon\Di\Di;
use Phalcon\Di\Service;
use Phalcon\Encryption\Security;
use Phalcon\Events\Manager as EventsManager;
use Phalcon\Filter\FilterFactory;
use Phalcon\Http\Message\Request;
use Phalcon\Http\Message\Response;
use Phalcon\Logger\Adapter\Stream;
use Phalcon\Logger\Logger;
use Phalcon\Mvc\Router;
use Phalcon\Storage\SerializerFactory;

use function array_merge;
use function sprintf;

class Container extends Di
{
/** @var string */
public const APPLICATION = 'application';
/** @var string */
public const CACHE = 'cache';
/** @var string */
public const CONNECTION = 'connection';
/** @var string */
public const EVENTS_MANAGER = 'eventsManager';
/** @var string */
public const FILTER = 'filter';
/** @var string */
public const LOGGER = 'logger';
/** @var string */
public const REQUEST = 'request';
/** @var string */
public const RESPONSE = 'response';
/** @var string */
public const ROUTER = 'router';
/** @var string */
public const SECURITY = 'security';
/** @var string */
public const TIME = 'time';

/**
* @throws InvalidConfigurationArguments
*/
public function __construct()
{
/** @var array<string, Service> $services */
$services = $this->services;

$this->services = array_merge(
[
self::LOGGER => $this->getServiceLogger(),
self::CACHE => $this->getServiceCache(),
self::CONNECTION => $this->getServiceConnection(),
self::EVENTS_MANAGER => $this->getServiceEventsManger(),
self::FILTER => $this->getServiceFilter(),
self::REQUEST => $this->getServiceRequest(),
self::RESPONSE => $this->getServiceResponse(),
self::ROUTER => $this->getServiceRouter(),
self::SECURITY => $this->getServiceSecurity(),
],
$services
);

parent::__construct();
}

/**
* @return Service
* @throws InvalidConfigurationArguments
*/
private function getServiceCache(): Service
{
$adapter = EnvManager::getString('CACHE_ADAPTER', 'redis');
$options = EnvManager::getCacheOptions();
return new Service(
function () use ($adapter, $options) {
return new Cache(
(new AdapterFactory(new SerializerFactory()))
->newInstance($adapter, $options)
);
},
true
);
}

/**
* @return Service
*/
private function getServiceConnection(): Service
{
return new Service(
function () {
$dbname = EnvManager::getString('DB_NAME', 'phalcon');
$host = EnvManager::getString('DB_HOST', 'rest-db');
$password = EnvManager::getString('DB_PASSWORD', 'secret');
$port = (int)EnvManager::get('DB_PORT', 3306);
$username = EnvManager::getString('DB_USER', 'phalcon');
$encoding = 'utf8';
$queries = ['SET NAMES utf8mb4'];
$dsn = sprintf(
"mysql:host=%s;dbname=%s;charset=%s;port=%s",
$host,
$dbname,
$encoding,
$port
);

return new Connection(
$dsn,
$username,
$password,
[],
$queries
);
},
true
);
}

/**
* @return Service
*/
private function getServiceEventsManger(): Service
{
return new Service(
function () {
$em = new EventsManager();
$em->enablePriorities(true);

return $em;
}
);
}

/**
* @return Service
*/
private function getServiceFilter(): Service
{
return new Service(
function () {
return (new FilterFactory())->newInstance();
},
true
);
}

/**
* @return Service
*/
private function getServiceLogger(): Service
{
return new Service(
function () {
$fileName = EnvManager::getString('USER_LOG_FILENAME', 'rest');
$logPath = EnvManager::getString('USER_LOG_PATH', 'storage/logs');
$logFile = EnvManager::appPath($logPath)
. '/' . $fileName . '.log';

return new Logger(
$fileName,
[
'main' => new Stream($logFile),
]
);
},
true
);
}

/**
* @return Service
*/
private function getServiceRequest(): Service
{
return new Service(
[
'className' => Request::class,
],
true
);
}

/**
* @return Service
*/
private function getServiceResponse(): Service
{
return new Service(
[
'className' => Response::class,
],
true
);
}

/**
* @return Service
*/
private function getServiceRouter(): Service
{
return new Service(
[
'className' => Router::class,
'arguments' => [
[
'type' => 'parameter',
'value' => false,
]
]
],
true
);
}

/**
* @return Service
*/
private function getServiceSecurity(): Service
{
return new Service(
[
'className' => Security::class,
],
true
);
}
}
33 changes: 33 additions & 0 deletions src/Domain/Services/Environment/Adapter/AdapterInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

/**
* This file is part of the Phalcon API.
*
* (c) Phalcon Team <[email protected]>
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/

declare(strict_types=1);

namespace Phalcon\Api\Domain\Services\Environment\Adapter;

use Phalcon\Api\Domain\Services\Environment\EnvManager;
use Phalcon\Api\Domain\Services\Exceptions\InvalidConfigurationArguments;

/**
* Interface for Env adapters
*
* @phpstan-import-type TSettings from EnvManager
*/
interface AdapterInterface
{
/**
* @param array<string, string> $options
*
* @return TSettings
* @throws InvalidConfigurationArguments
*/
public function load(array $options): array;
}
Loading
Loading