Drupal 8.0.0 beta 12. More info: https://www.drupal.org/node/2514176

This commit is contained in:
Pantheon Automation 2015-08-17 17:00:26 -07:00 committed by Greg Anderson
commit 9921556621
13277 changed files with 1459781 additions and 0 deletions

View file

@ -0,0 +1,46 @@
<?php
/**
* @file
* Contains \Drupal\Core\Logger\LogMessageParser.
*/
namespace Drupal\Core\Logger;
/**
* Parses log messages and their placeholders.
*/
class LogMessageParser implements LogMessageParserInterface {
/**
* {@inheritdoc}
*/
public function parseMessagePlaceholders(&$message, array &$context) {
$variables = array();
$has_psr3 = FALSE;
if (($start = strpos($message, '{')) !== FALSE && strpos($message, '}') > $start) {
$has_psr3 = TRUE;
// Transform PSR3 style messages containing placeholders to
// \Drupal\Component\Utility\SafeMarkup::format() style.
$message = preg_replace('/\{(.*)\}/U', '@$1', $message);
}
foreach ($context as $key => $variable) {
// PSR3 style placeholders.
if ($has_psr3) {
// Keys are not prefixed with anything according to PSR3 specs.
// If the message is "User {username} created" the variable key will be
// just "username".
if (strpos($message, '@' . $key) !== FALSE) {
$key = '@' . $key;
}
}
if (!empty($key) && ($key[0] === '@' || $key[0] === '%' || $key[0] === '!')) {
// The key is now in \Drupal\Component\Utility\SafeMarkup::format() style.
$variables[$key] = $variable;
}
}
return $variables;
}
}

View file

@ -0,0 +1,39 @@
<?php
/**
* @file
* Contains \Drupal\Core\Logger\LogMessageParserInterface.
*/
namespace Drupal\Core\Logger;
/**
* Defines an interface for parsing log messages and their placeholders.
*/
interface LogMessageParserInterface {
/**
* Parses and transforms message and its placeholders to a common format.
*
* For a value to be considered as a placeholder should be in the following
* formats:
* - PSR3 format:
* @see https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-3-logger-interface.md#12-message
* - Drupal specific string placeholder format:
* @see \Drupal\Component\Utility\SafeMarkup::format()
*
* Values in PSR3 format will be transformed to SafeMarkup::format() format.
*
* @param string $message
* The message that contains the placeholders.
* If the message is in PSR3 style, it will be transformed to
* \Drupal\Component\Utility\SafeMarkup::format() style.
* @param array $context
* An array that may or may not contain placeholder variables.
*
* @return array
* An array of the extracted message placeholders.
*/
public function parseMessagePlaceholders(&$message, array &$context);
}

View file

@ -0,0 +1,156 @@
<?php
/**
* @file
* Contains \Drupal\Core\Logger\LoggerChannel.
*/
namespace Drupal\Core\Logger;
use Drupal\Core\Session\AccountInterface;
use Psr\Log\LoggerInterface;
use Psr\Log\LoggerTrait;
use Psr\Log\LogLevel;
use Symfony\Component\HttpFoundation\RequestStack;
/**
* Defines a logger channel that most implementations will use.
*/
class LoggerChannel implements LoggerChannelInterface {
use LoggerTrait;
/**
* The name of the channel of this logger instance.
*
* @var string
*/
protected $channel;
/**
* Map of PSR3 log constants to RFC 5424 log constants.
*
* @var array
*/
protected $levelTranslation = array(
LogLevel::EMERGENCY => RfcLogLevel::EMERGENCY,
LogLevel::ALERT => RfcLogLevel::ALERT,
LogLevel::CRITICAL => RfcLogLevel::CRITICAL,
LogLevel::ERROR => RfcLogLevel::ERROR,
LogLevel::WARNING => RfcLogLevel::WARNING,
LogLevel::NOTICE => RfcLogLevel::NOTICE,
LogLevel::INFO => RfcLogLevel::INFO,
LogLevel::DEBUG => RfcLogLevel::DEBUG,
);
/**
* An array of arrays of \Psr\Log\LoggerInterface keyed by priority.
*
* @var array
*/
protected $loggers = array();
/**
* The request stack object.
*
* @var \Symfony\Component\HttpFoundation\RequestStack
*/
protected $requestStack;
/**
* The current user object.
*
* @var \Drupal\Core\Session\AccountInterface
*/
protected $currentUser;
/**
* Constructs a LoggerChannel object
*
* @param string $channel
* The channel name for this instance.
*/
public function __construct($channel) {
$this->channel = $channel;
}
/**
* {@inheritdoc}
*/
public function log($level, $message, array $context = array()) {
// Merge in defaults.
$context += array(
'channel' => $this->channel,
'link' => '',
'user' => NULL,
'uid' => 0,
'request_uri' => '',
'referer' => '',
'ip' => '',
'timestamp' => time(),
);
// Some context values are only available when in a request context.
if ($this->requestStack && $request = $this->requestStack->getCurrentRequest()) {
$context['request_uri'] = $request->getUri();
$context['referer'] = $request->headers->get('Referer', '');
$context['ip'] = $request->getClientIP();
if ($this->currentUser) {
$context['user'] = $this->currentUser;
$context['uid'] = $this->currentUser->id();
}
}
if (is_string($level)) {
// Convert to integer equivalent for consistency with RFC 5424.
$level = $this->levelTranslation[$level];
}
// Call all available loggers.
foreach ($this->sortLoggers() as $logger) {
$logger->log($level, $message, $context);
}
}
/**
* {@inheritdoc}
*/
public function setRequestStack(RequestStack $requestStack = NULL) {
$this->requestStack = $requestStack;
}
/**
* {@inheritdoc}
*/
public function setCurrentUser(AccountInterface $current_user = NULL) {
$this->currentUser = $current_user;
}
/**
* {@inheritdoc}
*/
public function setLoggers(array $loggers) {
$this->loggers = $loggers;
}
/**
* {@inheritdoc}
*/
public function addLogger(LoggerInterface $logger, $priority = 0) {
$this->loggers[$priority][] = $logger;
}
/**
* Sorts loggers according to priority.
*
* @return array
* An array of sorted loggers by priority.
*/
protected function sortLoggers() {
$sorted = array();
krsort($this->loggers);
foreach ($this->loggers as $loggers) {
$sorted = array_merge($sorted, $loggers);
}
return $sorted;
}
}

View file

@ -0,0 +1,69 @@
<?php
/**
* @file
* Contains \Drupal\Core\Logger\LoggerChannelFactory.
*/
namespace Drupal\Core\Logger;
use Psr\Log\LoggerInterface;
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
use Symfony\Component\DependencyInjection\ContainerAwareTrait;
/**
* Defines a factory for logging channels.
*/
class LoggerChannelFactory implements LoggerChannelFactoryInterface, ContainerAwareInterface {
use ContainerAwareTrait;
/**
* Array of all instantiated logger channels keyed by channel name.
*
* @var \Drupal\Core\Logger\LoggerChannelInterface[]
*/
protected $channels = array();
/**
* An array of arrays of \Psr\Log\LoggerInterface keyed by priority.
*
* @var array
*/
protected $loggers = array();
/**
* {@inheritdoc}
*/
public function get($channel) {
if (!isset($this->channels[$channel])) {
$instance = new LoggerChannel($channel);
// If we have a container set the request_stack and current_user services
// on the channel. It is up to the channel to determine if there is a
// current request.
if ($this->container) {
$instance->setRequestStack($this->container->get('request_stack'));
$instance->setCurrentUser($this->container->get('current_user'));
}
// Pass the loggers to the channel.
$instance->setLoggers($this->loggers);
$this->channels[$channel] = $instance;
}
return $this->channels[$channel];
}
/**
* {@inheritdoc}
*/
public function addLogger(LoggerInterface $logger, $priority = 0) {
// Store it so we can pass it to potential new logger instances.
$this->loggers[$priority][] = $logger;
// Add the logger to already instantiated channels.
foreach ($this->channels as $channel) {
$channel->addLogger($logger, $priority);
}
}
}

View file

@ -0,0 +1,40 @@
<?php
/**
* @file
* Contains \Drupal\Core\Logger\LoggerChannelFactoryInterface.
*/
namespace Drupal\Core\Logger;
use Psr\Log\LoggerInterface;
/**
* Logger channel factory interface.
*/
interface LoggerChannelFactoryInterface {
/**
* Retrieves the registered logger for the requested channel.
*
* @return \Drupal\Core\Logger\LoggerChannelInterface
* The registered logger for this channel.
*/
public function get($channel);
/**
* Adds a logger.
*
* Here is were all services tagged as 'logger' are being retrieved and then
* passed to the channels after instantiation.
*
* @param \Psr\Log\LoggerInterface $logger
* The PSR-3 logger to add.
* @param int $priority
* The priority of the logger being added.
*
* @see \Drupal\Core\DependencyInjection\Compiler\RegisterLoggersPass
*/
public function addLogger(LoggerInterface $logger, $priority = 0);
}

View file

@ -0,0 +1,53 @@
<?php
/**
* @file
* Contains \Drupal\Core\Logger\LoggerChannelInterface.
*/
namespace Drupal\Core\Logger;
use Drupal\Core\Session\AccountInterface;
use Psr\Log\LoggerInterface;
use Symfony\Component\HttpFoundation\RequestStack;
/**
* Logger channel interface.
*/
interface LoggerChannelInterface extends LoggerInterface {
/**
* Sets the request stack.
*
* @param \Symfony\Component\HttpFoundation\RequestStack|null $requestStack
* The current request object.
*/
public function setRequestStack(RequestStack $requestStack = NULL);
/**
* Sets the current user.
*
* @param \Drupal\Core\Session\AccountInterface|null $current_user
* The current user object.
*/
public function setCurrentUser(AccountInterface $current_user = NULL);
/**
* Sets the loggers for this channel.
*
* @param array $loggers
* An array of arrays of \Psr\Log\LoggerInterface keyed by priority.
*/
public function setLoggers(array $loggers);
/**
* Adds a logger.
*
* @param \Psr\Log\LoggerInterface $logger
* The PSR-3 logger to add.
* @param int $priority
* The priority of the logger being added.
*/
public function addLogger(LoggerInterface $logger, $priority = 0);
}

View file

@ -0,0 +1,113 @@
<?php
/**
* @file
* Contains \Drupal\Core\Logger\RfcLogLevel.
*/
namespace Drupal\Core\Logger;
use Drupal\Core\StringTranslation\TranslationWrapper;
/**
* @defgroup logging_severity_levels Logging severity levels
* @{
* Logging severity levels as defined in RFC 5424.
*
* The constant definitions of this class correspond to the logging severity
* levels defined in RFC 5424, section 4.1.1. PHP supplies predefined LOG_*
* constants for use in the syslog() function, but their values on Windows
* builds do not correspond to RFC 5424. The associated PHP bug report was
* closed with the comment, "And it's also not a bug, as Windows just have less
* log levels," and "So the behavior you're seeing is perfectly normal."
*
* @see http://tools.ietf.org/html/rfc5424
* @see http://bugs.php.net/bug.php?id=18090
* @see http://php.net/manual/function.syslog.php
* @see http://php.net/manual/network.constants.php
* @see self::getLevels()
*
* @} End of "defgroup logging_severity_levels".
*/
/**
* Defines various logging severity levels.
*
* @ingroup logging_severity_levels
*/
class RfcLogLevel {
/**
* Log message severity -- Emergency: system is unusable.
*/
const EMERGENCY = 0;
/**
* Log message severity -- Alert: action must be taken immediately.
*/
const ALERT = 1;
/**
* Log message severity -- Critical conditions.
*/
const CRITICAL = 2;
/**
* Log message severity -- Error conditions.
*/
const ERROR = 3;
/**
* Log message severity -- Warning conditions.
*/
const WARNING = 4;
/**
* Log message severity -- Normal but significant conditions.
*/
const NOTICE = 5;
/**
* Log message severity -- Informational messages.
*/
const INFO = 6;
/**
* Log message severity -- Debug-level messages.
*/
const DEBUG = 7;
/**
* An array with the severity levels as keys and labels as values.
*
* @var array
*/
protected static $levels;
/**
* Returns a list of severity levels, as defined in RFC 5424.
*
* @return array
* Array of the possible severity levels for log messages.
*
* @see http://tools.ietf.org/html/rfc5424
* @ingroup logging_severity_levels
*/
public static function getLevels() {
if (!static::$levels) {
static::$levels = [
static::EMERGENCY => new TranslationWrapper('Emergency'),
static::ALERT => new TranslationWrapper('Alert'),
static::CRITICAL => new TranslationWrapper('Critical'),
static::ERROR => new TranslationWrapper('Error'),
static::WARNING => new TranslationWrapper('Warning'),
static::NOTICE => new TranslationWrapper('Notice'),
static::INFO => new TranslationWrapper('Info'),
static::DEBUG => new TranslationWrapper('Debug'),
];
}
return static::$levels;
}
}

View file

@ -0,0 +1,83 @@
<?php
/**
* @file
* Contains \Drupal\Core\Logger\RfcLoggerTrait.
*/
namespace Drupal\Core\Logger;
/**
* A copy of \Psr\Log\LoggerTrait that uses RFC 5424 compliant log levels.
*
* Internal Drupal logger implementations should use this trait instead of
* \Psr\Log\LoggerTrait. Callers of those implementations are responsible for
* translating any other log level format to RFC 5424 compliant integers.
*
* @see https://groups.google.com/forum/#!topic/php-fig/Rc5YDhNdGz4
* @see https://www.drupal.org/node/2267545
*/
trait RfcLoggerTrait {
/**
* Implements \Psr\Log\LoggerInterface::emergency()
*/
public function emergency($message, array $context = array()) {
$this->log(RfcLogLevel::EMERGENCY, $message, $context);
}
/**
* Implements \Psr\Log\LoggerInterface::alert()
*/
public function alert($message, array $context = array()) {
$this->log(RfcLogLevel::ALERT, $message, $context);
}
/**
* Implements \Psr\Log\LoggerInterface::critical()
*/
public function critical($message, array $context = array()) {
$this->log(RfcLogLevel::CRITICAL, $message, $context);
}
/**
* Implements \Psr\Log\LoggerInterface::error()
*/
public function error($message, array $context = array()) {
$this->log(RfcLogLevel::ERROR, $message, $context);
}
/**
* Implements \Psr\Log\LoggerInterface::warning()
*/
public function warning($message, array $context = array()) {
$this->log(RfcLogLevel::WARNING, $message, $context);
}
/**
* Implements \Psr\Log\LoggerInterface::notice()
*/
public function notice($message, array $context = array()) {
$this->log(RfcLogLevel::NOTICE, $message, $context);
}
/**
* Implements \Psr\Log\LoggerInterface::info()
*/
public function info($message, array $context = array()) {
$this->log(RfcLogLevel::INFO, $message, $context);
}
/**
* Implements \Psr\Log\LoggerInterface::debug()
*/
public function debug($message, array $context = array()) {
$this->log(RfcLogLevel::DEBUG, $message, $context);
}
/**
* Implements \Psr\Log\LoggerInterface::log()
*/
abstract public function log($level, $message, array $context = array());
}