Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 24 additions & 12 deletions src/Model/ContentProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
namespace Pehapkari\InlineEditable\Model;

use Psr\Cache\CacheItemPoolInterface;
use Psr\Cache\InvalidArgumentException;

/**
* CACHING LEVELS
Expand Down Expand Up @@ -73,58 +74,68 @@ public function __construct(
* @param string $namespace
* @param string $locale
* @param string $name
*
* @return string
*
* @throws InvalidArgumentException
*/
public function getContent(string $namespace, string $locale, string $name): string
{
$nKey = self::getNKey($namespace, $locale);
$namespaceKey = self::getNamespaceKey($namespace, $locale);

// L1 read
$content = $this->loadedData[$nKey][$name] ?? false;
$content = $this->loadedData[$namespaceKey][$name] ?? false;

if (is_string($content)) {
// L1 hit
return $content;
}

// L2 read + L1 write
$this->loadedData[$nKey] = $this->loadedData[$nKey] ?? $this->loadNspaceFromCache($namespace, $locale);
$this->loadedData[$namespaceKey] = $this->loadedData[$namespaceKey] ?? $this->loadNamespaceFromCache($namespace, $locale);

$fallbackLocale = $this->config['fallback'] ?? '';
$content = $this->loadedData[$nKey][$name] ?? false;
$content = $this->loadedData[$namespaceKey][$name] ?? false;

if (is_string($content)) {
return $content;
} elseif ($locale === $fallbackLocale) {
return '';
} else {
return $this->getContent($namespace, $fallbackLocale, $name);
}

return $this->getContent($namespace, $fallbackLocale, $name);
}

/**
* @param string $namespace
* @param string $locale
* @param string $name
* @param string $content
*
* @throws InvalidArgumentException
*/
public function saveContent(string $namespace, string $locale, string $name, string $content)
{
$nKey = self::getNKey($namespace, $locale);
$namespaceKey = self::getNamespaceKey($namespace, $locale);

// L1 + L2 clear => L3 write
unset($this->loadedData[$nKey]);
$this->cache->deleteItem($nKey);
unset($this->loadedData[$namespaceKey]);

$this->cache->deleteItem($namespaceKey);
$this->persistenceLayer->saveContent($namespace, $name, $locale, $content);
}

/**
* @param string $namespace
* @param string $locale
*
* @return array
*
* @throws InvalidArgumentException
*/
public function loadNspaceFromCache(string $namespace, string $locale): array
public function loadNamespaceFromCache(string $namespace, string $locale): array
{
$nKey = self::getNKey($namespace, $locale);
$nKey = self::getNamespaceKey($namespace, $locale);

// L2 read
$cacheItem = $this->cache->getItem($nKey);
Expand All @@ -146,9 +157,10 @@ public function loadNspaceFromCache(string $namespace, string $locale): array
/**
* @param string $namespace
* @param string $locale
*
* @return string
*/
public static function getNKey(string $namespace, string $locale): string
public static function getNamespaceKey(string $namespace, string $locale): string
{
return '__inline_prefix_' . $namespace . '.' . $locale;
}
Expand Down
15 changes: 10 additions & 5 deletions src/Model/PersistenceLayer/AbstractPersistenceLayer.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@

namespace Pehapkari\InlineEditable\Model\PersistenceLayer;

use Exception;
use Pehapkari\InlineEditable\Model\PersistenceLayerInterface;
use UnexpectedValueException;

/**
* @author Jakub Janata <jakubjanata@gmail.com>
Expand Down Expand Up @@ -39,13 +39,15 @@ public function __construct(string $tableName)
/**
* @param string $sql
* @param array $args
*
* @return array
*/
abstract protected function getKeyPairResult(string $sql, array $args): array;

/**
* @param string $sql
* @param array $args
*
* @return bool
*/
abstract protected function updateOrInsertRecord(string $sql, array $args): bool;
Expand All @@ -58,11 +60,13 @@ abstract protected function getDriverName(): string;
/**
* @param string $namespace
* @param string $locale
*
* @return array
*/
public function getNamespaceContent(string $namespace, string $locale): array
{
$sql = "SELECT name, content FROM $this->tableName WHERE namespace = ? AND locale = ?";

return $this->getKeyPairResult($sql, [$namespace, $locale]);
}

Expand All @@ -72,7 +76,8 @@ public function getNamespaceContent(string $namespace, string $locale): array
* @param string $locale
* @param string $content
* @return bool
* @throws Exception
*
* @throws UnexpectedValueException
*/
public function saveContent(string $namespace, string $name, string $locale, string $content): bool
{
Expand All @@ -85,7 +90,7 @@ public function saveContent(string $namespace, string $name, string $locale, str
} elseif ($driver === 'pgsql') {
$sql = "$sql ON CONFLICT (namespace, name, locale) DO UPDATE SET content = EXCLUDED.content";
} else {
throw new Exception("Unknown driver '$driver'");
throw new UnexpectedValueException("Unknown driver '$driver'");
}

return $this->updateOrInsertRecord($sql, [$namespace, $name, $locale, $content]);
Expand All @@ -99,12 +104,12 @@ private function detectDbDriver(): string
if (!$this->driverName) {
$driverName = $this->getDriverName();

$this->driverName = '';

if (strpos($driverName, 'mysql') !== false) {
$this->driverName = 'mysql';
} elseif (strpos($driverName, 'pgsql') !== false || strpos($driverName, 'postgre') !== false) {
$this->driverName = 'pgsql';
} else {
$this->driverName = '';
}
}

Expand Down
14 changes: 13 additions & 1 deletion src/Model/PersistenceLayer/Dbal.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
namespace Pehapkari\InlineEditable\Model\PersistenceLayer;

use Doctrine\DBAL\Connection;
use Doctrine\DBAL\DBALException;
use PDO;

/**
* @author Jakub Janata <jakubjanata@gmail.com>
Expand All @@ -30,36 +32,46 @@ class Dbal extends AbstractPersistenceLayer
public function __construct(string $tableName, Connection $connection)
{
parent::__construct($tableName);

$this->connection = $connection;
}

/**
* @param string $sql
* @param array $args
*
* @return array
*
* @throws DBALException
*/
protected function getKeyPairResult(string $sql, array $args): array
{
$stmt = $this->connection->prepare($sql);

$stmt->bindValue(1, $args[0]);
$stmt->bindValue(2, $args[1]);
$stmt->execute();

return $stmt->fetchAll(\PDO::FETCH_KEY_PAIR);
return $stmt->fetchAll(PDO::FETCH_KEY_PAIR);
}

/**
* @param string $sql
* @param array $args
*
* @return bool
*
* @throws DBALException
*/
protected function updateOrInsertRecord(string $sql, array $args): bool
{
$stmt = $this->connection->prepare($sql);

$stmt->bindValue(1, $args[0]);
$stmt->bindValue(2, $args[1]);
$stmt->bindValue(3, $args[2]);
$stmt->bindValue(4, $args[3]);

return $stmt->execute();
}

Expand Down
14 changes: 13 additions & 1 deletion src/Model/PersistenceLayer/Dibi.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
namespace Pehapkari\InlineEditable\Model\PersistenceLayer;

use Dibi\Connection;
use Dibi\Exception;
use InvalidArgumentException;
use PDO;

/**
* @author Jakub Janata <jakubjanata@gmail.com>
Expand All @@ -29,13 +32,18 @@ class Dibi extends AbstractPersistenceLayer
public function __construct(string $tableName, Connection $connection)
{
parent::__construct($tableName);

$this->connection = $connection;
}

/**
* @param string $sql
* @param array $args
*
* @return array
*
* @throws Exception
* @throws InvalidArgumentException
*/
protected function getKeyPairResult(string $sql, array $args): array
{
Expand All @@ -45,11 +53,15 @@ protected function getKeyPairResult(string $sql, array $args): array
/**
* @param string $sql
* @param array $args
*
* @return bool
*
* @throws Exception
*/
protected function updateOrInsertRecord(string $sql, array $args): bool
{
$this->connection->query($sql, $args[0], $args[1], $args[2], $args[3]);

return true;
}

Expand All @@ -61,7 +73,7 @@ protected function getDriverName(): string
$driverName = $this->connection->getConfig('driver', '');

return ($driverName === 'pdo') ?
$this->connection->getDriver()->getResource()->getAttribute(\PDO::ATTR_DRIVER_NAME) :
$this->connection->getDriver()->getResource()->getAttribute(PDO::ATTR_DRIVER_NAME) :
$driverName;
}
}
1 change: 1 addition & 0 deletions src/Model/PersistenceLayer/NetteDatabase.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ class NetteDatabase extends AbstractPersistenceLayer
public function __construct(string $tableName, Connection $connection)
{
parent::__construct($tableName);

$this->connection = $connection;
}

Expand Down
2 changes: 2 additions & 0 deletions src/Model/PersistenceLayerInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ interface PersistenceLayerInterface
/**
* @param string $namespace
* @param string $locale
*
* @return array - output format ["name" => "value", ...]
*/
public function getNamespaceContent(string $namespace, string $locale): array;
Expand All @@ -27,6 +28,7 @@ public function getNamespaceContent(string $namespace, string $locale): array;
* @param string $name
* @param string $locale
* @param string $content
*
* @return bool - TRUE on success or FALSE on failure
*/
public function saveContent(string $namespace, string $name, string $locale, string $content): bool;
Expand Down
3 changes: 0 additions & 3 deletions tests/Integration/PersistenceLayer/BaseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,6 @@ abstract class BaseTest extends TestCase
*/
abstract protected function initPersistentLayer();

/**
*
*/
public function setUp()
{
$this->initPersistentLayer();
Expand Down
4 changes: 2 additions & 2 deletions tests/Unit/Model/ContentProviderTest.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ class ContentProviderTest extends TestCase
*/
public function testGeneratingNKey()
{
$nKey = ContentProvider::getNKey('space', 'cs');
Assert::equal('__inline_prefix_space.cs', $nKey);
$nsKey = ContentProvider::getNamespaceKey('space', 'cs');
Assert::equal('__inline_prefix_space.cs', $nsKey);
}

/**
Expand Down