Skip to content

Commit

Permalink
Merge pull request #6 from young-steveo/separate-resolver
Browse files Browse the repository at this point in the history
Separate resolver into Codex library.
  • Loading branch information
young-steveo committed Jun 4, 2023
2 parents 8a38049 + 1d1477c commit 3c862af
Show file tree
Hide file tree
Showing 50 changed files with 534 additions and 435 deletions.
10 changes: 6 additions & 4 deletions src/Cabinet/Container.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,18 @@
namespace Arcanum\Cabinet;

use Psr\Container\ContainerInterface;
use Arcanum\Codex\ClassResolver;
use Arcanum\Codex\Resolver;

/**
* @implements \ArrayAccess<class-string, mixed>
*/
class Container implements \ArrayAccess, ContainerInterface
{
/**
* Resolver used to build classes.
* ClassResolver used to build classes.
*/
protected Resolver $resolver;
protected ClassResolver $resolver;

/**
* @var array<class-string, Provider>
Expand All @@ -24,7 +26,7 @@ class Container implements \ArrayAccess, ContainerInterface
/**
* Container uses a resolver to instantiate services.
*/
protected function __construct(Resolver $resolver = null)
protected function __construct(ClassResolver $resolver = null)
{
$this->resolver = $resolver ?? Resolver::forContainer($this);
}
Expand All @@ -40,7 +42,7 @@ public static function create(): self
/**
* Create a new container from a resolver.
*/
public static function fromResolver(Resolver $resolver): self
public static function fromResolver(ClassResolver $resolver): self
{
return new self($resolver);
}
Expand Down
22 changes: 0 additions & 22 deletions src/Cabinet/Event/CabinetEvent.php

This file was deleted.

35 changes: 0 additions & 35 deletions src/Cabinet/Event/ServiceRequested.php

This file was deleted.

35 changes: 0 additions & 35 deletions src/Cabinet/Event/ServiceResolved.php

This file was deleted.

13 changes: 0 additions & 13 deletions src/Cabinet/EventDispatcher.php

This file was deleted.

2 changes: 1 addition & 1 deletion src/Cabinet/NullProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ final class NullProvider extends Provider
/**
* Provide null
*/
public function __invoke(Container $container): mixed
public function __invoke(Container $container): object|null
{
return null;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Cabinet/PrototypeProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public static function fromFactory(\Closure $factory): static
/**
* Provide a service from the factory.
*/
public function __invoke(Container $container): mixed
public function __invoke(Container $container): object|null
{
return ($this->factory)($container);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Cabinet/Provider.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@ abstract class Provider
/**
* Invoking the provider should return the service.
*/
abstract public function __invoke(Container $container): mixed;
abstract public function __invoke(Container $container): object|null;
}
2 changes: 1 addition & 1 deletion src/Cabinet/SimpleProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public static function fromFactory(\Closure $factory): static
/**
* Provide a service from the factory.
*/
public function __invoke(Container $container): mixed
public function __invoke(Container $container): object|null
{
if (!isset($this->service)) {
$this->service = ($this->factory)($container);
Expand Down
23 changes: 23 additions & 0 deletions src/Codex/ClassResolver.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

declare(strict_types=1);

namespace Arcanum\Codex;

use Psr\Container\ContainerInterface;

interface ClassResolver
{
/**
* Resolve a class.
*
* @template T of object
* @param class-string<T>|(callable(ContainerInterface): T) $className
* @return T
* @throws Error\UnknownClass
* @throws Error\UnresolvableClass
* @throws Error\UnresolvablePrimitive
* @throws Error\UnresolvableUnionType
*/
public function resolve(string|callable $className, bool $isDependency = false): object;
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace Arcanum\Cabinet\Error;
namespace Arcanum\Codex\Error;

final class UnknownClass extends Unresolvable
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace Arcanum\Cabinet\Error;
namespace Arcanum\Codex\Error;

use Psr\Container\ContainerExceptionInterface;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace Arcanum\Cabinet\Error;
namespace Arcanum\Codex\Error;

final class UnresolvableClass extends Unresolvable
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace Arcanum\Cabinet\Error;
namespace Arcanum\Codex\Error;

final class UnresolvablePrimitive extends Unresolvable
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace Arcanum\Cabinet\Error;
namespace Arcanum\Codex\Error;

final class UnresolvableUnionType extends Unresolvable
{
Expand Down
35 changes: 35 additions & 0 deletions src/Codex/Event/ClassRequested.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

declare(strict_types=1);

namespace Arcanum\Codex\Event;

final class ClassRequested extends \Arcanum\Echo\Event implements CodexEvent
{
/**
* @param class-string $class
*/
public function __construct(private string $class)
{
}

/**
* Get the class name.
*
* @return class-string
*/
public function className(): string
{
return $this->class;
}

/**
* Get the class.
*
* Will return null if the class is not yet resolved.
*/
public function class(): object|null
{
return null;
}
}
35 changes: 35 additions & 0 deletions src/Codex/Event/ClassResolved.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

declare(strict_types=1);

namespace Arcanum\Codex\Event;

final class ClassResolved extends \Arcanum\Echo\Event implements CodexEvent
{
/**
* @param object $class
*/
public function __construct(private object $class)
{
}

/**
* Get the class name.
*
* @return class-string
*/
public function className(): string
{
return get_class($this->class);
}

/**
* Get the class.
*
* Will return null if the class is not yet resolved.
*/
public function class(): object|null
{
return $this->class;
}
}
22 changes: 22 additions & 0 deletions src/Codex/Event/CodexEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

declare(strict_types=1);

namespace Arcanum\Codex\Event;

interface CodexEvent
{
/**
* Get the class name.
*
* @return class-string
*/
public function className(): string;

/**
* Get the class.
*
* Will return null if the class is not yet resolved.
*/
public function class(): object|null;
}
13 changes: 13 additions & 0 deletions src/Codex/EventDispatcher.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

declare(strict_types=1);

namespace Arcanum\Codex;

/**
* If Arcanum\Codex\EventDispatcher is resolved by Codex,
* it will retain a reference to it and dispatch events to it.
*/
interface EventDispatcher extends \Psr\EventDispatcher\EventDispatcherInterface
{
}
Loading

0 comments on commit 3c862af

Please sign in to comment.