Skip to content

Commit

Permalink
Merge dcb9418 into e7098f5
Browse files Browse the repository at this point in the history
  • Loading branch information
qzminski committed Nov 28, 2017
2 parents e7098f5 + dcb9418 commit 1e12510
Show file tree
Hide file tree
Showing 20 changed files with 857 additions and 118 deletions.
115 changes: 115 additions & 0 deletions src/ConfigProvider/BundleConfigProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
<?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;

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): ?RewriteConfig
{
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;
}
}
61 changes: 61 additions & 0 deletions src/ConfigProvider/ChainConfigProvider.php
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\RewriteConfig;

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): ?RewriteConfig
{
/** @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;
}
}
32 changes: 32 additions & 0 deletions src/ConfigProvider/ConfigProviderInterface.php
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\RewriteConfig;

interface ConfigProviderInterface
{
/**
* Find the config.
*
* @param string $id
*
* @return RewriteConfig|null
*/
public function find(string $id): ?RewriteConfig;

/**
* Find all configs.
*
* @return array
*/
public function findAll(): array;
}
136 changes: 136 additions & 0 deletions src/ConfigProvider/DatabaseConfigProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
<?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;

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): ?RewriteConfig
{
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;
}
}
Loading

0 comments on commit 1e12510

Please sign in to comment.