PSR-4 compliance & autoloader

master
Gilles Crettenand 11 years ago
parent 6690b9fe55
commit ab2092e9c3

@ -1,5 +1,7 @@
<?php <?php
namespace BSR\Lib;
class Configuration { class Configuration {
private static $instance = null; private static $instance = null;
@ -62,7 +64,7 @@ class Configuration {
require_once($this->custom_config); require_once($this->custom_config);
if(! isset($configuration) || ! is_array($configuration)) { if(! isset($configuration) || ! is_array($configuration)) {
throw new RuntimeException("You custom configuration in '{$this->custom_config}' must be in a variable named '\$configuration' and be an array."); throw new \RuntimeException("You custom configuration in '{$this->custom_config}' must be in a variable named '\$configuration' and be an array.");
} }
$this->values = array_replace_recursive($this->values, $configuration); $this->values = array_replace_recursive($this->values, $configuration);
@ -74,7 +76,7 @@ class Configuration {
$keys = explode('.', $key); $keys = explode('.', $key);
foreach ($keys as $k) { foreach ($keys as $k) {
if (!is_array($data)) { if (!is_array($data)) {
throw new Exception("Try to access non-array as array, key '$key''"); throw new \Exception("Try to access non-array as array, key '$key''");
} }
if (!isset($data[$k])) { if (!isset($data[$k])) {
return $default; return $default;

@ -0,0 +1,9 @@
<?php
namespace BSR\Lib\Exception;
class BookNotFoundException extends WebException {
public function __construct($code) {
parent::__construct('BookNotFound', "The book with code $code was not found", -404);
}
}

@ -0,0 +1,8 @@
<?php
namespace BSR\Lib\Exception;
/**
* Exception raised when an invalid attribute name is accessed
*/
class InvalidAttributeException extends \Exception { }

@ -0,0 +1,19 @@
<?php
namespace BSR\Lib\Exception;
class SqlException extends \Exception
{
private $query;
public function __construct($message = "Sql Error", $query = "")
{
$this->query = $query;
parent::__construct($message, 0);
}
public function getSqlError()
{
return $this->getMessage().' while executing: '.$this->query;
}
}

@ -0,0 +1,24 @@
<?php
namespace BSR\Lib\Exception;
class WebException extends \Exception
{
private $excname;
/**
* @param string $name
* @param string $reason
* @param int $code
*/
function __construct($name, $reason, $code)
{
$this->excname = $name;
parent::__construct($reason, $code);
}
public function getName()
{
return $this->excname;
}
}

@ -1,5 +1,9 @@
<?php <?php
namespace BSR\Lib\Search;
use BSR\Lib\Configuration;
mb_http_output('UTF-8'); mb_http_output('UTF-8');
class BookSearch class BookSearch

@ -1,5 +1,9 @@
<?php <?php
namespace BSR\Lib;
use BSR\Lib\Exception\WebException;
abstract class WebService abstract class WebService
{ {
private $func = null; private $func = null;
@ -37,7 +41,7 @@ abstract class WebService
$data["error"]["name"] = $e->getName(); $data["error"]["name"] = $e->getName();
$data["error"]["reason"] = $e->getMessage(); $data["error"]["reason"] = $e->getMessage();
$this->log(sprintf("Failure : [%s] %s", $e->getCode(), $e->getName())); $this->log(sprintf("Failure : [%s] %s", $e->getCode(), $e->getName()));
} catch (Exception $e) { } catch (\Exception $e) {
$data["unexpected error"]["message"] = $e->getMessage(); $data["unexpected error"]["message"] = $e->getMessage();
$this->log(sprintf("Unexpected exception : %s", $e->getMessage())); $this->log(sprintf("Unexpected exception : %s", $e->getMessage()));
} }
@ -74,7 +78,7 @@ abstract class WebService
throw new WebException ("CallFunction", "'func' method not available", -3); throw new WebException ("CallFunction", "'func' method not available", -3);
} }
$rm = new ReflectionMethod ($this, $this->func); $rm = new \ReflectionMethod($this, $this->func);
$nbParams = count($params); $nbParams = count($params);
$nbArgsFix = $rm->getNumberOfRequiredParameters(); $nbArgsFix = $rm->getNumberOfRequiredParameters();
$nbArgs = $rm->getNumberOfParameters(); $nbArgs = $rm->getNumberOfParameters();

@ -1,6 +1,6 @@
<?php <?php
require_once('DbMapping.php'); namespace BSR\Lib\db;
/** /**
* AudioBook is mapped on a Notice from NetBiblio * AudioBook is mapped on a Notice from NetBiblio

@ -1,5 +1,10 @@
<?php <?php
namespace BSR\Lib\db;
use BSR\Lib\Configuration;
use BSR\Lib\Exception\SqlException;
class Connection class Connection
{ {
// Internal variable to hold the connection // Internal variable to hold the connection
@ -52,7 +57,7 @@ class Connection
final private function __clone() {} final private function __clone() {}
} }
class OdbcResultSet implements Iterator, ArrayAccess class OdbcResultSet implements \Iterator, \ArrayAccess
{ {
public $length; public $length;
@ -79,7 +84,7 @@ class OdbcResultSet implements Iterator, ArrayAccess
$this->results[] = $data; $this->results[] = $data;
} }
}; };
} catch (Exception $e) { } catch (\Exception $e) {
print($e->getMessage()); print($e->getMessage());
} }
@ -137,7 +142,7 @@ class OdbcResultSet implements Iterator, ArrayAccess
public function offsetUnset($offset) public function offsetUnset($offset)
{ {
throw new RuntimeException("This makes no sense at all."); throw new \RuntimeException("This makes no sense at all.");
} }
// Iterator // Iterator

@ -1,5 +1,9 @@
<?php <?php
namespace BSR\Lib\db;
use BSR\Lib\Exception\InvalidAttributeException;
/** /**
* Base class for mapping objects. inherit you database filled objects from here. * Base class for mapping objects. inherit you database filled objects from here.
* *
@ -103,7 +107,7 @@ abstract class DbMapping
* @return DbMapping * @return DbMapping
*/ */
public static function find($id) { public static function find($id) {
throw new RuntimeException("This method must be implemented in child classes."); throw new \RuntimeException("This method must be implemented in child classes.");
} }
/** /**

@ -1,6 +1,7 @@
<?php <?php
require_once('DbMapping.php');
require_once('AudioBook.php'); namespace BSR\Lib\db;
use BSR\Lib\Configuration;
/** /**
* User is mapped on the Useraccounts table. Contains user information : id, login, firstName, lastName, displayName. * User is mapped on the Useraccounts table. Contains user information : id, login, firstName, lastName, displayName.

@ -1,17 +1,14 @@
<?php <?php
/* namespace BSR;
* Copyright Mathieu Schroeter for the BSR, 2013
* Modif Simon Schulé pour la BSR, 2013. 2014
* Copyright Gilles Crettenand for the BSR, 2015
*/
require_once "global.php"; use BSR\Lib\Configuration;
require_once "mobile.webservice.php"; use BSR\Lib\db\AudioBook;
use BSR\Lib\db\Connection;
require_once "lib/AudioBook.php"; use BSR\Lib\db\User;
require_once "lib/User.php"; use BSR\Lib\Exception\WebException;
require_once "lib/BookSearch.php"; use BSR\Lib\Search\BookSearch;
use BSR\Lib\WebService;
class NetBiblio extends WebService class NetBiblio extends WebService
{ {
@ -416,14 +413,14 @@ class NetBiblio extends WebService
$bs = new BookSearch(); $bs = new BookSearch();
if (isset($queryArray['queryType'])) { if (isset($queryArray['queryType'])) {
$bs->addSortField('author', SolrQuery::ORDER_ASC); $bs->addSortField('author', \SolrQuery::ORDER_ASC);
$bs->addSortField('title', SolrQuery::ORDER_ASC); $bs->addSortField('title', \SolrQuery::ORDER_ASC);
$bs->addSortField('producerCode'); $bs->addSortField('producerCode');
$bs->addSortField('mediaType', SolrQuery::ORDER_ASC); $bs->addSortField('mediaType', \SolrQuery::ORDER_ASC);
} else { } else {
$bs->addSortField('availabilityDate'); $bs->addSortField('availabilityDate');
$bs->addSortField('author', SolrQuery::ORDER_ASC); $bs->addSortField('author', \SolrQuery::ORDER_ASC);
$bs->addSortField('title', SolrQuery::ORDER_ASC); $bs->addSortField('title', \SolrQuery::ORDER_ASC);
} }
if (isset($queryArray['queryText']) && strlen($queryArray['queryText']) > 0) { if (isset($queryArray['queryText']) && strlen($queryArray['queryText']) > 0) {
@ -443,7 +440,7 @@ class NetBiblio extends WebService
}); });
if (count($selectedGenres) > 0) { if (count($selectedGenres) > 0) {
$selectedGenres = array_map(function ($c) { $selectedGenres = array_map(function ($c) {
return 'genreCode:'.SolrUtils::escapeQueryChars($c); return 'genreCode:'.\SolrUtils::escapeQueryChars($c);
}, $selectedGenres); }, $selectedGenres);
$bs->addQuery('('.implode(' OR ', $selectedGenres).')', null, false); $bs->addQuery('('.implode(' OR ', $selectedGenres).')', null, false);
} }
@ -516,7 +513,7 @@ class NetBiblio extends WebService
try { try {
$results = $s->getResults(0, $itemsByGroup); $results = $s->getResults(0, $itemsByGroup);
} catch(SolrClientException $e) { } catch(\SolrClientException $e) {
throw new WebException ("SolrError", $e->getMessage(), -710); throw new WebException ("SolrError", $e->getMessage(), -710);
} }

@ -1,49 +0,0 @@
<?php
class WebException extends Exception
{
private $excname;
/**
* @param string $name
* @param string $reason
* @param int $code
*/
function __construct($name, $reason, $code)
{
$this->excname = $name;
parent::__construct($reason, $code);
}
public function getName()
{
return $this->excname;
}
}
class SqlException extends Exception
{
private $query;
public function __construct($message = "Sql Error", $query = "")
{
$this->query = $query;
parent::__construct($message, 0);
}
public function getSqlError()
{
return $this->getMessage().' while executing: '.$this->query;
}
}
class BookNotFoundException extends WebException {
public function __construct($code) {
parent::__construct('BookNotFound', "The book with code $code was not found", -404);
}
}
/**
* Exception raised when an invalid attribute name is accessed
*/
class InvalidAttributeException extends Exception { }

@ -1,5 +0,0 @@
<?php
require_once('exceptions.php');
require_once('configuration.php');
require_once('lib/Connection.php');

@ -0,0 +1,20 @@
<?php
namespace BSR;
ini_set('display_errors', 'On');
// register an autoloader to automatically load classes
// the namespace for the class must begin with BSR and
// otherwise respect the PSR-4 standard
spl_autoload_register(function ($class) {
$class = substr($class, strlen('BSR'));
$path = sprintf('%s/%s.php', __DIR__, str_replace('\\', '/', $class));
if (file_exists($path)) {
require $path;
}
});
$web = new NetBiblio();
$web->Run();

@ -1,6 +1,4 @@
<?php <?php
require_once "mobile.netbiblio.php"; // this file is here for compatibility purpose, do not delete
require_once('index.php');
$web = new NetBiblio();
$web->Run();
Loading…
Cancel
Save