-
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.
Add buffer factory, refactor StreamWrapper DI into PhpStreamWrapper, …
…remove ServiceLocator
- Loading branch information
Showing
13 changed files
with
178 additions
and
123 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<?php | ||
|
||
namespace Elazar\Flystream; | ||
|
||
class BufferFactory implements BufferFactoryInterface | ||
{ | ||
private function __construct( | ||
private BufferInterface|string $bufferInstanceOrClassFqcn | ||
) { } | ||
|
||
public static function fromInstance(BufferInterface $instance) | ||
{ | ||
return new static($instance); | ||
} | ||
|
||
public static function fromClass(string $classFqcn) | ||
{ | ||
if (!class_exists($classFqcn)) { | ||
throw FlystreamException::bufferClassNotFound($classFqcn); | ||
} | ||
if (!is_a($classFqcn, BufferInterface::class, true)) { | ||
throw FlystreamException::bufferClassMissingInterface($classFqcn); | ||
} | ||
return new static($classFqcn); | ||
} | ||
|
||
public function createBuffer(): BufferInterface | ||
{ | ||
return $this->bufferInstanceOrClassFqcn instanceof BufferInterface | ||
? clone $this->bufferInstanceOrClassFqcn | ||
: new $this->bufferInstanceOrClassFqcn; | ||
} | ||
} |
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,8 @@ | ||
<?php | ||
|
||
namespace Elazar\Flystream; | ||
|
||
interface BufferFactoryInterface | ||
{ | ||
public function createBuffer(): BufferInterface; | ||
} |
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
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,27 @@ | ||
<?php | ||
|
||
namespace Elazar\Flystream; | ||
|
||
use League\Flysystem\UnixVisibility\VisibilityConverter; | ||
use Psr\Log\LoggerInterface; | ||
|
||
class PhpStreamWrapper extends StreamWrapper | ||
{ | ||
private static Container $container; | ||
|
||
public static function setContainer(Container $container): void | ||
{ | ||
static::$container = $container; | ||
} | ||
|
||
public function __construct() | ||
{ | ||
parent::__construct( | ||
static::$container->get(VisibilityConverter::class), | ||
static::$container->get(FilesystemRegistry::class), | ||
static::$container->get(LockRegistryInterface::class), | ||
static::$container->get(BufferFactoryInterface::class), | ||
static::$container->get(LoggerInterface::class) | ||
); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
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,38 @@ | ||
<?php | ||
|
||
use Elazar\Flystream\BufferFactory; | ||
use Elazar\Flystream\FlystreamException; | ||
use Elazar\Flystream\OverflowBuffer; | ||
|
||
it('creates a buffer from a buffer instance', function () { | ||
$maxMemory = 1024 ** 2; | ||
$instance = new OverflowBuffer($maxMemory); | ||
$factory = BufferFactory::fromInstance($instance); | ||
$buffer = $factory->createBuffer(); | ||
expect($buffer) | ||
->toBeInstanceOf(OverflowBuffer::class) | ||
->not->toBe($instance); | ||
expect($buffer->getMaxMemory())->toBe($maxMemory); | ||
}); | ||
|
||
it('creates a buffer from a class', function () { | ||
$factory = BufferFactory::fromClass(OverflowBuffer::class); | ||
$buffer = $factory->createBuffer(); | ||
expect($buffer)->toBeInstanceOf(OverflowBuffer::class); | ||
}); | ||
|
||
it('does not create a buffer from a nonexistent class', function () { | ||
BufferFactory::fromClass('NonexistentClass'); | ||
})->throws( | ||
FlystreamException::class, | ||
null, | ||
FlystreamException::CODE_BUFFER_CLASS_NOT_FOUND | ||
); | ||
|
||
it('does not create a buffer from a non-buffer class', function () { | ||
BufferFactory::fromClass('stdClass'); | ||
})->throws( | ||
FlystreamException::class, | ||
null, | ||
FlystreamException::CODE_BUFFER_CLASS_MISSING_INTERFACE | ||
); |
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
Oops, something went wrong.