application: introduce the Shaarli\Config namespace

Namespaces have been introduced with the REST API, and should be generalized
to the whole codebase to manage object scope and benefit from autoloading.

See:
- https://secure.php.net/manual/en/language.namespaces.php
- http://www.php-fig.org/psr/psr-4/

Signed-off-by: VirtualTam <virtualtam@flibidi.net>
This commit is contained in:
VirtualTam 2017-03-03 23:06:12 +01:00
parent 74198dcdf6
commit 3c66e56435
26 changed files with 62 additions and 47 deletions

View file

@ -1,4 +1,6 @@
<?php <?php
use Shaarli\Config\ConfigJson;
use Shaarli\Config\ConfigPhp;
/** /**
* Class Updater. * Class Updater.

View file

@ -1,9 +1,9 @@
<?php <?php
namespace Shaarli\Api; namespace Shaarli\Api;
use Shaarli\Api\Exceptions\ApiException; use Shaarli\Api\Exceptions\ApiException;
use Shaarli\Api\Exceptions\ApiAuthorizationException; use Shaarli\Api\Exceptions\ApiAuthorizationException;
use Slim\Container; use Slim\Container;
use Slim\Http\Request; use Slim\Http\Request;
use Slim\Http\Response; use Slim\Http\Response;

View file

@ -1,4 +1,5 @@
<?php <?php
namespace Shaarli\Config;
/** /**
* Interface ConfigIO * Interface ConfigIO

View file

@ -1,4 +1,5 @@
<?php <?php
namespace Shaarli\Config;
/** /**
* Class ConfigJson (ConfigIO implementation) * Class ConfigJson (ConfigIO implementation)
@ -21,7 +22,7 @@ public function read($filepath)
$data = json_decode($data, true); $data = json_decode($data, true);
if ($data === null) { if ($data === null) {
$error = json_last_error(); $error = json_last_error();
throw new Exception('An error occurred while parsing JSON file: error code #'. $error); throw new \Exception('An error occurred while parsing JSON file: error code #'. $error);
} }
return $data; return $data;
} }
@ -35,7 +36,7 @@ public function write($filepath, $conf)
$print = defined('JSON_PRETTY_PRINT') ? JSON_PRETTY_PRINT : 0; $print = defined('JSON_PRETTY_PRINT') ? JSON_PRETTY_PRINT : 0;
$data = self::getPhpHeaders() . json_encode($conf, $print) . self::getPhpSuffix(); $data = self::getPhpHeaders() . json_encode($conf, $print) . self::getPhpSuffix();
if (!file_put_contents($filepath, $data)) { if (!file_put_contents($filepath, $data)) {
throw new IOException( throw new \IOException(
$filepath, $filepath,
'Shaarli could not create the config file. 'Shaarli could not create the config file.
Please make sure Shaarli has the right to write in the folder is it installed in.' Please make sure Shaarli has the right to write in the folder is it installed in.'

View file

@ -1,9 +1,5 @@
<?php <?php
namespace Shaarli\Config;
// FIXME! Namespaces...
require_once 'ConfigIO.php';
require_once 'ConfigJson.php';
require_once 'ConfigPhp.php';
/** /**
* Class ConfigManager * Class ConfigManager
@ -124,12 +120,12 @@ public function get($setting, $default = '')
* @param bool $write Write the new setting in the config file, default false. * @param bool $write Write the new setting in the config file, default false.
* @param bool $isLoggedIn User login state, default false. * @param bool $isLoggedIn User login state, default false.
* *
* @throws Exception Invalid * @throws \Exception Invalid
*/ */
public function set($setting, $value, $write = false, $isLoggedIn = false) public function set($setting, $value, $write = false, $isLoggedIn = false)
{ {
if (empty($setting) || ! is_string($setting)) { if (empty($setting) || ! is_string($setting)) {
throw new Exception('Invalid setting key parameter. String expected, got: '. gettype($setting)); throw new \Exception('Invalid setting key parameter. String expected, got: '. gettype($setting));
} }
// During the ConfigIO transition, map legacy settings to the new ones. // During the ConfigIO transition, map legacy settings to the new ones.
@ -177,7 +173,7 @@ public function exists($setting)
* *
* @throws MissingFieldConfigException: a mandatory field has not been provided in $conf. * @throws MissingFieldConfigException: a mandatory field has not been provided in $conf.
* @throws UnauthorizedConfigException: user is not authorize to change configuration. * @throws UnauthorizedConfigException: user is not authorize to change configuration.
* @throws IOException: an error occurred while writing the new config file. * @throws \IOException: an error occurred while writing the new config file.
*/ */
public function write($isLoggedIn) public function write($isLoggedIn)
{ {
@ -366,7 +362,7 @@ public function setConfigIO($configIO)
/** /**
* Exception used if a mandatory field is missing in given configuration. * Exception used if a mandatory field is missing in given configuration.
*/ */
class MissingFieldConfigException extends Exception class MissingFieldConfigException extends \Exception
{ {
public $field; public $field;
@ -385,7 +381,7 @@ public function __construct($field)
/** /**
* Exception used if an unauthorized attempt to edit configuration has been made. * Exception used if an unauthorized attempt to edit configuration has been made.
*/ */
class UnauthorizedConfigException extends Exception class UnauthorizedConfigException extends \Exception
{ {
/** /**
* Construct exception. * Construct exception.

View file

@ -1,4 +1,5 @@
<?php <?php
namespace Shaarli\Config;
/** /**
* Class ConfigPhp (ConfigIO implementation) * Class ConfigPhp (ConfigIO implementation)
@ -115,7 +116,7 @@ public function write($filepath, $conf)
if (!file_put_contents($filepath, $configStr) if (!file_put_contents($filepath, $configStr)
|| strcmp(file_get_contents($filepath), $configStr) != 0 || strcmp(file_get_contents($filepath), $configStr) != 0
) { ) {
throw new IOException( throw new \IOException(
$filepath, $filepath,
'Shaarli could not create the config file. 'Shaarli could not create the config file.
Please make sure Shaarli has the right to write in the folder is it installed in.' Please make sure Shaarli has the right to write in the folder is it installed in.'

View file

@ -1,4 +1,6 @@
<?php <?php
namespace Shaarli\Config;
/** /**
* Plugin configuration helper functions. * Plugin configuration helper functions.
* *
@ -112,7 +114,7 @@ function load_plugin_parameter_values($plugins, $conf)
/** /**
* Exception used if an error occur while saving plugin configuration. * Exception used if an error occur while saving plugin configuration.
*/ */
class PluginConfigOrderException extends Exception class PluginConfigOrderException extends \Exception
{ {
/** /**
* Construct exception. * Construct exception.

View file

@ -27,7 +27,8 @@
"Shaarli\\": "application", "Shaarli\\": "application",
"Shaarli\\Api\\": "application/api/", "Shaarli\\Api\\": "application/api/",
"Shaarli\\Api\\Controllers\\": "application/api/controllers", "Shaarli\\Api\\Controllers\\": "application/api/controllers",
"Shaarli\\Api\\Exceptions\\": "application/api/exceptions" "Shaarli\\Api\\Exceptions\\": "application/api/exceptions",
"Shaarli\\Config\\": "application/config/"
} }
} }
} }

View file

@ -62,8 +62,6 @@
require_once 'application/ApplicationUtils.php'; require_once 'application/ApplicationUtils.php';
require_once 'application/Cache.php'; require_once 'application/Cache.php';
require_once 'application/CachedPage.php'; require_once 'application/CachedPage.php';
require_once 'application/config/ConfigManager.php';
require_once 'application/config/ConfigPlugin.php';
require_once 'application/FeedBuilder.php'; require_once 'application/FeedBuilder.php';
require_once 'application/FileUtils.php'; require_once 'application/FileUtils.php';
require_once 'application/HttpUtils.php'; require_once 'application/HttpUtils.php';
@ -80,6 +78,7 @@
require_once 'application/Router.php'; require_once 'application/Router.php';
require_once 'application/Updater.php'; require_once 'application/Updater.php';
use \Shaarli\ThemeUtils; use \Shaarli\ThemeUtils;
use \Shaarli\Config\ConfigManager;
// Ensure the PHP version is supported // Ensure the PHP version is supported
try { try {

View file

@ -1,9 +1,10 @@
<?php <?php
use Shaarli\Config\ConfigManager;
/** /**
* ApplicationUtils' tests * ApplicationUtils' tests
*/ */
require_once 'application/config/ConfigManager.php';
require_once 'application/ApplicationUtils.php'; require_once 'application/ApplicationUtils.php';
/** /**

View file

@ -1,4 +1,5 @@
<?php <?php
use Shaarli\Config\ConfigManager;
/** /**
* Plugin Manager tests * Plugin Manager tests

View file

@ -1,6 +1,8 @@
<?php <?php
use Shaarli\Config\ConfigJson;
use Shaarli\Config\ConfigManager;
use Shaarli\Config\ConfigPhp;
require_once 'application/config/ConfigManager.php';
require_once 'tests/Updater/DummyUpdater.php'; require_once 'tests/Updater/DummyUpdater.php';
require_once 'inc/rain.tpl.class.php'; require_once 'inc/rain.tpl.class.php';

View file

@ -1,7 +1,8 @@
<?php <?php
namespace Shaarli\Api; namespace Shaarli\Api;
use Shaarli\Config\ConfigManager;
use Slim\Container; use Slim\Container;
use Slim\Http\Environment; use Slim\Http\Environment;
use Slim\Http\Request; use Slim\Http\Request;
@ -44,7 +45,7 @@ class ApiMiddlewareTest extends \PHPUnit_Framework_TestCase
*/ */
public function setUp() public function setUp()
{ {
$this->conf = new \ConfigManager('tests/utils/config/configJson.json.php'); $this->conf = new ConfigManager('tests/utils/config/configJson.json.php');
$this->conf->set('api.secret', 'NapoleonWasALizard'); $this->conf->set('api.secret', 'NapoleonWasALizard');
$this->refDB = new \ReferenceLinkDB(); $this->refDB = new \ReferenceLinkDB();

View file

@ -2,6 +2,7 @@
namespace Shaarli\Api\Controllers; namespace Shaarli\Api\Controllers;
use Shaarli\Config\ConfigManager;
use Slim\Container; use Slim\Container;
use Slim\Http\Environment; use Slim\Http\Environment;
@ -25,7 +26,7 @@ class GetLinkIdTest extends \PHPUnit_Framework_TestCase
protected static $testDatastore = 'sandbox/datastore.php'; protected static $testDatastore = 'sandbox/datastore.php';
/** /**
* @var \ConfigManager instance * @var ConfigManager instance
*/ */
protected $conf; protected $conf;
@ -54,7 +55,7 @@ class GetLinkIdTest extends \PHPUnit_Framework_TestCase
*/ */
public function setUp() public function setUp()
{ {
$this->conf = new \ConfigManager('tests/utils/config/configJson'); $this->conf = new ConfigManager('tests/utils/config/configJson');
$this->refDB = new \ReferenceLinkDB(); $this->refDB = new \ReferenceLinkDB();
$this->refDB->write(self::$testDatastore); $this->refDB->write(self::$testDatastore);

View file

@ -1,7 +1,7 @@
<?php <?php
namespace Shaarli\Api\Controllers; namespace Shaarli\Api\Controllers;
use Shaarli\Config\ConfigManager;
use Slim\Container; use Slim\Container;
use Slim\Http\Environment; use Slim\Http\Environment;
@ -25,7 +25,7 @@ class GetLinksTest extends \PHPUnit_Framework_TestCase
protected static $testDatastore = 'sandbox/datastore.php'; protected static $testDatastore = 'sandbox/datastore.php';
/** /**
* @var \ConfigManager instance * @var ConfigManager instance
*/ */
protected $conf; protected $conf;
@ -54,7 +54,7 @@ class GetLinksTest extends \PHPUnit_Framework_TestCase
*/ */
public function setUp() public function setUp()
{ {
$this->conf = new \ConfigManager('tests/utils/config/configJson'); $this->conf = new ConfigManager('tests/utils/config/configJson');
$this->refDB = new \ReferenceLinkDB(); $this->refDB = new \ReferenceLinkDB();
$this->refDB->write(self::$testDatastore); $this->refDB->write(self::$testDatastore);

View file

@ -1,7 +1,8 @@
<?php <?php
namespace Shaarli\Api\Controllers; namespace Shaarli\Api\Controllers;
use Shaarli\Config\ConfigManager;
use Slim\Container; use Slim\Container;
use Slim\Http\Environment; use Slim\Http\Environment;
use Slim\Http\Request; use Slim\Http\Request;
@ -22,7 +23,7 @@ class InfoTest extends \PHPUnit_Framework_TestCase
protected static $testDatastore = 'sandbox/datastore.php'; protected static $testDatastore = 'sandbox/datastore.php';
/** /**
* @var \ConfigManager instance * @var ConfigManager instance
*/ */
protected $conf; protected $conf;
@ -46,7 +47,7 @@ class InfoTest extends \PHPUnit_Framework_TestCase
*/ */
public function setUp() public function setUp()
{ {
$this->conf = new \ConfigManager('tests/utils/config/configJson.json.php'); $this->conf = new ConfigManager('tests/utils/config/configJson.json.php');
$this->refDB = new \ReferenceLinkDB(); $this->refDB = new \ReferenceLinkDB();
$this->refDB->write(self::$testDatastore); $this->refDB->write(self::$testDatastore);
@ -84,7 +85,7 @@ public function testGetInfo()
$this->assertEquals('Shaarli', $data['settings']['title']); $this->assertEquals('Shaarli', $data['settings']['title']);
$this->assertEquals('?', $data['settings']['header_link']); $this->assertEquals('?', $data['settings']['header_link']);
$this->assertEquals('UTC', $data['settings']['timezone']); $this->assertEquals('UTC', $data['settings']['timezone']);
$this->assertEquals(\ConfigManager::$DEFAULT_PLUGINS, $data['settings']['enabled_plugins']); $this->assertEquals(ConfigManager::$DEFAULT_PLUGINS, $data['settings']['enabled_plugins']);
$this->assertEquals(false, $data['settings']['default_private_links']); $this->assertEquals(false, $data['settings']['default_private_links']);
$title = 'My links'; $title = 'My links';

View file

@ -1,11 +1,10 @@
<?php <?php
namespace Shaarli\Config;
require_once 'application/config/ConfigJson.php';
/** /**
* Class ConfigJsonTest * Class ConfigJsonTest
*/ */
class ConfigJsonTest extends PHPUnit_Framework_TestCase class ConfigJsonTest extends \PHPUnit_Framework_TestCase
{ {
/** /**
* @var ConfigJson * @var ConfigJson
@ -40,7 +39,7 @@ public function testReadNonExistent()
/** /**
* Read a non existent config file -> empty array. * Read a non existent config file -> empty array.
* *
* @expectedException Exception * @expectedException \Exception
* @expectedExceptionMessage An error occurred while parsing JSON file: error code #4 * @expectedExceptionMessage An error occurred while parsing JSON file: error code #4
*/ */
public function testReadInvalidJson() public function testReadInvalidJson()
@ -112,7 +111,7 @@ public function testOverwrite()
/** /**
* Write to invalid path. * Write to invalid path.
* *
* @expectedException IOException * @expectedException \IOException
*/ */
public function testWriteInvalidArray() public function testWriteInvalidArray()
{ {
@ -123,7 +122,7 @@ public function testWriteInvalidArray()
/** /**
* Write to invalid path. * Write to invalid path.
* *
* @expectedException IOException * @expectedException \IOException
*/ */
public function testWriteInvalidBlank() public function testWriteInvalidBlank()
{ {

View file

@ -1,4 +1,5 @@
<?php <?php
namespace Shaarli\Config;
/** /**
* Unit tests for Class ConfigManagerTest * Unit tests for Class ConfigManagerTest
@ -6,7 +7,7 @@
* Note: it only test the manager with ConfigJson, * Note: it only test the manager with ConfigJson,
* ConfigPhp is only a workaround to handle the transition to JSON type. * ConfigPhp is only a workaround to handle the transition to JSON type.
*/ */
class ConfigManagerTest extends PHPUnit_Framework_TestCase class ConfigManagerTest extends \PHPUnit_Framework_TestCase
{ {
/** /**
* @var ConfigManager * @var ConfigManager
@ -83,7 +84,7 @@ public function testSetWriteGetNested()
/** /**
* Set with an empty key. * Set with an empty key.
* *
* @expectedException Exception * @expectedException \Exception
* @expectedExceptionMessageRegExp #^Invalid setting key parameter. String expected, got.*# * @expectedExceptionMessageRegExp #^Invalid setting key parameter. String expected, got.*#
*/ */
public function testSetEmptyKey() public function testSetEmptyKey()
@ -94,7 +95,7 @@ public function testSetEmptyKey()
/** /**
* Set with an array key. * Set with an array key.
* *
* @expectedException Exception * @expectedException \Exception
* @expectedExceptionMessageRegExp #^Invalid setting key parameter. String expected, got.*# * @expectedExceptionMessageRegExp #^Invalid setting key parameter. String expected, got.*#
*/ */
public function testSetArrayKey() public function testSetArrayKey()
@ -105,7 +106,7 @@ public function testSetArrayKey()
/** /**
* Try to write the config without mandatory parameter (e.g. 'login'). * Try to write the config without mandatory parameter (e.g. 'login').
* *
* @expectedException MissingFieldConfigException * @expectedException Shaarli\Config\MissingFieldConfigException
*/ */
public function testWriteMissingParameter() public function testWriteMissingParameter()
{ {

View file

@ -1,11 +1,10 @@
<?php <?php
namespace Shaarli\Config;
require_once 'application/config/ConfigPhp.php';
/** /**
* Class ConfigPhpTest * Class ConfigPhpTest
*/ */
class ConfigPhpTest extends PHPUnit_Framework_TestCase class ConfigPhpTest extends \PHPUnit_Framework_TestCase
{ {
/** /**
* @var ConfigPhp * @var ConfigPhp

View file

@ -1,4 +1,6 @@
<?php <?php
namespace Shaarli\Config;
/** /**
* Config' tests * Config' tests
*/ */
@ -8,7 +10,7 @@
/** /**
* Unitary tests for Shaarli config related functions * Unitary tests for Shaarli config related functions
*/ */
class ConfigPluginTest extends PHPUnit_Framework_TestCase class ConfigPluginTest extends \PHPUnit_Framework_TestCase
{ {
/** /**
* Test save_plugin_config with valid data. * Test save_plugin_config with valid data.
@ -39,7 +41,7 @@ public function testSavePluginConfigValid()
/** /**
* Test save_plugin_config with invalid data. * Test save_plugin_config with invalid data.
* *
* @expectedException PluginConfigOrderException * @expectedException Shaarli\Config\PluginConfigOrderException
*/ */
public function testSavePluginConfigInvalid() public function testSavePluginConfigInvalid()
{ {

View file

@ -1,4 +1,5 @@
<?php <?php
use Shaarli\Config\ConfigManager;
require_once 'plugins/isso/isso.php'; require_once 'plugins/isso/isso.php';

View file

@ -1,4 +1,5 @@
<?php <?php
use Shaarli\Config\ConfigManager;
/** /**
* PluginMarkdownTest.php * PluginMarkdownTest.php

View file

@ -1,4 +1,5 @@
<?php <?php
use Shaarli\Config\ConfigManager;
require_once 'plugins/pubsubhubbub/pubsubhubbub.php'; require_once 'plugins/pubsubhubbub/pubsubhubbub.php';
require_once 'application/Router.php'; require_once 'application/Router.php';

View file

@ -1,5 +1,4 @@
<?php <?php
/** /**
* PluginQrcodeTest.php * PluginQrcodeTest.php
*/ */

View file

@ -1,4 +1,5 @@
<?php <?php
use Shaarli\Config\ConfigManager;
/** /**
* PluginReadityourselfTest.php.php * PluginReadityourselfTest.php.php

View file

@ -1,4 +1,5 @@
<?php <?php
use Shaarli\Config\ConfigManager;
/** /**
* PluginWallabagTest.php.php * PluginWallabagTest.php.php