From 54f96b9938112940840222a776ee2a621c895849 Mon Sep 17 00:00:00 2001 From: Sobak Date: Thu, 21 Jul 2016 17:09:48 +0200 Subject: [PATCH] Introduce PSR-4 autoloading --- .gitignore | 1 + composer.json | 11 ++-- index.php | 4 +- lib/auto.php | 38 -------------- lib/configuration.php | 5 ++ .../abstract.php => data/AbstractData.php} | 4 +- lib/{privatebin => data}/data.php | 6 ++- lib/{privatebin => data}/db.php | 10 +++- lib/filter.php | 7 ++- lib/i18n.php | 4 +- lib/model.php | 16 +++++- lib/model/{abstract.php => AbstractModel.php} | 12 ++++- lib/model/comment.php | 11 +++- lib/model/paste.php | 11 +++- lib/persistence.php | 4 ++ lib/privatebin.php | 6 ++- lib/purgelimiter.php | 2 + lib/request.php | 2 + lib/serversalt.php | 4 ++ lib/sjcl.php | 2 + lib/trafficlimiter.php | 2 + lib/view.php | 4 ++ lib/vizhash16x16.php | 2 + tpl/bootstrap-compact.php | 50 +++++++++---------- tpl/bootstrap-dark-page.php | 48 +++++++++--------- tpl/bootstrap-dark.php | 48 +++++++++--------- tpl/bootstrap-page.php | 48 +++++++++--------- tpl/bootstrap.php | 48 +++++++++--------- tpl/page.php | 48 +++++++++--------- tst/auto.php | 8 --- tst/bootstrap.php | 5 +- tst/configuration.php | 3 ++ tst/filter.php | 3 ++ tst/i18n.php | 3 ++ tst/jsonApi.php | 8 ++- tst/model.php | 15 ++++-- tst/phpunit.xml | 2 +- tst/privatebin.php | 8 ++- tst/privatebin/data.php | 5 +- tst/privatebin/db.php | 31 ++++++------ tst/privatebinWithDb.php | 8 ++- tst/purgelimiter.php | 3 ++ tst/request.php | 3 ++ tst/serversalt.php | 3 ++ tst/sjcl.php | 3 ++ tst/trafficlimiter.php | 3 ++ tst/view.php | 4 ++ tst/vizhash16x16.php | 4 ++ 48 files changed, 342 insertions(+), 238 deletions(-) delete mode 100644 lib/auto.php rename lib/{privatebin/abstract.php => data/AbstractData.php} (98%) rename lib/{privatebin => data}/data.php (98%) rename lib/{privatebin => data}/db.php (99%) rename lib/model/{abstract.php => AbstractModel.php} (91%) delete mode 100644 tst/auto.php diff --git a/.gitignore b/.gitignore index 9a3d92c..0323182 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,6 @@ # Ignore data/, tmp/ and vendor/ data/ +!lib/data/ tmp/ vendor/ # Ignore for safety diff --git a/composer.json b/composer.json index 1280da3..bd94adf 100644 --- a/composer.json +++ b/composer.json @@ -1,15 +1,14 @@ { "name": "privatebin/privatebin", "description": "PrivateBin is a minimalist, open source online pastebin where the server has zero knowledge of pasted data. Data is encrypted/decrypted in the browser using 256 bit AES in Galois Counter mode (GCM).", - "repositories": [ - { - "type": "vcs", - "url": "https://github.com/PrivateBin/PrivateBin" - } - ], "license":"zlib-acknowledgement", "require-dev": { "codacy/coverage": "dev-master", "codeclimate/php-test-reporter": "dev-master" + }, + "autoload": { + "psr-4": { + "PrivateBin\\": "lib/" + } } } diff --git a/index.php b/index.php index 06c3617..ec40497 100644 --- a/index.php +++ b/index.php @@ -14,5 +14,5 @@ define('PATH', ''); define('PUBLIC_PATH', dirname(__FILE__)); -require PATH . 'lib/auto.php'; -new privatebin; +require __DIR__ . '/vendor/autoload.php'; +new PrivateBin\privatebin; diff --git a/lib/auto.php b/lib/auto.php deleted file mode 100644 index 30ef778..0000000 --- a/lib/auto.php +++ /dev/null @@ -1,38 +0,0 @@ -_conf, $this->_getStore()); + $paste = new paste($this->_conf, $this->_getStore()); if ($pasteId !== null) $paste->setId($pasteId); return $paste; } @@ -76,10 +80,18 @@ class model */ private function _getStore() { + // FIXME + // Workaround so that config value don't need to be changed + $callable = str_replace( + array('privatebin_data', 'privatebin_db'), + array('PrivateBin\\data\\data', 'PrivateBin\\data\\db'), + $this->_conf->getKey('class', 'model') + ); + if ($this->_store === null) { $this->_store = forward_static_call( - array($this->_conf->getKey('class', 'model'), 'getInstance'), + array($callable, 'getInstance'), $this->_conf->getSection('model_options') ); } diff --git a/lib/model/abstract.php b/lib/model/AbstractModel.php similarity index 91% rename from lib/model/abstract.php rename to lib/model/AbstractModel.php index 5e0435d..c7979b2 100644 --- a/lib/model/abstract.php +++ b/lib/model/AbstractModel.php @@ -10,12 +10,20 @@ * @version 0.22 */ +namespace PrivateBin\Model; + +use Exception; +use PrivateBin\configuration; +use PrivateBin\data\AbstractData; +use PrivateBin\sjcl; +use stdClass; + /** * model_abstract * * Abstract model for PrivateBin objects. */ -abstract class model_abstract +abstract class AbstractModel { /** * Instance ID. @@ -57,7 +65,7 @@ abstract class model_abstract * @param privatebin_abstract $storage * @return void */ - public function __construct(configuration $configuration, privatebin_abstract $storage) + public function __construct(configuration $configuration, AbstractData $storage) { $this->_conf = $configuration; $this->_store = $storage; diff --git a/lib/model/comment.php b/lib/model/comment.php index 011649d..42057ab 100644 --- a/lib/model/comment.php +++ b/lib/model/comment.php @@ -10,12 +10,19 @@ * @version 0.22 */ +namespace PrivateBin\model; + +use Exception; +use PrivateBin\sjcl; +use PrivateBin\trafficlimiter; +use PrivateBin\vizhash16x16; + /** * model_comment * * Model of a PrivateBin comment. */ -class model_comment extends model_abstract +class comment extends AbstractModel { /** * Instance's parent. @@ -118,7 +125,7 @@ class model_comment extends model_abstract * @throws Exception * @return void */ - public function setPaste(model_paste $paste) + public function setPaste(paste $paste) { $this->_paste = $paste; $this->_data->meta->pasteid = $paste->getId(); diff --git a/lib/model/paste.php b/lib/model/paste.php index 57b86b6..4294614 100644 --- a/lib/model/paste.php +++ b/lib/model/paste.php @@ -10,12 +10,19 @@ * @version 0.22 */ +namespace PrivateBin\model; + +use Exception; +use PrivateBin\privatebin; +use PrivateBin\serversalt; +use PrivateBin\sjcl; + /** * model_paste * * Model of a PrivateBin paste. */ -class model_paste extends model_abstract +class paste extends AbstractModel { /** * Get paste data. @@ -130,7 +137,7 @@ class model_paste extends model_abstract { throw new Exception('Invalid data.', 62); } - $comment = new model_comment($this->_conf, $this->_store); + $comment = new comment($this->_conf, $this->_store); $comment->setPaste($this); $comment->setParentId($parentId); if ($commentId !== null) $comment->setId($commentId); diff --git a/lib/persistence.php b/lib/persistence.php index d65f844..28259de 100644 --- a/lib/persistence.php +++ b/lib/persistence.php @@ -10,6 +10,10 @@ * @version 0.22 */ +namespace PrivateBin; + +use Exception; + /** * persistence * diff --git a/lib/privatebin.php b/lib/privatebin.php index eb6f9cb..f1f15a8 100644 --- a/lib/privatebin.php +++ b/lib/privatebin.php @@ -10,6 +10,10 @@ * @version 0.22 */ +namespace PrivateBin; + +use Exception; + /** * privatebin * @@ -419,7 +423,7 @@ class privatebin } // translate all the formatter options - $formatters = array_map(array('i18n', 'translate'), $this->_conf->getSection('formatter_options')); + $formatters = array_map(array('PrivateBin\\i18n', 'translate'), $this->_conf->getSection('formatter_options')); // set language cookie if that functionality was enabled $languageselection = ''; diff --git a/lib/purgelimiter.php b/lib/purgelimiter.php index d8c595a..36fa9ce 100644 --- a/lib/purgelimiter.php +++ b/lib/purgelimiter.php @@ -10,6 +10,8 @@ * @version 0.22 */ +namespace PrivateBin; + /** * purgelimiter * diff --git a/lib/request.php b/lib/request.php index 1339e84..d5d9c9d 100644 --- a/lib/request.php +++ b/lib/request.php @@ -10,6 +10,8 @@ * @version 0.22 */ +namespace PrivateBin; + /** * request * diff --git a/lib/serversalt.php b/lib/serversalt.php index 91cfcdc..fe28fef 100644 --- a/lib/serversalt.php +++ b/lib/serversalt.php @@ -10,6 +10,10 @@ * @version 0.22 */ +namespace PrivateBin; + +use Exception; + /** * serversalt * diff --git a/lib/sjcl.php b/lib/sjcl.php index ae09c9c..cc79cea 100644 --- a/lib/sjcl.php +++ b/lib/sjcl.php @@ -10,6 +10,8 @@ * @version 0.22 */ +namespace PrivateBin; + /** * sjcl * diff --git a/lib/trafficlimiter.php b/lib/trafficlimiter.php index b0fc5a7..ed1f534 100644 --- a/lib/trafficlimiter.php +++ b/lib/trafficlimiter.php @@ -10,6 +10,8 @@ * @version 0.22 */ +namespace PrivateBin; + /** * trafficlimiter * diff --git a/lib/view.php b/lib/view.php index 4595200..a63c2d2 100644 --- a/lib/view.php +++ b/lib/view.php @@ -10,6 +10,10 @@ * @version 0.22 */ +namespace PrivateBin; + +use Exception; + /** * view * diff --git a/lib/vizhash16x16.php b/lib/vizhash16x16.php index 47b725d..314a399 100644 --- a/lib/vizhash16x16.php +++ b/lib/vizhash16x16.php @@ -11,6 +11,8 @@ * @version 0.0.4 beta PrivateBin 0.22 */ +namespace PrivateBin; + /** * vizhash16x16 * diff --git a/tpl/bootstrap-compact.php b/tpl/bootstrap-compact.php index e39652e..7d299ad 100644 --- a/tpl/bootstrap-compact.php +++ b/tpl/bootstrap-compact.php @@ -5,7 +5,7 @@ - <?php echo i18n::_('PrivateBin'); ?> + <?php echo PrivateBin\i18n::_('PrivateBin'); ?>
@@ -202,9 +202,9 @@ endif; ?> - - -
@@ -238,16 +238,16 @@ endif; ?>
-

-

+

-

- in the browser using 256 bits AES. More information on the project page.'); ?> + in the browser using 256 bits AES. More information on the project page.'); ?>

diff --git a/tpl/bootstrap-dark-page.php b/tpl/bootstrap-dark-page.php index 8f264cd..10cb75d 100644 --- a/tpl/bootstrap-dark-page.php +++ b/tpl/bootstrap-dark-page.php @@ -5,7 +5,7 @@ - <?php echo i18n::_('PrivateBin'); ?> + <?php echo PrivateBin\i18n::_('PrivateBin'); ?>
  • checked="checked" /> - +
  • checked="checked" /> - +
  • checked="checked" /> - +