Skip to content

Commit

Permalink
Improved the juggling of the rewrite config
Browse files Browse the repository at this point in the history
  • Loading branch information
qzminski committed Nov 28, 2017
1 parent f6bd584 commit 99d021b
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 38 deletions.
23 changes: 5 additions & 18 deletions src/ConfigProvider/BundleConfigProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,6 @@ class BundleConfigProvider implements ConfigProviderInterface
*/
private $entries = [];

/**
* @var string
*/
private $key = 'bundle';

/**
* BundleConfigProvider constructor.
*
Expand All @@ -40,14 +35,6 @@ public function __construct(array $entries = [])
*/
public function find(string $id): ?RewriteConfigInterface
{
list($key, $id) = explode(':', $id);

// Return if the key is not supported
if ($key !== $this->key) {
return null;
}

// Return if the entry does not exist
if (!array_key_exists($id, $this->entries)) {
return null;
}
Expand All @@ -67,7 +54,7 @@ public function findAll(): array
$configs = [];

foreach ($this->entries as $id => $entry) {
if (($config = $this->createConfig($id, $entry)) !== null) {
if (($config = $this->createConfig((string) $id, $entry)) !== null) {
$configs[] = $config;
}
}
Expand All @@ -78,18 +65,18 @@ public function findAll(): array
/**
* Create the config.
*
* @param int $id
* @param array $data
* @param string $id
* @param array $data
*
* @return null|RewriteConfig
*/
private function createConfig(int $id, array $data): ?RewriteConfig
private function createConfig(string $id, array $data): ?RewriteConfig
{
if (!isset($data['request']['path'], $data['response']['code'])) {
return null;
}

$config = new RewriteConfig($this->key.':'.$id, $data['request']['path'], (int) $data['response']['code']);
$config = new RewriteConfig($id, $data['request']['path'], (int) $data['response']['code']);

// Request hosts
if (isset($data['request']['hosts'])) {
Expand Down
25 changes: 23 additions & 2 deletions src/ConfigProvider/ChainConfigProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,11 @@ public function addProvider(ConfigProviderInterface $provider): void
*/
public function find(string $id): ?RewriteConfigInterface
{
list($class, $id) = explode(':', $id);

/** @var ConfigProviderInterface $provider */
foreach ($this->providers as $provider) {
if (($config = $provider->find($id)) !== null) {
if ($class === $this->getProviderIdentifier($provider) && ($config = $provider->find($id)) !== null) {
return $config;
}
}
Expand All @@ -53,9 +55,28 @@ public function findAll(): array

/** @var ConfigProviderInterface $provider */
foreach ($this->providers as $provider) {
$configs = array_merge($configs, $provider->findAll());
$providerConfigs = $provider->findAll();

/** @var RewriteConfigInterface $config */
foreach ($providerConfigs as $config) {
$config->setIdentifier($this->getProviderIdentifier($provider) . ':' . $config->getIdentifier());
}

$configs = array_merge($configs, $providerConfigs);
}

return $configs;
}

/**
* Get the provider identifier
*
* @param ConfigProviderInterface $provider
*
* @return string
*/
private function getProviderIdentifier(ConfigProviderInterface $provider): string
{
return get_class($provider);
}
}
19 changes: 6 additions & 13 deletions src/ConfigProvider/DatabaseConfigProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,6 @@ class DatabaseConfigProvider implements ConfigProviderInterface
*/
private $connection;

/**
* @var string
*/
private $key = 'database';

/**
* DatabaseConfigProvider constructor.
*
Expand All @@ -43,13 +38,6 @@ public function __construct(Connection $connection)
*/
public function find(string $id): ?RewriteConfigInterface
{
list($key, $id) = explode(':', $id);

// Return if the key is not supported
if ($key !== $this->key) {
return null;
}

try {
$data = $this->connection->fetchAssoc('SELECT * FROM tl_url_rewrite WHERE id=?', [$id]);
} catch (\PDOException | TableNotFoundException $e) {
Expand Down Expand Up @@ -102,13 +90,18 @@ private function createConfig(array $data): ?RewriteConfig
return null;
}

$config = new RewriteConfig($this->key.':'.$data['id'], $data['requestPath'], (int) $data['responseCode']);
$config = new RewriteConfig((string) $data['id'], $data['requestPath'], (int) $data['responseCode']);

// Hosts
if (isset($data['requestHosts'])) {
$config->setRequestHosts(StringUtil::deserialize($data['requestHosts'], true));
}

// Response URI
if (isset($data['responseUri'])) {
$config->setResponseUri($data['responseUri']);
}

switch ($data['type']) {
// Basic type
case 'basic':
Expand Down
8 changes: 3 additions & 5 deletions src/DependencyInjection/Compiler/ConfigProviderPass.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public function process(ContainerBuilder $container)
$services = $this->findAndSortTaggedServices($this->tag, $container);

// If there's only one service or chain service is not present alias the first service
if ((count($services) === 1 && count($services[0]) === 1) || !$container->has($this->chain)) {
if ((count($services) === 1 && count($services[0]) === 1) || !$container->hasDefinition($this->chain)) {
$container->setAlias($this->alias, (string) $services[0]);

return;
Expand All @@ -64,10 +64,8 @@ public function process(ContainerBuilder $container)
$definition = $container->findDefinition($this->chain);

// Add providers to the chain
foreach ($services as $providers) {
foreach ($providers as $provider) {
$definition->addMethodCall('addProvider', [$provider]);
}
foreach ($services as $service) {
$definition->addMethodCall('addProvider', [$service]);
}
}
}
8 changes: 8 additions & 0 deletions src/RewriteConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,14 @@ public function getIdentifier(): string
return $this->identifier;
}

/**
* @inheritDoc
*/
public function setIdentifier(string $identifier): void
{
$this->identifier = $identifier;
}

/**
* @return string
*/
Expand Down
5 changes: 5 additions & 0 deletions src/RewriteConfigInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ interface RewriteConfigInterface
*/
public function getIdentifier(): string;

/**
* @param string $identifier
*/
public function setIdentifier(string $identifier): void;

/**
* @return string
*/
Expand Down

0 comments on commit 99d021b

Please sign in to comment.