Skip to content

Commit

Permalink
add enhancements
Browse files Browse the repository at this point in the history
  • Loading branch information
ArrayIterator committed Oct 19, 2023
1 parent e3faebc commit 4a8e207
Show file tree
Hide file tree
Showing 17 changed files with 850 additions and 209 deletions.
2 changes: 2 additions & 0 deletions src/Console/Command/ChecksumGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
use function basename;
use function clearstatcache;
use function date;
use function dirname;
use function error_clear_last;
Expand Down Expand Up @@ -250,6 +251,7 @@ static function ($e) {
continue;
}
$hash = $printMode === 'sha1' ? $sha1Sum : $md5Sum;
clearstatcache(true, $realPath);
$output->writeln(
sprintf(
'<info>[%s]</info> <comment>%s</comment> %s',
Expand Down
56 changes: 34 additions & 22 deletions src/Container/ContainerResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,10 @@ public function getContainer(): ContainerInterface
}

/**
* @param callable|array|mixed $callable
* @template T of object
* @param callable|array|class-string<T>|mixed $callable
* @param array $arguments
* @return array|mixed
* @throws ContainerFrozenException
* @throws ContainerNotFoundException
* @return array|T|mixed
* @throws Throwable
*/
public function resolveCallable(mixed $callable, array $arguments = []): mixed
Expand Down Expand Up @@ -174,14 +173,16 @@ public function resolveArguments(
ReflectionClass|ReflectionFunctionAbstract $reflection,
$arguments
): array {
// resolver empty arguments when auto resolve enabled
if (!empty($arguments)) {
return $arguments;
}
$reflectionName = $reflection->getName();
$reflection = $reflection instanceof ReflectionClass
? $reflection->getConstructor()
: $reflection;

// resolver empty arguments when auto resolve enabled
/*if (!empty($arguments) && count($arguments) === $reflection->getNumberOfRequiredParameters()) {
return $arguments;
}*/

$parameters = $reflection?->getParameters()??[];
$container = $this->getContainer();
$containerParameters = method_exists($container, 'getParameters')
Expand All @@ -191,7 +192,9 @@ public function resolveArguments(
$containerAliases = method_exists($container, 'getAliases')
? $container->getAliases()
: [];
$paramArguments = [];
foreach ($parameters as $parameter) {
$argumentName = $parameter->getName();
$type = $parameter->getType();
if ($type instanceof ReflectionUnionType) {
foreach ($type->getTypes() as $unionType) {
Expand All @@ -214,28 +217,37 @@ public function resolveArguments(
if (!$type instanceof ReflectionNamedType
|| $type->isBuiltin()
) {
if (isset($arguments[$parameter->getName()])) {
$paramArguments[$argumentName] = $containerParameters[$parameter->getName()];
continue;
}
if (array_key_exists($parameter->getName(), $containerParameters)) {
$arguments[] = $containerParameters[$parameter->getName()];
$paramArguments[$argumentName] = $containerParameters[$parameter->getName()];
continue;
}

if ($parameter->isDefaultValueAvailable()) {
$arguments[] = $parameter->getDefaultValue();
$paramArguments[$argumentName] = $parameter->getDefaultValue();
continue;
}
if ($parameter->allowsNull()) {
$arguments[] = null;
$paramArguments[$argumentName] = null;
continue;
}
$arguments = [];
$paramArguments = [];
break;
}

if (isset($arguments[$parameter->getName()])) {
$paramArguments[$argumentName] = $arguments[$parameter->getName()];
continue;
}

$name = $type->getName();
if ($name === ContainerInterface::class
|| is_a($name, __CLASS__)
) {
$arguments[] = $container->has($name)
$paramArguments[$argumentName] = $container->has($name)
? $container->get($name)
: $container;
continue;
Expand All @@ -244,7 +256,7 @@ public function resolveArguments(
&& isset($containerAliases[$name])
&& $container->has($containerAliases[$name])
) {
$arguments[] = $container->get($containerAliases[$name]);
$paramArguments[$argumentName] = $container->get($containerAliases[$name]);
continue;
}

Expand All @@ -254,32 +266,32 @@ public function resolveArguments(
if (is_string($param) && $container->has($param)) {
$param = $container->get($param);
}
$arguments[] = $param;
$paramArguments[$argumentName] = $param;
continue;
}
if ($parameter->isDefaultValueAvailable()) {
$arguments[] = $parameter->getDefaultValue();
$paramArguments[$argumentName] = $parameter->getDefaultValue();
continue;
}
if ($parameter->allowsNull()) {
$arguments[] = null;
$paramArguments[$argumentName] = null;
continue;
}
$arguments = [];
$paramArguments = [];
break;
}
$arguments[] = $container->get($name);
$paramArguments[$argumentName] = $container->get($name);
}
if (($required = $reflection?->getNumberOfRequiredParameters()??0) > count($arguments)) {
if (($required = $reflection?->getNumberOfRequiredParameters()??0) > count($paramArguments)) {
throw new UnResolveAbleException(
sprintf(
'Could not resolve required arguments for : %s. Required %d argument, but %d given',
$reflectionName,
$required,
count($arguments)
count($paramArguments)
)
);
}
return $arguments;
return $paramArguments;
}
}
12 changes: 10 additions & 2 deletions src/Database/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
use ArrayAccess\TrayDigita\Container\Interfaces\ContainerIndicateInterface;
use ArrayAccess\TrayDigita\Database\Wrapper\DriverWrapper;
use ArrayAccess\TrayDigita\Database\Wrapper\EntityManagerWrapper;
use ArrayAccess\TrayDigita\Event\Interfaces\ManagerAllocatorInterface;
use ArrayAccess\TrayDigita\Event\Interfaces\ManagerInterface;
use ArrayAccess\TrayDigita\Traits\Manager\ManagerAllocatorTrait;
use ArrayAccess\TrayDigita\Traits\Manager\ManagerDispatcherTrait;
use ArrayAccess\TrayDigita\Util\Filter\Consolidation;
use ArrayAccess\TrayDigita\Util\Filter\ContainerHelper;
Expand Down Expand Up @@ -42,9 +45,10 @@
/**
* @mixin DoctrineConnection
*/
class Connection implements ContainerIndicateInterface
class Connection implements ContainerIndicateInterface, ManagerAllocatorInterface
{
use ManagerDispatcherTrait;
use ManagerDispatcherTrait,
ManagerAllocatorTrait;

private ?DoctrineConnection $connection = null;

Expand All @@ -71,6 +75,10 @@ public function __construct(
$this->config ??= ContainerHelper::use(Config::class, $this->container)??new Config();
$this->eventManager = $eventManager??new EventManager();
$this->defaultConfiguration = $configuration??new OrmConfiguration();
$manager = ContainerHelper::getNull(ManagerInterface::class, $this->container);
if ($manager) {
$this->setManager($manager);
}
}

protected function getPrefixNameEventIdentity(): ?string
Expand Down
23 changes: 11 additions & 12 deletions src/Database/Wrapper/ConnectionWrapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,17 @@
namespace ArrayAccess\TrayDigita\Database\Wrapper;

use ArrayAccess\TrayDigita\Database\Connection;
use ArrayAccess\TrayDigita\Event\Interfaces\ManagerIndicateInterface;
use ArrayAccess\TrayDigita\Event\Interfaces\ManagerInterface;
use ArrayAccess\TrayDigita\Traits\Manager\ManagerDispatcherTrait;
use ArrayAccess\TrayDigita\Util\Filter\ContainerHelper;
use Doctrine\DBAL\Driver\Connection as DoctrineConnection;
use Doctrine\DBAL\Driver\Middleware\AbstractConnectionMiddleware;
use Doctrine\DBAL\Driver\Result;
use Doctrine\DBAL\Driver\Statement;
use Throwable;

class ConnectionWrapper extends AbstractConnectionMiddleware
class ConnectionWrapper extends AbstractConnectionMiddleware implements ManagerIndicateInterface
{
use ManagerDispatcherTrait;

Expand All @@ -28,19 +30,13 @@ protected function getPrefixNameEventIdentity(): ?string
return 'connection';
}

protected function getManagerFromContainer(): ?ManagerInterface

public function getManager(): ?ManagerInterface
{
$container = $this->databaseConnection->getContainer();
try {
$manager = $container->has(ManagerInterface::class)
? $container->get(ManagerInterface::class)
: null;
} catch (Throwable) {
$manager = null;
}
return $manager instanceof ManagerInterface ? $manager : null;
return $this->databaseConnection->getManager();
}


public function prepare(string $sql): Statement
{
// @dispatch(connection.queryString)
Expand All @@ -56,7 +52,10 @@ public function prepare(string $sql): Statement
$this->databaseConnection
);

$prepared = parent::prepare($sql);
$prepared = new StatementWrapper(
$this,
parent::prepare($sql)
);

// @dispatch(connection.prepare)
$this->dispatchCurrent(
Expand Down
61 changes: 38 additions & 23 deletions src/Database/Wrapper/DriverWrapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
namespace ArrayAccess\TrayDigita\Database\Wrapper;

use ArrayAccess\TrayDigita\Database\Connection;
use ArrayAccess\TrayDigita\Event\Interfaces\ManagerIndicateInterface;
use ArrayAccess\TrayDigita\Event\Interfaces\ManagerInterface;
use ArrayAccess\TrayDigita\Traits\Manager\ManagerDispatcherTrait;
use ArrayAccess\TrayDigita\Traits\Service\CallStackTraceTrait;
use ArrayAccess\TrayDigita\Util\Filter\Conversion;
use DateTimeZone;
Expand All @@ -21,9 +23,10 @@
use function preg_match;
use function trim;

final class DriverWrapper extends AbstractDriverMiddleware
final class DriverWrapper extends AbstractDriverMiddleware implements ManagerIndicateInterface
{
use CallStackTraceTrait;
use CallStackTraceTrait,
ManagerDispatcherTrait;

public function __construct(
private readonly Connection $databaseConnection,
Expand All @@ -32,32 +35,27 @@ public function __construct(
parent::__construct($wrappedDriver);
}

protected function getPrefixNameEventIdentity(): ?string
{
return 'connection';
}

public function getDatabaseConnection(): Connection
{
return $this->databaseConnection;
}

public function getManager(): ?ManagerInterface
{
return $this->databaseConnection->getManager();
}

public function connect(#[SensitiveParameter] array $params) : DoctrineConnection
{
$this->assertCallstack();

$container = $this->databaseConnection->getContainer();
try {
$manager = $container->has(ManagerInterface::class)
? $container->get(ManagerInterface::class)
: null;
} catch (Throwable) {
$manager = null;
}
$manager = $manager instanceof ManagerInterface ? $manager : null;

// @dispatch(connection.beforeConnect)
$manager?->dispatch(
'connection.beforeConnect',
$params,
$this->databaseConnection,
$this,
);
$this->dispatchBefore($params, $this->databaseConnection);

try {
$connection = new ConnectionWrapper(
Expand All @@ -67,8 +65,7 @@ public function connect(#[SensitiveParameter] array $params) : DoctrineConnectio

$this->initConnection($connection);
// @dispatch(connection.connect)
$manager?->dispatch(
'connection.connect',
$this->dispatchCurrent(
$params,
$this->databaseConnection,
$this,
Expand All @@ -78,8 +75,7 @@ public function connect(#[SensitiveParameter] array $params) : DoctrineConnectio
} finally {
$this->resetCallstack();
// @dispatch(connection.afterConnect)
$manager?->dispatch(
'connection.afterConnect',
$this->dispatchAfter(
$params,
$this->databaseConnection,
$this,
Expand All @@ -90,6 +86,8 @@ public function connect(#[SensitiveParameter] array $params) : DoctrineConnectio

private function initConnection(Driver\Connection $connection): void
{
// @dispatch(connection.beforeInitConnection)
$this->dispatchBefore($connection, $this->databaseConnection);
$platform = $this->wrappedDriver->getDatabasePlatform();
$query = '';
$config = $this->databaseConnection->getDatabaseConfig();
Expand Down Expand Up @@ -148,8 +146,25 @@ private function initConnection(Driver\Connection $connection): void
$query = "SET SESSION TIME_ZONE='$timezone';";
}
try {
$query && $connection->exec($query);
if ($query) {
$result = $connection->exec($query);
}
// @dispatch(connection.initConnection)
$this->dispatchCurrent(
$connection,
$this->databaseConnection,
$query,
$result??null
);
} catch (Throwable) {
} finally {
// @dispatch(connection.afterInitConnection)
$this->dispatchAfter(
$connection,
$this->databaseConnection,
$query,
$result??null
);
}
}
}
Loading

0 comments on commit 4a8e207

Please sign in to comment.