Skip to content
Snippets Groups Projects
Commit 0a1334ef authored by Patrick Barroca's avatar Patrick Barroca :grin:
Browse files

dev #48349 : versions loader refactoring

parent 78e3fbe6
Branches
Tags
2 merge requests!2334Master,!2104Dev#48349 ux versionning
Pipeline #959 passed with stage
in 18 minutes and 16 seconds
......@@ -21,70 +21,35 @@
class Class_Version extends Class_Entity {
protected
$_attribs = ['Name' => '',
'Key' => '',
'Data' => ''];
protected $_loader;
protected static $_persistence;
public static function setPersistence($persistence) {
static::$_persistence = $persistence;
}
public static function getPersistence() {
return static::$_persistence
? static::$_persistence
: static::$_persistence = new Class_Version_FilePersistence();
public function __construct($loader) {
$this->_loader = $loader;
}
public function findAll() {
return static::getPersistence()->findAllOf($this);
}
public function findLastKey() {
return static::getPersistence()->findLastKeyOf($this);
public function save() {
$this->_loader->save($this);
}
public function find($key) {
return static::getPersistence()->findOneOf($this, $key);
public function delete() {
return $this->_loader->delete($this);
}
public function previous() {
return static::getPersistence()->previousOf($this);
return $this->_loader->previousOf($this);
}
public function next() {
return static::getPersistence()->nextOf($this);
}
public function save() {
static::getPersistence()->save($this);
}
public function newFromMe() {
return (new Class_Version())
->setName($this->getName())
->setKey($this->getKey());
return $this->_loader->nextOf($this);
}
public function callGetterByAttributeName($attribute) {
return $this->get($attribute);
}
public function delete() {
return static::getPersistence()->delete($this);
}
}
......@@ -42,25 +42,25 @@ class Class_Version_FilePersistence extends Class_Entity {
}
public function findAllOf($version) {
if (!$version->getName() || !$version->getKey())
public function findAllOf($versions) {
if (!$versions->getName() || !$versions->getKey())
return [];
$path = $this->_getFolderPath($version);
$path = $this->_getFolderPath($versions);
if (!$files = $this->_filesIn($path))
return [];
$versions = [];
$instances = [];
foreach($files as $file)
$versions[] = $this->_newFromFile($version, $file, $path);
$instances[] = $this->_newFromFile($versions, $file, $path);
return array_filter($versions);
return array_filter($instances);
}
public function findOneOf($version, $key) {
if (!$all = $this->findAllOf($version))
public function findOneOf($versions, $key) {
if (!$all = $this->findAllOf($versions))
return;
foreach($all as $one)
......@@ -69,11 +69,11 @@ class Class_Version_FilePersistence extends Class_Entity {
}
public function findLastKeyOf($version) {
if (!$version->getName() || !$version->getKey())
public function findLastKeyOf($versions) {
if (!$versions->getName() || !$versions->getKey())
return;
$path = $this->_getFolderPath($version);
$path = $this->_getFolderPath($versions);
return ($files = $this->_filesIn($path))
? str_replace('.json', '', current($files))
......@@ -81,12 +81,12 @@ class Class_Version_FilePersistence extends Class_Entity {
}
public function previousOf($version) {
if (!$version->getName() || !$version->getKey()
|| (!$id = $version->getId()))
public function previousOf($versions, $id) {
if (!$versions->getName() || !$versions->getKey()
|| !$id)
return;
if (!$all = $this->findAllOf($version))
if (!$all = $this->findAllOf($versions))
return;
$found = false;
......@@ -102,12 +102,12 @@ class Class_Version_FilePersistence extends Class_Entity {
}
public function nextOf($version) {
if (!$version->getName() || !$version->getKey()
|| (!$id = $version->getId()))
public function nextOf($versions, $id) {
if (!$versions->getName() || !$versions->getKey()
|| !$id)
return;
if (!$all = $this->findAllOf($version))
if (!$all = $this->findAllOf($versions))
return;
$found = false;
......@@ -121,25 +121,27 @@ class Class_Version_FilePersistence extends Class_Entity {
}
public function save($version) {
if (!$version->getName() || !$version->getKey() || !$version->getData())
public function save($versions, $instance) {
if (!$versions->getName() || !$versions->getKey()
|| !$instance->getData())
return;
if (!$path = $this->getPathOf($version))
if (!$path = $this->getPathOf($versions))
return;
$this->getFileSystem()
->file_put_contents($path, $this->encode($version));
->file_put_contents($path, $this->encode($instance));
$this->_cleanStackOf($version);
$this->_cleanStackOf($versions);
}
public function delete($version) {
if (!$version->getName() || !$version->getKey() || !$version->getId())
public function delete($versions, $id) {
if (!$versions->getName() || !$versions->getKey()
|| !$id)
return;
$path = $this->_getFolderPath($version) . '/' . $version->getId() . '.json';
$path = $this->_getFolderPath($versions) . '/' . $id . '.json';
$file_system = $this->getFileSystem();
if ($file_system->file_exists($path))
$file_system->unlink($path);
......@@ -151,12 +153,12 @@ class Class_Version_FilePersistence extends Class_Entity {
}
public function getPathOf($version) {
public function getPathOf($versions) {
if (!$this->getFolderManager()
->ensure($folder = $this->_getFolderPath($version)))
->ensure($folder = $this->_getFolderPath($versions)))
return;
return $folder . '/'. $this->_fileName($version);
return $folder . '/'. $this->_fileName();
}
......@@ -169,11 +171,11 @@ class Class_Version_FilePersistence extends Class_Entity {
protected function _getFolderPath($version) {
protected function _getFolderPath($versions) {
return PATH_TEMP
. implode('/', [static::BASE_PATH,
$this->_sanitize($version->getName()),
$this->_sanitize($version->getKey())]);
$this->_sanitize($versions->getName()),
$this->_sanitize($versions->getKey())]);
}
......@@ -190,11 +192,11 @@ class Class_Version_FilePersistence extends Class_Entity {
}
protected function _newFromFile($version, $name, $path) {
protected function _newFromFile($versions, $name, $path) {
if (in_array($name, ['.', '..']))
return;
return $this->_populateFrom($version->newFromMe(), $name, $path);
return $this->_populateFrom($versions->newInstance(), $name, $path);
}
......@@ -235,8 +237,8 @@ class Class_Version_FilePersistence extends Class_Entity {
}
protected function _cleanStackOf($version) {
$path = $this->_getFolderPath($version);
protected function _cleanStackOf($versions) {
$path = $this->_getFolderPath($versions);
if (!$files = $this->_filesIn($path))
return;
......
<?php
/**
* Copyright (c) 2012-2017, Agence Française Informatique (AFI). All rights reserved.
*
* BOKEH is free software; you can redistribute it and/or modify
* it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE as published by
* the Free Software Foundation.
*
* There are special exceptions to the terms and conditions of the AGPL as it
* is applied to this software (see README file).
*
* BOKEH is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU AFFERO GENERAL PUBLIC LICENSE for more details.
*
* You should have received a copy of the GNU AFFERO GENERAL PUBLIC LICENSE
* along with BOKEH; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
class Class_Versions {
protected $_name, $_key;
protected static $_persistence;
public static function setPersistence($persistence) {
static::$_persistence = $persistence;
}
public static function getPersistence() {
return static::$_persistence
? static::$_persistence
: static::$_persistence = new Class_Version_FilePersistence();
}
public function __construct($name, $key) {
$this->_name = $name;
$this->_key = $key;
}
public function getName() {
return $this->_name;
}
public function getKey() {
return $this->_key;
}
public function newInstance() {
return new Class_Version($this);
}
public function save($version) {
return static::getPersistence()->save($this, $version);
}
public function delete($version) {
return static::getPersistence()->delete($this, $version->getId());
}
public function find($key) {
return static::getPersistence()->findOneOf($this, $key);
}
public function findAll() {
return static::getPersistence()->findAllOf($this);
}
public function findLastKey() {
return static::getPersistence()->findLastKeyOf($this);
}
public function previousOf($version) {
return static::getPersistence()->previousOf($this, $version->getId());
}
public function nextOf($version) {
return static::getPersistence()->nextOf($this, $version->getId());
}
}
......@@ -38,7 +38,7 @@ class ZendAfi_Controller_Plugin_Versionning_Article
$has_version = function($model) {
return 0 < count($this->_versionFor($model)
return 0 < count($this->_versionsFor($model)
->findAll());
};
......@@ -70,13 +70,13 @@ class ZendAfi_Controller_Plugin_Versionning_Article
$this->_view->titre = $this->_('Versions de l\'article : "%s"',
$article->getTitre());
if (!$versions = $this->_versionFor($article)->findAll()) {
if (!$versions = $this->_versionsFor($article)->findAll()) {
$this->_helper->notify($this->_('Aucune version dans l\'historique'));
$this->_redirect('admin/cms/edit/id/' . $article->getId());
return;
}
$this->_view->versions = $this->_versionFor($article)->findAll();
$this->_view->versions = $this->_versionsFor($article)->findAll();
$this->_view->article = $article;
}
......@@ -88,9 +88,9 @@ class ZendAfi_Controller_Plugin_Versionning_Article
}
if (!$key = $this->_getParam('key'))
$key = $this->_versionFor($article)->findLastKey();
$key = $this->_versionsFor($article)->findLastKey();
if (!$version = $this->_versionFor($article)->find($key)) {
if (!$version = $this->_versionsFor($article)->find($key)) {
$this->_helper->notify($this->_('Aucune version dans l\'historique'));
$this->_redirectToEdit($article);
return;
......@@ -115,7 +115,7 @@ class ZendAfi_Controller_Plugin_Versionning_Article
return;
}
if (!$version = $this->_versionFor($article)->find($this->_getParam('key'))) {
if (!$version = $this->_versionsFor($article)->find($this->_getParam('key'))) {
$this->_helper->notify($this->_view->_('Version introuvable'));
$this->_redirectToEdit($article);
return;
......@@ -128,7 +128,8 @@ class ZendAfi_Controller_Plugin_Versionning_Article
public function notifyAfterSave($model) {
$this->_versionFor($model)
$this->_versionsFor($model)
->newInstance()
->setData($this->_getPost())
->save();
}
......@@ -139,14 +140,7 @@ class ZendAfi_Controller_Plugin_Versionning_Article
}
protected function _versionName() {
return 'article';
}
protected function _versionFor($model) {
return (new Class_Version())
->setName($this->_versionName())
->setKey($model->getId());
protected function _versionsFor($model) {
return new Class_Versions('article', $model->getId());
}
}
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment