Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add get server #23

Closed
wants to merge 11 commits into from
Closed
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
5 changes: 3 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,15 @@
},
"autoload-dev": {
"psr-4": {
"Utopia\\Tests\\": "tests/Platform"
"Utopia\\Tests\\": "tests/Platform",
"Utopia\\Unit\\": "tests/unit"
}
},
"require": {
"php": ">=8.0",
"ext-json": "*",
"ext-redis": "*",
"utopia-php/framework": "0.*.*",
"utopia-php/framework": "0.33.*",
"utopia-php/cli": "0.15.*"
},
"require-dev": {
Expand Down
158 changes: 83 additions & 75 deletions composer.lock

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
stopOnFailure="false"
>
<testsuites>
<testsuite name="unit">
<directory>./tests/unit</directory>
</testsuite>
<testsuite name="Application Test Suite">
<file>./tests/e2e/Client.php</file>
<directory>./tests/</directory>
Expand Down
87 changes: 87 additions & 0 deletions src/Platform/Module.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
<?php

namespace Utopia\Platform;

use Exception;

abstract class Module
{
protected array $services = [
'all' => [],
Service::TYPE_TASK => [],
Service::TYPE_HTTP => [],
Service::TYPE_GRAPHQL => [],
Service::TYPE_WORKER => [],
];

/**
* Add Service
*
* @param string $key
* @param Service $service
* @return Platform
*/
public function addService(string $key, Service $service): self
{
$this->services['all'][$key] = $service;
$this->services[$service->getType()][$key] = $service;

return $this;
}

/**
* Remove Service
*
* @param string $key
* @return Platform
*/
public function removeService(string $key): self
{
$service = $this->services['all'][$key] ?? null;
if (empty($service)) {
return $this;
}
$type = $service->getType();
unset($this->services['all'][$key]);
unset($this->services[$type][$key]);

return $this;
}

/**
* Get Service
*
* @param string $key
* @return Service|null
*/
public function getService(string $key): ?Service
{
$service = $this->services['all'][$key] ?? null;
if (empty($service)) {
throw new Exception('Service '.$key.' not found');
}

return $service;
}

/**
* Get Services
*
* @return array
*/
public function getServices(): array
{
return $this->services['all'];
}

/**
* Get services by type
*
* @param string $type
* @return array<Service>
*/
public function getServicesByType(string $type): array
{
return $this->services[$type] ?? [];
}
}
14 changes: 14 additions & 0 deletions src/Platform/Platform.php
Original file line number Diff line number Diff line change
Expand Up @@ -319,4 +319,18 @@ public function setWorker(Server $worker): self

return $this;
}

/**
* Get env
*
* Method for querying env parameters. If $key is not found $default value will be returned.
*
* @param string $key
* @param string|null $default
* @return mixed
*/
public function getEnv(string $key, string $default = null): mixed
{
return $_SERVER[$key] ?? $default;
}
}
2 changes: 2 additions & 0 deletions src/Platform/Service.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ abstract class Service

public const TYPE_GRAPHQL = 'GraphQL';

public const TYPE_TASK = 'Task';

public const TYPE_CLI = 'CLI';

public const TYPE_WORKER = 'Worker';
Expand Down
9 changes: 9 additions & 0 deletions tests/Platform/TestModule.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

namespace Utopia\Tests;

use Utopia\Platform\Module;

class TestModule extends Module
{
}
14 changes: 14 additions & 0 deletions tests/unit/GetEnvTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace Utopia\Unit;

use PHPUnit\Framework\TestCase;

class GetEnvTest extends TestCase
{
public function testGetEnv()
{
$platform = new Mock();
$this->assertEquals(3, $platform->getEnv('argc'));
}
}
15 changes: 15 additions & 0 deletions tests/unit/Mock.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace Utopia\Unit;

use Utopia\Platform\Platform;
use Utopia\Tests\TestModule;

class Mock extends Platform
{
public function __construct()
{
$module = new TestModule();
parent::__construct($module);
}
}
Loading