Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'feature/config-providers' into develop
- Loading branch information
Showing
31 changed files
with
1,643 additions
and
290 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,103 @@ | ||
<?php | ||
|
||
/* | ||
* UrlRewrite Bundle for Contao Open Source CMS. | ||
* | ||
* @copyright Copyright (c) 2017, terminal42 gmbh | ||
* @author terminal42 <https://terminal42.ch> | ||
* @license MIT | ||
*/ | ||
|
||
namespace Terminal42\UrlRewriteBundle\ConfigProvider; | ||
|
||
use Terminal42\UrlRewriteBundle\RewriteConfig; | ||
use Terminal42\UrlRewriteBundle\RewriteConfigInterface; | ||
|
||
class BundleConfigProvider implements ConfigProviderInterface | ||
{ | ||
/** | ||
* @var array | ||
*/ | ||
private $entries = []; | ||
|
||
/** | ||
* BundleConfigProvider constructor. | ||
* | ||
* @param array $entries | ||
*/ | ||
public function __construct(array $entries = []) | ||
{ | ||
$this->entries = $entries; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function find(string $id): ?RewriteConfigInterface | ||
{ | ||
if (!array_key_exists($id, $this->entries)) { | ||
return null; | ||
} | ||
|
||
return $this->createConfig($id, $this->entries[$id]); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function findAll(): array | ||
{ | ||
if (count($this->entries) === 0) { | ||
return []; | ||
} | ||
|
||
$configs = []; | ||
|
||
foreach ($this->entries as $id => $entry) { | ||
if (($config = $this->createConfig((string) $id, $entry)) !== null) { | ||
$configs[] = $config; | ||
} | ||
} | ||
|
||
return $configs; | ||
} | ||
|
||
/** | ||
* Create the config. | ||
* | ||
* @param string $id | ||
* @param array $data | ||
* | ||
* @return null|RewriteConfig | ||
*/ | ||
private function createConfig(string $id, array $data): ?RewriteConfig | ||
{ | ||
if (!isset($data['request']['path'], $data['response']['code'])) { | ||
return null; | ||
} | ||
|
||
$config = new RewriteConfig($id, $data['request']['path'], (int) $data['response']['code']); | ||
|
||
// Request hosts | ||
if (isset($data['request']['hosts'])) { | ||
$config->setRequestHosts($data['request']['hosts']); | ||
} | ||
|
||
// Request condition | ||
if (isset($data['request']['condition'])) { | ||
$config->setRequestCondition($data['request']['condition']); | ||
} | ||
|
||
// Request requirements | ||
if (isset($data['request']['requirements'])) { | ||
$config->setRequestRequirements($data['request']['requirements']); | ||
} | ||
|
||
// Response URI | ||
if (isset($data['response']['uri'])) { | ||
$config->setResponseUri($data['response']['uri']); | ||
} | ||
|
||
return $config; | ||
} | ||
} |
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,82 @@ | ||
<?php | ||
|
||
/* | ||
* UrlRewrite Bundle for Contao Open Source CMS. | ||
* | ||
* @copyright Copyright (c) 2017, terminal42 gmbh | ||
* @author terminal42 <https://terminal42.ch> | ||
* @license MIT | ||
*/ | ||
|
||
namespace Terminal42\UrlRewriteBundle\ConfigProvider; | ||
|
||
use Terminal42\UrlRewriteBundle\RewriteConfigInterface; | ||
|
||
class ChainConfigProvider implements ConfigProviderInterface | ||
{ | ||
/** | ||
* @var array | ||
*/ | ||
private $providers = []; | ||
|
||
/** | ||
* Add the config provider. | ||
* | ||
* @param ConfigProviderInterface $provider | ||
*/ | ||
public function addProvider(ConfigProviderInterface $provider): void | ||
{ | ||
$this->providers[] = $provider; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function find(string $id): ?RewriteConfigInterface | ||
{ | ||
list($class, $id) = explode(':', $id); | ||
|
||
/** @var ConfigProviderInterface $provider */ | ||
foreach ($this->providers as $provider) { | ||
if ($class === $this->getProviderIdentifier($provider) && ($config = $provider->find($id)) !== null) { | ||
return $config; | ||
} | ||
} | ||
|
||
return null; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function findAll(): array | ||
{ | ||
$configs = []; | ||
|
||
/** @var ConfigProviderInterface $provider */ | ||
foreach ($this->providers as $provider) { | ||
$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); | ||
} | ||
} |
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,32 @@ | ||
<?php | ||
|
||
/* | ||
* UrlRewrite Bundle for Contao Open Source CMS. | ||
* | ||
* @copyright Copyright (c) 2017, terminal42 gmbh | ||
* @author terminal42 <https://terminal42.ch> | ||
* @license MIT | ||
*/ | ||
|
||
namespace Terminal42\UrlRewriteBundle\ConfigProvider; | ||
|
||
use Terminal42\UrlRewriteBundle\RewriteConfigInterface; | ||
|
||
interface ConfigProviderInterface | ||
{ | ||
/** | ||
* Find the config. | ||
* | ||
* @param string $id | ||
* | ||
* @return RewriteConfigInterface|null | ||
*/ | ||
public function find(string $id): ?RewriteConfigInterface; | ||
|
||
/** | ||
* Find all configs. | ||
* | ||
* @return array | ||
*/ | ||
public function findAll(): array; | ||
} |
Oops, something went wrong.