[Pkg-owncloud-commits] [php-sabredav] 118/220: Added support for PSR-3.
David Prévot
taffit at moszumanska.debian.org
Thu May 12 01:21:15 UTC 2016
This is an automated email from the git hooks/post-receive script.
taffit pushed a commit to branch master
in repository php-sabredav.
commit 99f8249ac008d9b5f1642b2dd316765274d360f6
Author: Evert Pot <me at evertpot.com>
Date: Thu Mar 31 19:54:36 2016 -0400
Added support for PSR-3.
Fixes #397
---
CHANGELOG.md | 2 +
composer.json | 6 ++-
lib/DAV/CorePlugin.php | 34 +++++++++++++++++
lib/DAV/Server.php | 22 ++++++++++-
tests/Sabre/DAV/MockLogger.php | 36 +++++++++++++++++
tests/Sabre/DAV/PSR3Test.php | 87 ++++++++++++++++++++++++++++++++++++++++++
6 files changed, 184 insertions(+), 3 deletions(-)
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 9fc5d88..283cd7a 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -9,6 +9,8 @@ ChangeLog
* Support for WebDAV Resource Sharing, an upcoming standard.
* Added support for sharing in the CalDAV PDO backend! Users can now invite
others to their calendar and give them read/read-write access!
+* #397: Support for PSR-3. You can now log exceptions with your favourite
+ psr3-compatible logging tool.
* Removed database migration script for sabre/dav 1.7. To update from that
version you now first need to update to sabre/dav 3.1.
* Removed deprecated function: `Sabre\DAV\Auth\Plugin::getCurrentUser()`.
diff --git a/composer.json b/composer.json
index 87c344b..63dfb53 100644
--- a/composer.json
+++ b/composer.json
@@ -28,12 +28,14 @@
"ext-ctype" : "*",
"ext-date" : "*",
"ext-iconv" : "*",
- "lib-libxml" : ">=2.7.0"
+ "lib-libxml" : ">=2.7.0",
+ "psr/log": "^1.0"
},
"require-dev" : {
"phpunit/phpunit" : "> 4.8, <=6.0.0",
"evert/phpdoc-md" : "~0.1.0",
- "sabre/cs" : "~0.0.5"
+ "sabre/cs" : "~0.0.5",
+ "monolog/monolog": "^1.18"
},
"suggest" : {
"ext-curl" : "*",
diff --git a/lib/DAV/CorePlugin.php b/lib/DAV/CorePlugin.php
index d67d8fc..a1b0529 100644
--- a/lib/DAV/CorePlugin.php
+++ b/lib/DAV/CorePlugin.php
@@ -50,6 +50,8 @@ class CorePlugin extends ServerPlugin {
$server->on('propFind', [$this, 'propFindNode'], 120);
$server->on('propFind', [$this, 'propFindLate'], 200);
+ $server->on('exception', [$this, 'exception']);
+
}
/**
@@ -903,6 +905,38 @@ class CorePlugin extends ServerPlugin {
}
/**
+ * Listens for exception events, and automatically logs them.
+ *
+ * @param Exception $e
+ */
+ function exception($e) {
+
+ $logLevel = \Psr\Log\LogLevel::CRITICAL;
+ if ($e instanceof \Sabre\DAV\Exception) {
+ // If it's a standard sabre/dav exception, it means we have a http
+ // status code available.
+ $code = $e->getHTTPCode();
+
+ if ($code >= 400 && $code < 500) {
+ // user error
+ $logLevel = \Psr\Log\LogLevel::INFO;
+ } else {
+ // Server-side error. We mark it's as an error, but it's not
+ // critical.
+ $logLevel = \Psr\Log\LogLevel::ERROR;
+ }
+ }
+
+ $this->server->getLogger()->log(
+ $logLevel,
+ 'Uncaught exception',
+ [
+ 'exception' => $e,
+ ]
+ );
+ }
+
+ /**
* Returns a bunch of meta-data about the plugin.
*
* Providing this information is optional, and is mainly displayed by the
diff --git a/lib/DAV/Server.php b/lib/DAV/Server.php
index b376528..7506422 100644
--- a/lib/DAV/Server.php
+++ b/lib/DAV/Server.php
@@ -8,6 +8,10 @@ use Sabre\HTTP\RequestInterface;
use Sabre\HTTP\ResponseInterface;
use Sabre\HTTP\URLUtil;
use Sabre\Uri;
+use Psr\Log\LoggerAwareInterface;
+use Psr\Log\LoggerAwareTrait;
+use Psr\Log\LoggerInterface;
+use Psr\Log\NullLogger;
/**
* Main DAV server class
@@ -16,7 +20,9 @@ use Sabre\Uri;
* @author Evert Pot (http://evertpot.com/)
* @license http://sabre.io/license/ Modified BSD License
*/
-class Server extends EventEmitter {
+class Server extends EventEmitter implements LoggerAwareInterface {
+
+ use LoggerAwareTrait;
/**
* Infinity is used for some request supporting the HTTP Depth header and indicates that the operation should traverse the entire tree
@@ -431,6 +437,20 @@ class Server extends EventEmitter {
}
/**
+ * Returns the PSR-3 logger objcet.
+ *
+ * @return LoggerInterface
+ */
+ function getLogger() {
+
+ if (!$this->logger) {
+ $this->logger = new NullLogger();
+ }
+ return $this->logger;
+
+ }
+
+ /**
* Handles a http request, and execute a method based on its name
*
* @param RequestInterface $request
diff --git a/tests/Sabre/DAV/MockLogger.php b/tests/Sabre/DAV/MockLogger.php
new file mode 100644
index 0000000..0333256
--- /dev/null
+++ b/tests/Sabre/DAV/MockLogger.php
@@ -0,0 +1,36 @@
+<?php
+
+namespace Sabre\DAV;
+
+use Psr\Log\AbstractLogger;
+
+/**
+ * The MockLogger is a simple PSR-3 implementation that we can use to test
+ * whether things get logged correctly.
+ *
+ * @copyright Copyright (C) fruux GmbH. (https://fruux.com/)
+ * @author Evert Pot (http://evertpot.com/)
+ * @license http://sabre.io/license/ Modified BSD License
+ */
+class MockLogger extends AbstractLogger {
+
+ public $logs = [];
+
+ /**
+ * Logs with an arbitrary level.
+ *
+ * @param mixed $level
+ * @param string $message
+ * @param array $context
+ * @return null
+ */
+ function log($level, $message, array $context = []) {
+
+ $this->logs[] = [
+ $level,
+ $message,
+ $context
+ ];
+
+ }
+}
diff --git a/tests/Sabre/DAV/PSR3Test.php b/tests/Sabre/DAV/PSR3Test.php
new file mode 100644
index 0000000..d30fde1
--- /dev/null
+++ b/tests/Sabre/DAV/PSR3Test.php
@@ -0,0 +1,87 @@
+<?php
+
+namespace Sabre\DAV;
+
+class PSR3Test extends \PHPUnit_Framework_TestCase {
+
+ function testIsLoggerAware() {
+
+ $server = new Server();
+ $this->assertInstanceOf(
+ 'Psr\Log\LoggerAwareInterface',
+ $server
+ );
+
+ }
+
+ function testGetNullLoggerByDefault() {
+
+ $server = new Server();
+ $this->assertInstanceOf(
+ 'Psr\Log\NullLogger',
+ $server->getLogger()
+ );
+
+ }
+
+ function testSetLogger() {
+
+ $server = new Server();
+ $logger = new MockLogger();
+
+ $server->setLogger($logger);
+
+ $this->assertEquals(
+ $logger,
+ $server->getLogger()
+ );
+
+ }
+
+ /**
+ * Start the server, trigger an exception and see if the logger captured
+ * it.
+ */
+ function testLogException() {
+
+ $server = new Server();
+ $logger = new MockLogger();
+
+ $server->setLogger($logger);
+
+ // Creating a fake environment to execute http requests in.
+ $request = new \Sabre\HTTP\Request(
+ 'GET',
+ '/not-found',
+ []
+ );
+ $response = new \Sabre\HTTP\Response();
+
+ $server->httpRequest = $request;
+ $server->httpResponse = $response;
+ $server->sapi = new \Sabre\HTTP\SapiMock();
+
+ // Executing the request.
+ $server->exec();
+
+ // The request should have triggered a 404 status.
+ $this->assertEquals(404, $response->getStatus());
+
+ // We should also see this in the PSR-3 log.
+ $this->assertEquals(1, count($logger->logs));
+
+ $logItem = $logger->logs[0];
+
+ $this->assertEquals(
+ \Psr\Log\LogLevel::INFO,
+ $logItem[0]
+ );
+
+ $this->assertInstanceOf(
+ 'Exception',
+ $logItem[2]['exception']
+ );
+
+ }
+
+}
--
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-php/php-sabredav.git
More information about the Pkg-owncloud-commits
mailing list