Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"scriptfusion/retry": "^1.1",
"scriptfusion/retry-exception-handlers": "^1",
"eloquent/enumeration": "^5",
"psr/container": "^1",
"psr/cache": "^1",
"zendframework/zend-uri": "^2"
},
Expand Down
89 changes: 22 additions & 67 deletions src/Porter.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php
namespace ScriptFUSION\Porter;

use Psr\Container\ContainerInterface;
use ScriptFUSION\Porter\Cache\CacheAdvice;
use ScriptFUSION\Porter\Cache\CacheToggle;
use ScriptFUSION\Porter\Cache\CacheUnavailableException;
Expand All @@ -23,7 +24,7 @@
class Porter
{
/**
* @var Provider[]
* @var ContainerInterface
*/
private $providers;

Expand All @@ -32,6 +33,16 @@ class Porter
*/
private $providerFactory;

/**
* Initializes this instance with the specified container of providers.
*
* @param ContainerInterface $providers Container of providers.
*/
public function __construct(ContainerInterface $providers)
{
$this->providers = $providers;
}

/**
* Imports data according to the design of the specified import specification.
*
Expand All @@ -47,7 +58,7 @@ public function import(ImportSpecification $specification)

$records = $this->fetch(
$specification->getResource(),
$specification->getProviderTag(),
$specification->getProviderName(),
$specification->getCacheAdvice(),
$specification->getMaxFetchAttempts(),
$specification->getFetchExceptionHandler()
Expand Down Expand Up @@ -90,12 +101,12 @@ public function importOne(ImportSpecification $specification)

private function fetch(
ProviderResource $resource,
$providerTag,
$providerName,
CacheAdvice $cacheAdvice,
$fetchAttempts,
$fetchExceptionHandler
) {
$provider = $this->getProvider($resource->getProviderClassName(), $providerTag);
$provider = $this->getProvider($providerName ?: $resource->getProviderClassName());

$this->applyCacheAdvice($provider, $cacheAdvice);

Expand Down Expand Up @@ -189,81 +200,25 @@ private function applyCacheAdvice(Provider $provider, CacheAdvice $cacheAdvice)
}

/**
* Registers the specified provider optionally identified by the specified tag.
*
* @param Provider $provider Provider.
* @param string|null $tag Optional. Provider tag.
*
* @return $this
*
* @throws ProviderAlreadyRegisteredException The specified provider is already registered.
*/
public function registerProvider(Provider $provider, $tag = null)
{
if ($this->hasProvider($name = get_class($provider), $tag)) {
throw new ProviderAlreadyRegisteredException("Provider already registered: \"$name\" with tag \"$tag\".");
}

$this->providers[$this->hashProviderName($name, $tag)] = $provider;

return $this;
}

/**
* Gets the provider matching the specified class name and optionally a tag.
* Gets the provider matching the specified name.
*
* @param string $name Provider class name.
* @param string|null $tag Optional. Provider tag.
* @param string $name Provider name.
*
* @return Provider
*
* @throws ProviderNotFoundException The specified provider was not found.
*/
public function getProvider($name, $tag = null)
private function getProvider($name)
{
if ($this->hasProvider($name, $tag)) {
return $this->providers[$this->hashProviderName($name, $tag)];
if ($this->providers->has($name)) {
return $this->providers->get($name);
}

try {
// Tags are not supported for lazy-loaded providers because every instance would be the same.
if ($tag === null) {
$this->registerProvider($provider = $this->getOrCreateProviderFactory()->createProvider("$name"));

return $provider;
}
return $this->getOrCreateProviderFactory()->createProvider("$name");
} catch (ObjectNotCreatedException $exception) {
// We will throw our own exception.
throw new ProviderNotFoundException("No such provider registered: \"$name\".", $exception);
}

throw new ProviderNotFoundException(
"No such provider registered: \"$name\" with tag \"$tag\".",
isset($exception) ? $exception : null
);
}

/**
* Gets a value indicating whether the specified provider is registered.
*
* @param string $name Provider class name.
* @param string|null $tag Optional. Provider tag.
*
* @return bool True if the specified provider is registered, otherwise false.
*/
public function hasProvider($name, $tag = null)
{
return isset($this->providers[$this->hashProviderName($name, $tag)]);
}

/**
* @param string $name Provider class name.
* @param string|null $tag Provider tag.
*
* @return string Provider identifier hash.
*/
private function hashProviderName($name, $tag)
{
return "$name#$tag";
}

private function getOrCreateProviderFactory()
Expand Down
5 changes: 0 additions & 5 deletions src/Provider/Resource/StaticResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,6 @@ public function getProviderClassName()
return StaticDataProvider::class;
}

public function getProviderTag()
{
return;
}

public function fetch(Connector $connector, EncapsulatedOptions $options = null)
{
return $this->data;
Expand Down
20 changes: 11 additions & 9 deletions src/Specification/ImportSpecification.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class ImportSpecification
/**
* @var string
*/
private $providerTag;
private $providerName;

/**
* @var Transformer[]
Expand Down Expand Up @@ -86,30 +86,32 @@ final public function getResource()
}

/**
* Gets the provider identifier tag.
* Gets the provider name.
*
* @return string Provider tag.
* @return string Provider name.
*/
final public function getProviderTag()
final public function getProviderName()
{
return $this->providerTag;
return $this->providerName;
}

/**
* Sets the provider identifier tag.
* Sets the provider name.
*
* @param string $tag Provider tag.
* @param string $tag Provider name.
*
* @return $this
*/
final public function setProviderTag($tag)
final public function setProviderName($tag)
{
$this->providerTag = "$tag";
$this->providerName = "$tag";

return $this;
}

/**
* Gets the ordered list of transformers.
*
* @return Transformer[]
*/
final public function getTransformers()
Expand Down
Loading