Skip to content

Commit

Permalink
Merge pull request #791 from oat-sa/feature/psr16-support
Browse files Browse the repository at this point in the history
Feature/psr16 support
  • Loading branch information
siwane authored May 15, 2020
2 parents 97b22ab + 265d31a commit 2739577
Show file tree
Hide file tree
Showing 27 changed files with 614 additions and 79 deletions.
1 change: 1 addition & 0 deletions common/cache/class.KeyValueCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
* @access public
* @author Jerome Bogaerts, <[email protected]>
* @package generis
* @deprecated Please use oat\oatbox\cache\SimpleCache
*/
class common_cache_KeyValueCache extends ConfigurableService implements common_cache_Cache
{
Expand Down
119 changes: 119 additions & 0 deletions common/cache/class.PsrWrapperCache.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
<?php

/**
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; under version 2
* of the License (non-upgradable).
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* Copyright (c) 2020 (original work) Open Assessment Technologies SA
*
*/

use oat\oatbox\service\ConfigurableService;
use Psr\SimpleCache\CacheInterface;
use oat\oatbox\cache\SimpleCache;

/**
* Wrap the PSR simple cache implementation into the legacy interface
* @deprecated Please use oat\oatbox\cache\SimpleCache
*/
class common_cache_PsrWrapperCache extends ConfigurableService implements common_cache_Cache
{

/**
* puts "something" into the cache,
* * If this is an object and implements Serializable,
* * we use the serial provided by the object
* * else a serial must be provided
* @access public
* @author Jerome Bogaerts, <[email protected]>
* @param mixed $mixed
* @param null $serial
* @param null $ttl
* @return bool
* @throws common_exception_Error
*/
public function put($mixed, $serial = null, $ttl = null)
{
if ($mixed instanceof common_Serializable) {
if (!is_null($serial) && $serial != $mixed->getSerial()) {
throw new common_exception_Error('Serial mismatch for Serializable ' . $mixed->getSerial());
}
$serial = $mixed->getSerial();
}

return $this->getPsrSimpleCache()->set($serial, $mixed, $ttl);
}

/**
* gets the entry associted to the serial
*
* @access public
* @author Jerome Bogaerts, <[email protected]>
* @param string serial
* @return common_Serializable
* @throws common_cache_NotFoundException
*/
public function get($serial)
{
$returnValue = $this->getPsrSimpleCache()->get($serial, false);
if ($returnValue === false && !$this->getPsrSimpleCache()->has($serial)) {
$msg = "No cache entry found for '" . $serial . "'.";
throw new common_cache_NotFoundException($msg);
}
return $returnValue;
}

/**
* test whenever an entry associated to the serial exists
*
* @access public
* @author Jerome Bogaerts, <[email protected]>
* @param string serial
* @return boolean
*/
public function has($serial)
{
return $this->getPsrSimpleCache()->has($serial);
}

/**
* removes an entry from the cache
*
* @access public
* @author Jerome Bogaerts, <[email protected]>
* @param string serial
* @return mixed
*/
public function remove($serial)
{
return $this->getPsrSimpleCache()->delete($serial);
}

/**
* empties the cache
*
* @access public
* @author Jerome Bogaerts, <[email protected]>
* @return mixed
*/
public function purge()
{
return $this->getPsrSimpleCache()->clear();
}

protected function getPsrSimpleCache() : CacheInterface
{
return $this->getServiceLocator()->get(SimpleCache::SERVICE_ID);
}
}
15 changes: 1 addition & 14 deletions common/cache/interface.Cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,20 +22,7 @@

/**
* basic interface a cache implementation has to implement
*
* @author Jerome Bogaerts, <[email protected]>
* @package generis
*/


/**
* basic interface a cache implementation has to implement
*
* @access public
* @author Jerome Bogaerts, <[email protected]>
* @package generis
* @deprecated please use oat\oatbox\cache\SimpleCache
*/
interface common_cache_Cache
{
Expand Down
5 changes: 3 additions & 2 deletions common/ext/class.ExtensionInstaller.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
use oat\oatbox\event\EventManager;
use oat\oatbox\service\ConfigurableService;
use oat\generis\model\data\import\RdfImporter;
use oat\oatbox\cache\SimpleCache;

/**
* Generis installer of extensions
Expand Down Expand Up @@ -84,8 +85,8 @@ public function install()

// we purge the whole cache.
$this->log('d', 'Purging cache...');
$cache = common_cache_FileCache::singleton();
$cache->purge();
$cache = $this->getServiceManager()->get(SimpleCache::SERVICE_ID);
$cache->clear();


// check required extensions, throws exception if failed
Expand Down
6 changes: 4 additions & 2 deletions common/ext/class.ExtensionUninstaller.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<?php
use oat\oatbox\cache\SimpleCache;

/**
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
Expand Down Expand Up @@ -68,8 +70,8 @@ public function uninstall()
$this->unregister();

// we purge the whole cache.
$cache = common_cache_FileCache::singleton();
$cache->purge();
$cache = $this->getServiceManager()->get(SimpleCache::SERVICE_ID);
$cache->clear();

common_Logger::i('Uninstalled ' . $this->extension->getId());
return true;
Expand Down
3 changes: 2 additions & 1 deletion common/ext/class.GenerisInstaller.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
use oat\generis\persistence\PersistenceManager;
use oat\generis\model\data\Ontology;
use oat\generis\persistence\sql\SchemaProviderInterface;
use oat\oatbox\cache\SimpleCache;

/**
* Custom extension installer for generis
Expand Down Expand Up @@ -54,7 +55,7 @@ public function install()
$this->installOntology();
$this->installRegisterExt();

$this->getServiceManager()->get(common_cache_Cache::SERVICE_ID)->purge();
$this->getServiceManager()->get(SimpleCache::SERVICE_ID)->clear();

$this->log('d', 'Installing custom script for extension ' . $this->extension->getId());
$this->installCustomScript();
Expand Down
3 changes: 2 additions & 1 deletion common/ext/class.UpdateExtensions.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
use Zend\ServiceManager\ServiceLocatorAwareTrait;
use Psr\Log\LoggerAwareInterface;
use oat\oatbox\log\LoggerAwareTrait;
use oat\oatbox\cache\SimpleCache;

/**
* Run the extension updater
Expand Down Expand Up @@ -121,7 +122,7 @@ protected function updateExtension(common_ext_Extension $ext)

$report->add($versionReport);

common_cache_FileCache::singleton()->purge();
$this->getServiceLocator()->get(SimpleCache::SERVICE_ID)->clear();
}
} else {
$report = new common_report_Report(common_report_Report::TYPE_INFO, $ext->getName() . ' already up to date');
Expand Down
24 changes: 9 additions & 15 deletions common/oatbox/action/ActionService.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@
namespace oat\oatbox\action;

use oat\oatbox\service\ConfigurableService;
use oat\oatbox\service\ServiceNotFoundException;
use Zend\ServiceManager\ServiceLocatorAwareInterface;
use oat\oatbox\cache\SimpleCache;
use common_ext_ExtensionsManager;

class ActionService extends ConfigurableService
{
Expand All @@ -43,9 +43,7 @@ public function resolve($actionIdentifier)
$action = $this->getServiceManager()->get($actionIdentifier);
} elseif (class_exists($actionIdentifier) && is_subclass_of($actionIdentifier, Action::class)) {
$action = new $actionIdentifier();
if ($action instanceof ServiceLocatorAwareInterface) {
$action->setServiceLocator($this->getServiceLocator());
}
$this->propagate($action);
} else {
throw new ResolutionException('Unknown action ' . $actionIdentifier);
}
Expand All @@ -54,25 +52,21 @@ public function resolve($actionIdentifier)

public function getAvailableActions()
{
if ($this->getCache()->has(__FUNCTION__)) {
$actions = $this->getCache()->get(__FUNCTION__);
} else {
$actions = $this->getCache()->get(__FUNCTION__);
if (is_null($actions)) {
$actions = [];
foreach (\common_ext_ExtensionsManager::singleton()->getInstalledExtensions() as $ext) {
foreach ($this->getServiceLocator()->get(common_ext_ExtensionsManager::SERVICE_ID)->getInstalledExtensions() as $ext) {
$actions = array_merge($actions, $this->getActionsInDirectory($ext->getDir()));
}
$actions = array_merge($actions, $this->getActionsInDirectory(VENDOR_PATH . 'oat-sa'));
$this->getCache()->put($actions, __FUNCTION__);
$this->getCache()->set(__FUNCTION__, $actions);
}
return $actions;
}

/**
* @return \common_cache_Cache
*/
protected function getCache()
protected function getCache(): SimpleCache
{
return $this->getServiceManager()->get('generis/cache');
return $this->getServiceLocator()->get(SimpleCache::SERVICE_ID);
}

protected function getActionsInDirectory($dir)
Expand Down
100 changes: 100 additions & 0 deletions common/oatbox/cache/KeyValueCache.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
<?php

/**
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; under version 2
* of the License (non-upgradable).
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* Copyright (c) 2020 (original work) Open Assessment Technologies SA
*
*/

namespace oat\oatbox\cache;

use oat\oatbox\service\ConfigurableService;
use common_persistence_KeyValuePersistence;
use oat\generis\persistence\PersistenceManager;
use common_exception_NotImplemented;
use DateInterval;
use DateTimeImmutable;

/**
* Caches data in a key-value store
*
* @access public
* @author Jerome Bogaerts, <[email protected]>
* @package generis
*/
class KeyValueCache extends ConfigurableService implements SimpleCache
{
use MultipleCacheTrait;

const OPTION_PERSISTENCE = 'persistence';

/** @var common_persistence_KeyValuePersistence */
private $persistence;

public function set($key, $value, $ttl = null)
{
if ($ttl instanceof DateInterval) {
$ttl = $this->dateIntervalToSeconds($ttl);
}
return $this->getPersistence()->set($key, $value, $ttl);
}

public function clear()
{
try {
return $this->getPersistence()->purge();
} catch (common_exception_NotImplemented $e) {
return false;
}
}

public function delete($key)
{
return $this->getPersistence()->del($key);
}

public function get($key, $default = null)
{
$returnValue = $this->getPersistence()->get($key);
// persistence can return false on a value of false or a not found key
return ($returnValue !== false || $this->has($key))
? $returnValue
: $default;
}

public function has($key)
{
return $this->getPersistence()->exists($key);
}

protected function dateIntervalToSeconds(DateInterval $dateInterval): int
{
$reference = new DateTimeImmutable;
$endTime = $reference->add($dateInterval);
return $endTime->getTimestamp() - $reference->getTimestamp();
}

/**
* @return common_persistence_KeyValuePersistence
*/
protected function getPersistence()
{
if (is_null($this->persistence)) {
$this->persistence = $this->getServiceLocator()->get(PersistenceManager::SERVICE_ID)->getPersistenceById($this->getOption(self::OPTION_PERSISTENCE));
}
return $this->persistence;
}
}
Loading

0 comments on commit 2739577

Please sign in to comment.