Skip to content

Commit 8fb915b

Browse files
committed
move DI & Logger from core package to this one
1 parent 3be8452 commit 8fb915b

23 files changed

+1070
-2
lines changed

README.md

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,23 @@
1-
# common
2-
Commonly needed services
1+
# DevNet Common
2+
This dependency is the common services needed for **DevNet Application**, which includes the following Base Class Library:
3+
4+
- Configuration Provider
5+
- Dependency Injection
6+
- Event Logging
7+
8+
## Requirements
9+
- [PHP](https://www.php.net/) version 8.1 or higher
10+
- [Composer](https://getcomposer.org/) version 2.0 or higher
11+
12+
## Installation
13+
You can install DevNet Core as a third-party library to work with any PHP project, by running the following command in the terminal:
14+
15+
```bash
16+
composer require devnet/common
17+
```
18+
19+
## Documentation
20+
Full documentation on how to use **DevNet Framework** is available at [devnet-framework.github.io](https://devnet-framework.github.io)
21+
22+
## License
23+
This library is licensed under the MIT license. See [License File](https://github.com/DevNet-Framework/core/blob/master/LICENSE) in the root folder for more information.

composer.json

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
{
2+
"name": "devnet/common",
3+
"description": "The DevNet services that are commonly needed",
4+
"type": "library",
5+
"license": "MIT",
6+
"authors": [
7+
{
8+
"name": "Mohammed Moussaoui",
9+
"email": "[email protected]"
10+
}
11+
],
12+
"require": {
13+
"devnet/core": "1.0.*"
14+
},
15+
"autoload": {
16+
"psr-4": {
17+
"DevNet\\Common\\": "src/"
18+
},
19+
"files": [
20+
"global.php"
21+
]
22+
}
23+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php
2+
3+
/**
4+
* @author Mohammed Moussaoui
5+
* @license MIT license. For more license information, see the LICENSE file in the root directory.
6+
* @link https://github.com/DevNet-Framework
7+
*/
8+
9+
namespace DevNet\Common\Configuration;
10+
11+
class ConfigurationBuilder
12+
{
13+
private ?string $basePath = null;
14+
private array $settings = [];
15+
16+
public function __construct(array $settings = [])
17+
{
18+
$this->settings = $settings;
19+
}
20+
21+
public function setBasePath(string $basePath): void
22+
{
23+
$this->basePath = $basePath;
24+
}
25+
26+
public function addSetting(string $key, $value): void
27+
{
28+
$this->settings[$key] = $value;
29+
}
30+
31+
public function addJsonFile(string $path): void
32+
{
33+
if ($this->basePath) {
34+
$path = $this->basePath . "/" . $path;
35+
}
36+
37+
if (!file_exists($path)) {
38+
throw new \Exception("Not found file {$path}");
39+
}
40+
41+
$settings = file_get_contents($path);
42+
$settings = json_decode($settings, true);
43+
$this->settings = array_merge($this->settings, $settings);
44+
}
45+
46+
public function build(): IConfiguration
47+
{
48+
return new ConfigurationRoot($this->settings);
49+
}
50+
}
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
<?php
2+
3+
/**
4+
* @author Mohammed Moussaoui
5+
* @license MIT license. For more license information, see the LICENSE file in the root directory.
6+
* @link https://github.com/DevNet-Framework
7+
*/
8+
9+
namespace DevNet\Common\Configuration;
10+
11+
use DevNet\System\PropertyTrait;
12+
13+
class ConfigurationRoot implements IConfiguration
14+
{
15+
use PropertyTrait;
16+
17+
private array $settings = [];
18+
19+
public function __construct(array $settings = [])
20+
{
21+
$this->settings = $settings;
22+
}
23+
24+
public function get_Settings(): array
25+
{
26+
return $this->settings;
27+
}
28+
29+
public function getValue(string $key)
30+
{
31+
$keys = explode(":", $key);
32+
$value = $this->settings;
33+
34+
foreach ($keys as $key) {
35+
$value = $value[$key] ?? null;
36+
37+
if ($value == null) {
38+
return null;
39+
}
40+
}
41+
42+
if (is_array($value)) {
43+
return null;
44+
}
45+
46+
return $value;
47+
}
48+
49+
public function getSection(string $key): IConfiguration
50+
{
51+
return new ConfigurationSection($this, $key);
52+
}
53+
54+
public function getChildren(string $key = ''): array
55+
{
56+
$path = $key;
57+
$keys = explode(":", $key);
58+
59+
$settings = $this->settings;
60+
61+
foreach ($keys as $key) {
62+
$settings = $settings[$key] ?? null;
63+
64+
if (!is_array($settings)) {
65+
return [];
66+
}
67+
}
68+
69+
$children = [];
70+
$keys = array_keys($settings);
71+
72+
foreach ($keys as $key) {
73+
if ($path != '') {
74+
$key = $path . ":" . $key;
75+
}
76+
77+
$children[] = new ConfigurationSection($this, $key);
78+
}
79+
80+
return $children;
81+
}
82+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
/**
4+
* @author Mohammed Moussaoui
5+
* @license MIT license. For more license information, see the LICENSE file in the root directory.
6+
* @link https://github.com/DevNet-Framework
7+
*/
8+
9+
namespace DevNet\Common\Configuration;
10+
11+
class ConfigurationSection implements IConfiguration
12+
{
13+
private IConfiguration $root;
14+
private string $path;
15+
16+
public function __construct(IConfiguration $root, string $path)
17+
{
18+
$this->root = $root;
19+
$this->path = $path;
20+
}
21+
22+
public function __get($name)
23+
{
24+
return $this->$name;
25+
}
26+
27+
public function getValue(string $key)
28+
{
29+
return $this->root->getValue($this->path . ":" . $key);
30+
}
31+
32+
public function getSection(string $key): IConfiguration
33+
{
34+
return new ConfigurationSection($this->root, $this->path . ":" . $key);
35+
}
36+
37+
public function getChildren(): array
38+
{
39+
return $this->root->getChildren($this->path);
40+
}
41+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
/**
4+
* @author Mohammed Moussaoui
5+
* @license MIT license. For more license information, see the LICENSE file in the root directory.
6+
* @link https://github.com/DevNet-Framework
7+
*/
8+
9+
namespace DevNet\Common\Configuration;
10+
11+
interface IConfiguration
12+
{
13+
public function getValue(string $key);
14+
15+
public function getSection(string $key): IConfiguration;
16+
17+
public function getChildren(): array;
18+
}

src/Dependency/Activator.php

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
3+
/**
4+
* @author Mohammed Moussaoui
5+
* @license MIT license. For more license information, see the LICENSE file in the root directory.
6+
* @link https://github.com/DevNet-Framework
7+
*/
8+
9+
namespace DevNet\Common\Dependency;
10+
11+
use DevNet\System\Exceptions\ClassException;
12+
use ReflectionClass;
13+
14+
class Activator
15+
{
16+
public static function CreateInstance(string $type, IServiceProvider $provider = null): object
17+
{
18+
if (!class_exists($type)) {
19+
throw new ClassException("Cound not find class {$type}", 0, 1);
20+
}
21+
22+
$classInfo = new ReflectionClass($type);
23+
$methodInfo = $classInfo->getConstructor();
24+
25+
if (!$methodInfo || !$provider) {
26+
return $classInfo->newInstance();
27+
}
28+
29+
$parameters = $methodInfo->getParameters();
30+
$arguments = [];
31+
32+
foreach ($parameters as $parameter) {
33+
$parameterType = '';
34+
if ($parameter->getType()) {
35+
$parameterType = $parameter->getType()->getName();
36+
}
37+
38+
if (!$provider->contains($parameterType)) {
39+
break;
40+
}
41+
42+
$arguments[] = $provider->getService($parameterType);
43+
}
44+
45+
return $classInfo->newInstanceArgs($arguments);
46+
}
47+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
/**
4+
* @author Mohammed Moussaoui
5+
* @license MIT license. For more license information, see the LICENSE file in the root directory.
6+
* @link https://github.com/DevNet-Framework
7+
*/
8+
9+
namespace DevNet\Common\Dependency;
10+
11+
use IteratorAggregate;
12+
13+
interface IServiceCollection extends IteratorAggregate
14+
{
15+
public function add(ServiceDescriptor $serviceDescriptor): void;
16+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
/**
4+
* @author Mohammed Moussaoui
5+
* @license MIT license. For more license information, see the LICENSE file in the root directory.
6+
* @link https://github.com/DevNet-Framework
7+
*/
8+
9+
namespace DevNet\Common\Dependency;
10+
11+
interface IServiceProvider
12+
{
13+
/**
14+
* Find and return entry of the provider by its service type or throw an exception if not found.
15+
*/
16+
public function getService(string $serviceType);
17+
18+
/**
19+
* Check if the provider can return an entry for the given serviceType.
20+
*/
21+
public function contains(string $serviceType);
22+
}

0 commit comments

Comments
 (0)