Skip to content

Improve code quality and ensure compatibility with PHP 8.1+ #3

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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
42 changes: 15 additions & 27 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,14 @@ Container, Factories and dependency injectors will help to make your PHP code mo
Containers allowing you to easily create and retrieve objects that are needed throughout your application.
```php
use MaplePHP\Container\Container;

$container = new Container();
$container->set("YourClass", \YourNamespace\To\YourClass::class); // Bind "YourClass" to container and dependency injector
$yourClass = $container->get("YourClass")->get(); // Will return "YourClass"
//$yourClass->yourClassMehthod();

// You can set mixed values in the container
$container->set("hasEmail", true);

$hasEmail = $container->get("hasEmail")->get();
var_dump($hasEmail); // Result in: (bool) true
```
If the constructor of "YourClass" contains unresolved class arguments, the dependency injector will attempt to automatically locate them for you. Read more under the headline **dependency injector**.

Expand All @@ -40,32 +44,16 @@ Take a look at this example

```php

$container->set("YourClass", \YourNamespace\To\YourClass::class);
$testService = $container->get("YourClass");
echo $testService->start();

```
The above code will load **YourClass** and auto initialize the class **Test**.

```php
namespace YourNamespace\To;

use YourNamespace\ToTestClasses\Test;

class YourClass {

private $test;
use MaplePHP\Container\Container;
use MaplePHP\Container\Autowire;
use App\Services\MailService;

// Dependency injector will auto load "Test" class and the "Test" classes and so on.
function __construct(Test $test) {
$this->test = $test;
}
$container = new Container();
$container->set("MailService", new Autowire(MailService::class));

function start() {
return $this->test->get("This is the start page");
}

}
// This will return "MailService" with all dependencies resolved on constructor
$mailService = $container->get("MailService")->get();
echo $mailService->send();
```

## Event handler
Expand Down
6 changes: 5 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,12 @@
"maplephp/dto": "^3.0"
},
"autoload": {
"files": [
"src/Setup/aliases.php"
],
"psr-4": {
"MaplePHP\\Container\\": ""
"Psr\\Container\\": "src/Interfaces/",
"MaplePHP\\Container\\": "src"
}
},
"minimum-stability": "dev"
Expand Down
76 changes: 76 additions & 0 deletions src/Autowire.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<?php

namespace MaplePHP\Container;

use MaplePHP\Container\Interfaces\AutowireInterface;

class Autowire implements AutowireInterface
{

private Reflection $reflect;
/**
* @var mixed|object|null
*/
private mixed $class = null;
private bool $disableDI = false;

/**
* @param string $class
* @throws \ReflectionException
*/
public function __construct(string $class)
{
if (is_string($class) && class_exists($class)) {
$this->reflect = new Reflection($class);
} else {
throw new \InvalidArgumentException("The class {$class} does not exist.");
}
}

/**
* Disable the dependency injector
*
* Note: This method MUST be implemented in such a way as to retain the immutability
*
* @return $this
*/
public function disableDI(): self
{
$inst = clone $this;
$inst->disableDI = true;
return $inst;
}

/**
* Pass custom arguments to the class constructor
*
* Note: This method MUST be implemented in such a way as to retain the immutability
* NOTE: This will disable the dependency injector
*
* @param array $args
* @return $this
*/
public function addArgs(array $args): self
{
if(count($args) <= 0) {
throw new \InvalidArgumentException("You must provide at least one argument.");
}
$inst = $this->disableDI();
$inst->reflect->setArgs($args);
return $inst;
}

/**
* Run the class with all dependencies.
*
* @return mixed
* @throws \ReflectionException
*/
public function run(): mixed
{
if (is_null($this->class)) {
$this->class = ($this->disableDI) ? $this->reflect->get() : $this->reflect->dependencyInjector();
}
return $this->class;
}
}
24 changes: 12 additions & 12 deletions Container.php → src/Container.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
namespace MaplePHP\Container;

use Closure;
use MaplePHP\Container\Interfaces\ContainerInterface;
use MaplePHP\Container\Interfaces\AutowireInterface;
use Psr\Container\ContainerInterface;
use MaplePHP\Container\Interfaces\FactoryInterface;
use MaplePHP\DTO\Format\Arr;
//use MaplePHP\Container\Reflection;
Expand Down Expand Up @@ -112,27 +113,26 @@ public function isContainer(string $identifier): bool

/**
* Get a container or factory
* @param string $identifier [description]
* @param array $args Is possible to overwrite/add __construct or method argumnets
* @return mixed
* @template T of object
* @param class-string<T>|string $identifier The class or service identifier
* @param string $identifier
* @param array $args Is possible to overwrite/add __construct or method arguments
* @return T
* @throws ReflectionException
*/
public function get(string $identifier, array $args = []): mixed
{
if ($service = $this->getService($identifier)) {
$service = $this->getService($identifier);
if (isset($service)) {
if (count($args) === 0) {
$args = $this->getArgs($identifier);
}
if ($this->isFactory($identifier)) {
$this->getter[$identifier] = $service(...$args);
} else {
if (empty($this->getter[$identifier])) {
if (is_string($service) && class_exists($service)) {
$reflect = new Reflection($service);
if (count($args) > 0) {
$reflect->setArgs($args);
}
$this->getter[$identifier] = $reflect->get();
if (!isset($this->getter[$identifier])) {
if ($service instanceof AutowireInterface) {
$this->getter[$identifier] = $service->run();
} else {
$this->getter[$identifier] = $service;
}
Expand Down
12 changes: 6 additions & 6 deletions EventHandler.php → src/EventHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,18 +51,18 @@ public function addHandler(object|string $handler, string|array $method = null):
public function addEvent(callable|object|string $event, ?string $bind = null): void
{

if(!is_callable($event)) {
if(is_string($event)) {
if (!is_callable($event)) {
if (is_string($event)) {
$reflect = new Reflection($event);
$event = $reflect->get();
}

if(is_object($event) && !($event instanceof EventInterface)) {
if (is_object($event) && !($event instanceof EventInterface)) {
throw new Exception("Event object/class needs to be instance of \"EventInterface\"!", 1);
}
}

if (is_null($bind)) {
if ($bind === null) {
$this->event[] = $event;
} else {
$this->event[] = [$bind => $event];
Expand Down Expand Up @@ -95,7 +95,7 @@ public function __call(string $method, array $args): mixed
throw new BadMethodCallException("The method \"".$method."\" does not exist in the class (" . $handler[0]::class . ")", 1);
}
*/
if (is_null($handler[1][0]) || in_array($method, $handler[1])) {
if ($handler[1][0] === null || in_array($method, $handler[1])) {
$this->bindable[$method] = $method;
}
$data = call_user_func_array([$handler[0], $method], $args);
Expand Down Expand Up @@ -136,7 +136,7 @@ final protected function triggerEvents(): void
*/
final protected function getEvent(callable|object $data): void
{
if(is_callable($data)) {
if (is_callable($data)) {
$data();
} else {
$data->resolve();
Expand Down
File renamed without changes.
34 changes: 34 additions & 0 deletions src/Interfaces/AutowireInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

namespace MaplePHP\Container\Interfaces;

interface AutowireInterface
{
/**
* Disable the dependency injector
*
* Note: This method MUST be implemented in such a way as to retain the immutability
*
* @return $this
*/
public function disableDI(): self;

/**
* Pass custom arguments to the class constructor
*
* Note: This method MUST be implemented in such a way as to retain the immutability
* Note: This will disable the dependency injector
*
* @param array $args
* @return $this
*/
public function addArgs(array $args): self;

/**
* Run the class with all dependencies.
*
* @return mixed
* @throws \ReflectionException
*/
public function run(): mixed;
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace MaplePHP\Container\Interfaces;
namespace Psr\Container;

/**
* Describes the interface of a container that exposes methods to read its entries.
Expand Down
File renamed without changes.
File renamed without changes.
Loading