-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Switch from using Pimple to a custom PSR-11 container
- Loading branch information
Showing
8 changed files
with
157 additions
and
140 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
<?php | ||
|
||
namespace Elazar\Flystream; | ||
|
||
use IteratorAggregate; | ||
use League\Flysystem\PathNormalizer; | ||
use League\Flysystem\UnixVisibility\PortableVisibilityConverter; | ||
use League\Flysystem\UnixVisibility\VisibilityConverter; | ||
use League\Flysystem\WhitespacePathNormalizer; | ||
use Psr\Container\ContainerInterface; | ||
use Psr\Log\LoggerInterface; | ||
use Psr\Log\NullLogger; | ||
use Traversable; | ||
|
||
class Container implements ContainerInterface, IteratorAggregate | ||
{ | ||
/** | ||
* @var callable[] | ||
*/ | ||
private array $entries; | ||
|
||
/** | ||
* @var object[] | ||
*/ | ||
private array $instances; | ||
|
||
public function __construct() | ||
{ | ||
$this->entries = [ | ||
FilesystemRegistry::class => fn() => new FilesystemRegistry, | ||
PassThruPathNormalizer::class => fn() => new PassThruPathNormalizer, | ||
StripProtocolPathNormalizer::class => fn() => new StripProtocolPathNormalizer( | ||
null, | ||
$this->get(WhitespacePathNormalizer::class), | ||
), | ||
PathNormalizer::class => fn() => $this->get(StripProtocolPathNormalizer::class), | ||
WhitespacePathNormalizer::class => fn() => new WhitespacePathNormalizer, | ||
PortableVisibilityConverter::class => fn() => new PortableVisibilityConverter, | ||
VisibilityConverter::class => fn() => $this->get(PortableVisibilityConverter::class), | ||
LockRegistryInterface::class => fn() => $this->get(LocalLockRegistry::class), | ||
LocalLockRegistry::class => fn() => new LocalLockRegistry, | ||
NullLogger::class => fn() => new NullLogger, | ||
LoggerInterface::class => fn() => $this->get(NullLogger::class), | ||
BufferInterface::class => fn() => $this->get(MemoryBuffer::class), | ||
MemoryBuffer::class => fn() => new MemoryBuffer, | ||
OverflowBuffer::class => fn() => new OverflowBuffer, | ||
FileBuffer::class => fn() => new FileBuffer, | ||
]; | ||
|
||
$this->instances = []; | ||
} | ||
|
||
public function get(string $id) | ||
{ | ||
if (isset($this->instances[$id])) { | ||
return $this->instances[$id]; | ||
} | ||
|
||
if (isset($this->entries[$id])) { | ||
return $this->instances[$id] = $this->entries[$id](); | ||
} | ||
|
||
throw FlystreamException::containerEntryNotFound($id); | ||
} | ||
|
||
public function has(string $id): bool | ||
{ | ||
return isset($this->entries[$id]); | ||
} | ||
|
||
public function set(string $class, string|object $instanceOrClass): void | ||
{ | ||
$this->instances[$class] = is_string($instanceOrClass) | ||
? $this->get($instanceOrClass) | ||
: $instanceOrClass; | ||
} | ||
|
||
public function getIterator(): Traversable | ||
{ | ||
foreach (array_keys($this->entries) as $id) { | ||
yield $id => $this->get($id); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
<?php | ||
|
||
use Elazar\Flystream\BufferInterface; | ||
use Elazar\Flystream\Container; | ||
use Elazar\Flystream\FileBuffer; | ||
use Elazar\Flystream\FlystreamException; | ||
use Elazar\Flystream\MemoryBuffer; | ||
|
||
it('can iterate, detect, and get default entries', function () { | ||
$container = new Container; | ||
$expectedDependencyCount = 15; | ||
$actualDependencyCount = 0; | ||
foreach ($container as $class => $instance) { | ||
$actualDependencyCount++; | ||
expect($container->has($class))->toBe(true); | ||
expect($container->get($class))->toBe($instance); | ||
} | ||
expect($actualDependencyCount)->toBe($expectedDependencyCount); | ||
}); | ||
|
||
it('throws an exception for an unknown key', function () { | ||
(new Container)->get('unknown-key'); | ||
})->throws(FlystreamException::class); | ||
|
||
it('can override a dependency using a class name', function () { | ||
$container = new Container; | ||
|
||
$default = $container->get(BufferInterface::class); | ||
expect($default)->toBeInstanceOf(MemoryBuffer::class); | ||
|
||
$container->set(BufferInterface::class, FileBuffer::class); | ||
|
||
$override = $container->get(BufferInterface::class); | ||
expect($override)->toBeInstanceOf(FileBuffer::class); | ||
}); | ||
|
||
it('can override a dependency using an instance', function () { | ||
$container = new Container; | ||
|
||
$buffer = new FileBuffer; | ||
$container->set(BufferInterface::class, $buffer); | ||
|
||
$override = $container->get(BufferInterface::class); | ||
expect($override)->toBe($buffer); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters