-
-
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
927 additions
and
118 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,116 @@ | ||
<?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 = []; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
private $key = 'bundle'; | ||
|
||
/** | ||
* BundleConfigProvider constructor. | ||
* | ||
* @param array $entries | ||
*/ | ||
public function __construct(array $entries = []) | ||
{ | ||
$this->entries = $entries; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
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; | ||
} | ||
|
||
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($id, $entry)) !== null) { | ||
$configs[] = $config; | ||
} | ||
} | ||
|
||
return $configs; | ||
} | ||
|
||
/** | ||
* Create the config. | ||
* | ||
* @param int $id | ||
* @param array $data | ||
* | ||
* @return null|RewriteConfig | ||
*/ | ||
private function createConfig(int $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']); | ||
|
||
// 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,61 @@ | ||
<?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 | ||
{ | ||
/** @var ConfigProviderInterface $provider */ | ||
foreach ($this->providers as $provider) { | ||
if (($config = $provider->find($id)) !== null) { | ||
return $config; | ||
} | ||
} | ||
|
||
return null; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function findAll(): array | ||
{ | ||
$configs = []; | ||
|
||
/** @var ConfigProviderInterface $provider */ | ||
foreach ($this->providers as $provider) { | ||
$configs = array_merge($configs, $provider->findAll()); | ||
} | ||
|
||
return $configs; | ||
} | ||
} |
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,137 @@ | ||
<?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; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
private $key = 'database'; | ||
|
||
/** | ||
* DatabaseConfigProvider constructor. | ||
* | ||
* @param Connection $connection | ||
*/ | ||
public function __construct(Connection $connection) | ||
{ | ||
$this->connection = $connection; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
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) { | ||
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($this->key.':'.$data['id'], $data['requestPath'], (int) $data['responseCode']); | ||
|
||
// Hosts | ||
if (isset($data['requestHosts'])) { | ||
$config->setRequestHosts(StringUtil::deserialize($data['requestHosts'], true)); | ||
} | ||
|
||
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.