Skip to content

Commit

Permalink
Merge branch 'refs/heads/v6.0.x' into paginator
Browse files Browse the repository at this point in the history
# Conflicts:
#	composer.lock
  • Loading branch information
noone-silent committed Aug 1, 2024
2 parents 057901c + 9a2b4e0 commit 85be6de
Show file tree
Hide file tree
Showing 63 changed files with 4,554 additions and 75 deletions.
28 changes: 19 additions & 9 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,35 +4,45 @@
"keywords": [
"php",
"framework",
"psr-3",
"psr-4",
"psr-7",
"psr-15",
"psr-17"
"psr-11",
"psr-12",
"psr-16"
],
"license": "MIT",
"require": {
"php": ">=8.1 <9.0",
"ext-apcu": "*",
"ext-igbinary": "*",
"ext-json": "*",
"ext-fileinfo": "*",
"ext-json": "*",
"ext-mbstring": "*",
"ext-pdo": "*",
"ext-redis": "*",
"ext-xml": "*",
"payload-interop/payload-interop": "^1.0",
"phalcon/traits": "^2.0",
"phpbench/phpbench": "^1.2"
"psr/cache": "^3.0",
"psr/container": "^2.0",
"psr/log": "^3.0"
},
"suggest": {
"ext-apcu": "to use Cache\\Adapter\\Apcu, Storage\\Adapter\\Apcu",
"ext-gd": "to use Image\\Adapter\\Gd",
"ext-igbinary": "to use Storage\\Serializer\\Igbinary",
"ext-imagick": "to use Image\\Adapter\\Imagick",
"ext-memcached": "to use Cache\\Adapter\\Libmemcached, Storage\\Adapter\\Libmemcached",
"ext-memcached": "to use Cache\\Adapter\\Libmemcached, Session\\Adapter\\Libmemcached, Storage\\Adapter\\Libmemcached",
"ext-openssl": "to use Encryption\\Crypt",
"ext-redis": "to use Cache\\Adapter\\Redis, Session\\Adapter\\Redis, Storage\\Adapter\\Redis",
"ext-yaml": "to use Config\\Adapter\\Yaml"
},
"autoload": {
"psr-4": {
"Phalcon\\": "src/",
"Phalcon\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"Phalcon\\Tests\\Benchmarks\\": "tests/benchmarks/",
"Phalcon\\Tests\\Cli\\": "tests/cli/",
"Phalcon\\Tests\\Database\\": "tests/database/",
Expand Down Expand Up @@ -74,9 +84,9 @@
"codeception/module-redis": "^3.0",
"friendsofphp/php-cs-fixer": "^3.13",
"pds/skeleton": "^1.0",
"phpbench/phpbench": "^1.2",
"phpstan/phpstan": "^1.10",
"squizlabs/php_codesniffer": "^3.7",
"vimeo/psalm": "^5.4",
"vlucas/phpdotenv": "^5.5"
},
"scripts": {
Expand Down
115 changes: 87 additions & 28 deletions src/Container/Container.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,57 +7,116 @@
*
* For the full copyright and license information, please view the LICENSE.txt
* file that was distributed with this source code.
*
* Implementation of this file has been influenced by CapsulePHP
*
* @link https://github.com/capsulephp/di
* @license https://github.com/capsulephp/di/blob/3.x/LICENSE.md
*/

declare(strict_types=1);

namespace Phalcon\Container;

use Phalcon\Di\DiInterface;
use Phalcon\Container\Definitions\AbstractDefinition;
use Phalcon\Container\Definitions\Definitions;
use Phalcon\Container\Interfaces\ProviderInterface;
use Phalcon\Container\Traits\ArgumentsTrait;
use Psr\Container\ContainerInterface;
use ReflectionClass;

use function class_exists;
use function interface_exists;

/**
* Wrapper for `Phalcon\Di\Di`
*
* A full lazy loading autoloader will be implemented
* Dependency injection container.
*/
class Container implements ContainerInterface
{
use ArgumentsTrait;

/**
* @var DiInterface
* @var array
*/
protected DiInterface $container;
protected array $has = [];

/**
* Phalcon\Container constructor
*
* @param DiInterface $container
* @var array
*/
public function __construct(DiInterface $container)
{
$this->container = $container;
}
protected array $registry = [];

/**
* Return the service
*
* @param string $id
*
* @return mixed
* @param Definitions $definitions
* @param ProviderInterface[] $providers
*/
public function get(string $id)
public function __construct(
protected Definitions $definitions,
iterable $providers = []
) {
foreach ($providers as $provider) {
$provider->provide($this->definitions);
}

$this->registry[static::class] = $this;
}

public function callableGet(string $id): callable
{
return $this->container->getShared($id);
return function () use ($id) {
return $this->get($id);
};
}

public function callableNew(string $id): callable
{
return function () use ($id) {
return $this->new($id);
};
}

public function get(string $id): mixed
{
if (!isset($this->registry[$id])) {
$this->registry[$id] = $this->new($id);
}

return $this->registry[$id];
}

/**
* Whether a service exists or not in the container
*
* @param string $id
*
* @return bool
*/
public function has(string $id): bool
{
return $this->container->has($id);
if (!isset($this->has[$id])) {
$this->has[$id] = $this->find($id);
}

return $this->has[$id];
}

public function new(string $id): mixed
{
return $this->resolveArgument($this, $this->definitions->$id);
}

protected function find(string $id): bool
{
if (!isset($this->definitions->$id)) {
return $this->findImplicit($id);
}

if ($this->definitions->$id instanceof AbstractDefinition) {
return $this->definitions->$id->isInstantiable($this);
}

return true;
}

protected function findImplicit(string $id): bool
{
if (!class_exists($id) && !interface_exists($id)) {
return false;
}

$reflection = new ReflectionClass($id);
return $reflection->isInstantiable();
}
}
38 changes: 0 additions & 38 deletions src/Container/ContainerInterface.php

This file was deleted.

111 changes: 111 additions & 0 deletions src/Container/Definitions/AbstractDefinition.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
<?php

/**
* This file is part of the Phalcon Framework.
*
* (c) Phalcon Team <[email protected]>
*
* For the full copyright and license information, please view the LICENSE.txt
* file that was distributed with this source code.
*
* Implementation of this file has been influenced by CapsulePHP
*
* @link https://github.com/capsulephp/di
* @license https://github.com/capsulephp/di/blob/3.x/LICENSE.md
*/

declare(strict_types=1);

namespace Phalcon\Container\Definitions;

use Phalcon\Container\Container;
use Phalcon\Container\Exception\NotInstantiated;
use Phalcon\Container\Lazy\AbstractLazy;
use Throwable;

abstract class AbstractDefinition extends AbstractLazy
{
/**
* @var string|null
*/
protected ?string $class = null;
/**
* @var mixed|null
*/
protected mixed /* callable */
$factory = null;
/**
* @var string
*/
protected string $id;
/**
* @var bool
*/
protected bool $isInstantiable = false;

/**
* @param Container $container
*
* @return mixed
* @throws NotInstantiated
*/
public function __invoke(Container $container): mixed
{
return $this->new($container);
}

/**
* @param callable $factory
*
* @return $this
*/
public function factory(callable $factory): static
{
$this->factory = $factory;

return $this;
}

/**
* @param Container $container
*
* @return bool
*/
public function isInstantiable(Container $container): bool
{
if ($this->factory !== null) {
return true;
}

if ($this->class !== null) {
return $container->has($this->class);
}

return $this->isInstantiable;
}

/**
* @param Container $container
*
* @return object
* @throws NotInstantiated
*/
public function new(Container $container): object
{
try {
return $this->instantiate($container);
} catch (Throwable $ex) {
throw new NotInstantiated(
'Could not instantiate ' . $this->id,
previous: $ex
);
}
}

/**
* @param Container $container
*
* @return object
*/
abstract protected function instantiate(Container $container): object;
}
Loading

0 comments on commit 85be6de

Please sign in to comment.