PathProcessorLanguage.php in Zircon Profile 8.0
File
core/modules/language/src/HttpKernel/PathProcessorLanguage.php
View source
<?php
namespace Drupal\language\HttpKernel;
use Drupal\Component\Utility\Unicode;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\PathProcessor\InboundPathProcessorInterface;
use Drupal\Core\PathProcessor\OutboundPathProcessorInterface;
use Drupal\Core\Render\BubbleableMetadata;
use Drupal\language\ConfigurableLanguageManagerInterface;
use Drupal\language\EventSubscriber\ConfigSubscriber;
use Drupal\language\LanguageNegotiatorInterface;
use Symfony\Component\HttpFoundation\Request;
use Drupal\Core\Session\AccountInterface;
class PathProcessorLanguage implements InboundPathProcessorInterface, OutboundPathProcessorInterface {
protected $config;
protected $languageManager;
protected $negotiator;
protected $processors;
protected $multilingual;
protected $configSubscriber;
public function __construct(ConfigFactoryInterface $config, ConfigurableLanguageManagerInterface $language_manager, LanguageNegotiatorInterface $negotiator, AccountInterface $current_user, ConfigSubscriber $config_subscriber) {
$this->config = $config;
$this->languageManager = $language_manager;
$this->negotiator = $negotiator;
$this->negotiator
->setCurrentUser($current_user);
$this->configSubscriber = $config_subscriber;
}
public function processInbound($path, Request $request) {
if (!empty($path)) {
$scope = 'inbound';
if (!isset($this->processors[$scope])) {
$this
->initProcessors($scope);
}
foreach ($this->processors[$scope] as $instance) {
$path = $instance
->processInbound($path, $request);
}
}
return $path;
}
public function processOutbound($path, &$options = array(), Request $request = NULL, BubbleableMetadata $bubbleable_metadata = NULL) {
if (!isset($this->multilingual)) {
$this->multilingual = $this->languageManager
->isMultilingual();
}
if ($this->multilingual) {
$this->negotiator
->reset();
$scope = 'outbound';
if (!isset($this->processors[$scope])) {
$this
->initProcessors($scope);
}
foreach ($this->processors[$scope] as $instance) {
$path = $instance
->processOutbound($path, $options, $request, $bubbleable_metadata);
}
if (empty($this->processors[$scope])) {
unset($options['language']);
}
}
return $path;
}
protected function initProcessors($scope) {
$interface = '\\Drupal\\Core\\PathProcessor\\' . Unicode::ucfirst($scope) . 'PathProcessorInterface';
$this->processors[$scope] = array();
$weights = [];
foreach ($this->languageManager
->getLanguageTypes() as $type) {
foreach ($this->negotiator
->getNegotiationMethods($type) as $method_id => $method) {
if (!isset($this->processors[$scope][$method_id])) {
$reflector = new \ReflectionClass($method['class']);
if ($reflector
->implementsInterface($interface)) {
$this->processors[$scope][$method_id] = $this->negotiator
->getNegotiationMethodInstance($method_id);
$weights[$method_id] = $method['weight'];
}
}
}
}
uksort($this->processors[$scope], function ($method_id_a, $method_id_b) use ($weights) {
$a_weight = $weights[$method_id_a];
$b_weight = $weights[$method_id_b];
if ($a_weight == $b_weight) {
return 0;
}
return $a_weight < $b_weight ? -1 : 1;
});
}
public function initConfigSubscriber() {
$this->configSubscriber
->setPathProcessorLanguage($this);
}
public function reset() {
$this->processors = array();
}
}