Extract repository classes to read/update box versions

This commit is contained in:
Aztech
2015-07-08 09:51:21 +02:00
committed by Thibaud Fabre
parent 2a134d4bca
commit 5aad5e933e
7 changed files with 168 additions and 52 deletions

View File

@@ -13,14 +13,27 @@ namespace Alchemy\Phrasea\Core;
class Version
{
/**
* @var string
*/
private $number = '4.0.0-alpha.2';
/**
* @var string
*/
private $name = 'Herrerasaurus';
/**
* @return string
*/
public function getNumber()
{
return $this->number;
}
/**
* @return string
*/
public function getName()
{
return $this->name;

View File

@@ -0,0 +1,50 @@
<?php
namespace Alchemy\Phrasea\Core\Version;
use Alchemy\Phrasea\Core\Version;
use Doctrine\DBAL\Connection;
class AppboxVersionRepository implements VersionRepository
{
/**
* @var Connection
*/
private $connection;
/**
* @param Connection $connection
*/
public function __construct(Connection $connection)
{
$this->connection = $connection;
}
/**
* @return string
*/
public function getVersion()
{
$result = $this->connection->fetchAssoc('SELECT version FROM sitepreff');
if ($result !== false) {
return $result['version'];
}
return VersionRepository::DEFAULT_VERSION;
}
/**
* @param Version $version
* @return bool
*/
public function saveVersion(Version $version)
{
$statement = $this->connection->executeQuery(
'UPDATE sitepreff SET version = :version WHERE id = 1',
[ ':version' => $version->getNumber() ]
);
return $statement->rowCount() == 1;
}
}

View File

@@ -0,0 +1,53 @@
<?php
namespace Alchemy\Phrasea\Core\Version;
use Alchemy\Phrasea\Core\Version;
use Doctrine\DBAL\Connection;
class DataboxVersionRepository implements VersionRepository
{
/**
* @var Connection
*/
private $connection;
/**
* @param Connection $connection
*/
public function __construct(Connection $connection)
{
$this->connection = $connection;
}
/**
* @return string
*/
public function getVersion()
{
$result = $this->connection->fetchAssoc('SELECT value AS version FROM pref WHERE prop="version"');
if ($result !== false) {
return $result['version'];
}
return VersionRepository::DEFAULT_VERSION;
}
/**
* @param Version $version
* @return bool
* @throws \Doctrine\DBAL\DBALException
*/
public function saveVersion(Version $version)
{
$this->connection->exec("DELETE FROM pref WHERE prop='version'");
$statement = $this->connection->executeQuery(
'INSERT INTO pref (prop, value, locale, updated_on) VALUES ("version", :version, "", NOW())',
[ ':version' => $version->getNumber() ]
);
return $statement->rowCount() == 1;
}
}

View File

@@ -0,0 +1,22 @@
<?php
namespace Alchemy\Phrasea\Core\Version;
use Alchemy\Phrasea\Core\Version;
interface VersionRepository
{
const DEFAULT_VERSION = '0.0.0.0';
/**
* @return string
*/
public function getVersion();
/**
* @param Version $version
* @return bool
*/
public function saveVersion(Version $version);
}

View File

@@ -12,6 +12,7 @@
use Alchemy\Phrasea\Application;
use Alchemy\Phrasea\Core\Configuration\AccessRestriction;
use Alchemy\Phrasea\Core\Connection\ConnectionSettings;
use Alchemy\Phrasea\Core\Version\AppboxVersionRepository;
use Alchemy\Phrasea\Databox\DataboxRepositoryInterface;
use Doctrine\ORM\Tools\SchemaTool;
use MediaAlchemyst\Alchemyst;
@@ -45,6 +46,7 @@ class appbox extends base
public function __construct(Application $app)
{
$connectionConfig = $app['conf']->get(['main', 'database']);
$connection = $app['db.provider']($connectionConfig);
$connectionSettings = new ConnectionSettings(
$connectionConfig['host'],
@@ -54,9 +56,9 @@ class appbox extends base
$connectionConfig['password']
);
$connection = $app['db.provider']($connectionConfig);
$versionRepository = new AppboxVersionRepository($connection);
parent::__construct($app, $connection, $connectionSettings);
parent::__construct($app, $connection, $connectionSettings, $versionRepository);
}
public function write_collection_pic(Alchemyst $alchemyst, Filesystem $filesystem, collection $collection, SymfoFile $pathfile = null, $pic_type)

View File

@@ -13,7 +13,6 @@ use Alchemy\Phrasea\Application;
use Alchemy\Phrasea\Core\Connection\ConnectionSettings;
use Alchemy\Phrasea\Core\Database\DatabaseMaintenanceService;
use Alchemy\Phrasea\Core\Version as PhraseaVersion;
use vierbergenlars\SemVer\version;
use Doctrine\DBAL\Connection;
abstract class base implements cache_cacheableInterface
@@ -53,16 +52,26 @@ abstract class base implements cache_cacheableInterface
*/
protected $app;
/**
* @var PhraseaVersion\VersionRepository
*/
protected $versionRepository;
/**
* @param Application $application
* @param Connection $connection
* @param ConnectionSettings $connectionSettings
* @param PhraseaVersion\VersionRepository $versionRepository
*/
public function __construct(Application $application, Connection $connection, ConnectionSettings $connectionSettings)
public function __construct(Application $application,
Connection $connection,
ConnectionSettings $connectionSettings,
PhraseaVersion\VersionRepository $versionRepository)
{
$this->app = $application;
$this->connection = $connection;
$this->connectionSettings = $connectionSettings;
$this->versionRepository = $versionRepository;
}
/**
@@ -199,31 +208,21 @@ abstract class base implements cache_cacheableInterface
public function get_version()
{
if (! $this->version) {
$version = '0.0.0';
$sql = '';
if ($this->get_base_type() == self::APPLICATION_BOX) {
$sql = 'SELECT version FROM sitepreff';
}
if ($this->get_base_type() == self::DATA_BOX) {
$sql = 'SELECT value AS version FROM pref WHERE prop="version" LIMIT 1;';
}
if ($sql !== '') {
$row = $this->connection->fetchAssoc($sql);
if ($row) {
$version = $row['version'];
}
}
$this->version = $version;
$this->version = $this->versionRepository->getVersion($this);
}
return $this->version;
}
protected function setVersion(PhraseaVersion $version)
{
try {
return $this->versionRepository->saveVersion($this, $version);
} catch (\Exception $e) {
throw new Exception('Unable to set the database version : ' . $e->getMessage());
}
}
protected function upgradeDb($applyPatches)
{
$service = new DatabaseMaintenanceService($this->app, $this->connection);
@@ -231,32 +230,6 @@ abstract class base implements cache_cacheableInterface
return $service->upgradeDatabase($this, $applyPatches);
}
protected function setVersion(PhraseaVersion $version)
{
try {
$sql = '';
if ($this->get_base_type() === self::APPLICATION_BOX) {
$sql = 'UPDATE sitepreff SET version = :version WHERE id = 1';
}
if ($this->get_base_type() === self::DATA_BOX) {
$sql = 'DELETE FROM pref WHERE prop="version"';
$this->get_connection()->query($sql);
$sql = 'INSERT INTO pref (prop, value, locale, updated_on) VALUES ("version", :version, "", NOW())';
}
if ($sql !== '') {
$stmt = $this->get_connection()->prepare($sql);
$stmt->execute([':version' => $version->getNumber()]);
$stmt->closeCursor();
$this->version = $version->getNumber();
return true;
}
} catch (\Exception $e) {
throw new Exception('Unable to set the database version : ' . $e->getMessage());
}
}
/**
* @return base
* @throws Exception

View File

@@ -11,6 +11,7 @@
use Alchemy\Phrasea\Application;
use Alchemy\Phrasea\Core\Connection\ConnectionSettings;
use Alchemy\Phrasea\Core\Version\DataboxVersionRepository;
use Alchemy\Phrasea\Databox\DataboxFieldRepositoryInterface;
use Alchemy\Phrasea\Model\Entities\User;
use Alchemy\Phrasea\Exception\InvalidArgumentException;
@@ -80,6 +81,8 @@ class databox extends base
}
$connectionConfig = $connectionConfigs[$sbas_id];
$connection = $app['db.provider']($connectionConfig);
$connectionSettings = new ConnectionSettings(
$connectionConfig['host'],
$connectionConfig['port'],
@@ -88,9 +91,9 @@ class databox extends base
$connectionConfig['password']
);
$connection = $app['db.provider']($connectionConfig);
$versionRepository = new DataboxVersionRepository($connection);
parent::__construct($app, $connection, $connectionSettings);
parent::__construct($app, $connection, $connectionSettings, $versionRepository);
$this->loadFromRow($row);
}