-
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
21 changed files
with
941 additions
and
119 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,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; | ||
} |
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,130 @@ | ||
<?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 Contao\StringUtil; | ||
use Doctrine\DBAL\Connection; | ||
use Doctrine\DBAL\Exception\TableNotFoundException; | ||
use Terminal42\UrlRewriteBundle\RewriteConfig; | ||
use Terminal42\UrlRewriteBundle\RewriteConfigInterface; | ||
|
||
class DatabaseConfigProvider implements ConfigProviderInterface | ||
{ | ||
/** | ||
* @var Connection | ||
*/ | ||
private $connection; | ||
|
||
/** | ||
* DatabaseConfigProvider constructor. | ||
* | ||
* @param Connection $connection | ||
*/ | ||
public function __construct(Connection $connection) | ||
{ | ||
$this->connection = $connection; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function find(string $id): ?RewriteConfigInterface | ||
{ | ||
try { | ||
$data = $this->connection->fetchAssoc('SELECT * FROM tl_url_rewrite WHERE id=?', [$id]); | ||
} catch (\PDOException | TableNotFoundException $e) { | ||
return null; | ||
} | ||
|
||
if (false === $data) { | ||
return null; | ||
} | ||
|
||
return $this->createConfig($data); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function findAll(): array | ||
{ | ||
try { | ||
$records = $this->connection->fetchAll('SELECT * FROM tl_url_rewrite'); | ||
} catch (\PDOException | TableNotFoundException $e) { | ||
return []; | ||
} | ||
|
||
if (count($records) === 0) { | ||
return []; | ||
} | ||
|
||
$configs = []; | ||
|
||
foreach ($records as $record) { | ||
if (($config = $this->createConfig($record)) !== null) { | ||
$configs[] = $config; | ||
} | ||
} | ||
|
||
return $configs; | ||
} | ||
|
||
/** | ||
* Create the config. | ||
* | ||
* @param array $data | ||
* | ||
* @return null|RewriteConfig | ||
*/ | ||
private function createConfig(array $data): ?RewriteConfig | ||
{ | ||
if (!isset($data['id'], $data['type'], $data['requestPath'], $data['responseCode'])) { | ||
return null; | ||
} | ||
|
||
$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': | ||
if (isset($data['requestRequirements'])) { | ||
$requirements = []; | ||
|
||
foreach (StringUtil::deserialize($data['requestRequirements'], true) as $requirement) { | ||
if ($requirement['key'] !== '' && $requirement['value'] !== '') { | ||
$requirements[$requirement['key']] = $requirement['value']; | ||
} | ||
} | ||
|
||
$config->setRequestRequirements($requirements); | ||
} | ||
break; | ||
// Expert type | ||
case 'expert': | ||
$config->setRequestCondition($data['requestCondition']); | ||
break; | ||
default: | ||
throw new \RuntimeException(sprintf('Unsupported database record config type: %s', $data['type'])); | ||
} | ||
|
||
return $config; | ||
} | ||
} |
Oops, something went wrong.