Skip to content
Snippets Groups Projects
Commit bc6600d5 authored by guedressel's avatar guedressel
Browse files

Optimizing for code sniffer complaints

parent 0b8d4243
No related branches found
No related tags found
No related merge requests found
...@@ -12,19 +12,29 @@ use Drupal\Core\Site\Settings; ...@@ -12,19 +12,29 @@ use Drupal\Core\Site\Settings;
class OpcacheCtlAccess implements AccessInterface { class OpcacheCtlAccess implements AccessInterface {
/** /**
* List of IP addresses / network addresses allowed to access opcachectl * List of IP addresses allowed to access protected opcachectl routes.
* sites.
* *
* settings.php: * Example configuration in settings.php:
*
* ```
* $settings['opcachectl_reset_remote_addresses'] = ['127.0.0.1', '::1']; * $settings['opcachectl_reset_remote_addresses'] = ['127.0.0.1', '::1'];
* ```
* *
* @var array * @var array
*/ */
protected $authorizedAddresses = []; protected $authorizedAddresses = [];
/** /**
* Token required to access protected opcachectl routes.
*
* This Token will only be checked, if the request is made from a client
* with an address not listed in $authorizedAddresses.
*
* Generate token via * Generate token via
*
* ```
* #> cat /dev/urandom | tr -dc 'a-zA-Z0-9' | fold -w 32 | head -n 1 * #> cat /dev/urandom | tr -dc 'a-zA-Z0-9' | fold -w 32 | head -n 1
* ```
* *
* settings.php: * settings.php:
* $settings['opcachectl_reset_token'] = 'somerandomvalue'; * $settings['opcachectl_reset_token'] = 'somerandomvalue';
...@@ -35,7 +45,6 @@ class OpcacheCtlAccess implements AccessInterface { ...@@ -35,7 +45,6 @@ class OpcacheCtlAccess implements AccessInterface {
/** /**
* Constructs a new OpcacheCtlController object. * Constructs a new OpcacheCtlController object.
*
*/ */
public function __construct() { public function __construct() {
$this->requestToken = trim(Settings::get("opcachectl_reset_token") ?? ''); $this->requestToken = trim(Settings::get("opcachectl_reset_token") ?? '');
...@@ -43,12 +52,14 @@ class OpcacheCtlAccess implements AccessInterface { ...@@ -43,12 +52,14 @@ class OpcacheCtlAccess implements AccessInterface {
} }
/** /**
* A custom access check. * A custom access check for protected opcachectl routes.
* *
* @return \Drupal\Core\Access\AccessResult * @return \Drupal\Core\Access\AccessResult
* Result of access check.
*/ */
public function access() { public function access() {
// TODO: set $request to method arguments once on Drupal 11 - see https://www.drupal.org/project/drupal/issues/2786941 // TODO: Set $request to method arguments once on
// Drupal 11 - see https://www.drupal.org/project/drupal/issues/2786941
$request = \Drupal::request(); $request = \Drupal::request();
$ip = $request->getClientIp(); $ip = $request->getClientIp();
if ($ip == $_SERVER['SERVER_ADDR'] || $ip == "127.0.0.1") { if ($ip == $_SERVER['SERVER_ADDR'] || $ip == "127.0.0.1") {
......
<?php <?php
/**
* @file
* Contains \Drupal\opcachectl\Controller\OpcacheCtlController.
*/
namespace Drupal\opcachectl\Controller; namespace Drupal\opcachectl\Controller;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Controller\ControllerBase; use Drupal\Core\Controller\ControllerBase;
use Drupal\Core\Logger\LoggerChannelFactory; use Drupal\Core\Logger\LoggerChannelFactory;
use Drupal\Core\Site\Settings;
use Symfony\Component\DependencyInjection\ContainerInterface; use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\JsonResponse; use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Request;
...@@ -28,34 +21,14 @@ class OpcacheCtlController extends ControllerBase { ...@@ -28,34 +21,14 @@ class OpcacheCtlController extends ControllerBase {
*/ */
protected $logger; protected $logger;
/**
* List of IP addresses / network addresses allowed to access opcachectl
* sites.
*
* @var array
*/
protected $authorizedAddresses = [];
/**
* Generate token via
* #> cat /dev/urandom | tr -dc 'a-zA-Z0-9' | fold -w 32 | head -n 1
*
* settings.php:
* $config['opcachectl']['reset_token'] = 'somerandomvalue';
*
* @var string
*/
protected $resetToken;
/** /**
* Constructs a new OpcacheCtlController object. * Constructs a new OpcacheCtlController object.
* *
* @param LoggerChannelFactory $logger * @param \Drupal\Core\Logger\LoggerChannelFactory $logger
* @param ConfigFactoryInterface $config_factory * Logger factory to use.
*/ */
public function __construct(LoggerChannelFactory $logger, ConfigFactoryInterface $config_factory) { public function __construct(LoggerChannelFactory $logger) {
$this->logger = $logger->get('opcachectl'); $this->logger = $logger->get('opcachectl');
$this->resetToken = trim(Settings::get("opcachectl_reset_token") ?? '');
} }
/** /**
...@@ -64,14 +37,13 @@ class OpcacheCtlController extends ControllerBase { ...@@ -64,14 +37,13 @@ class OpcacheCtlController extends ControllerBase {
public static function create(ContainerInterface $container) { public static function create(ContainerInterface $container) {
return new static( return new static(
$container->get('logger.factory'), $container->get('logger.factory'),
$container->get('config.factory')
); );
} }
/** /**
* Callback for the PHP OPcache statistics page. * Callback for the PHP OPcache statistics page.
* *
* @return string * @return array
* The page output. * The page output.
*/ */
public function settingsPage() { public function settingsPage() {
...@@ -80,6 +52,14 @@ class OpcacheCtlController extends ControllerBase { ...@@ -80,6 +52,14 @@ class OpcacheCtlController extends ControllerBase {
return $output; return $output;
} }
/**
* Create JSON response to be used by opcachectl "API" routes.
*
* May help to keep responses consistent over various routes.
*
* @return \Symfony\Component\HttpFoundation\JsonResponse
* The Response.
*/
protected function createControlResponse(array $data, $status = Response::HTTP_OK) { protected function createControlResponse(array $data, $status = Response::HTTP_OK) {
$data['host'] = gethostname(); $data['host'] = gethostname();
$data['address'] = $_SERVER['SERVER_ADDR']; $data['address'] = $_SERVER['SERVER_ADDR'];
...@@ -87,6 +67,15 @@ class OpcacheCtlController extends ControllerBase { ...@@ -87,6 +67,15 @@ class OpcacheCtlController extends ControllerBase {
return new JsonResponse($data, $status); return new JsonResponse($data, $status);
} }
/**
* Request current OPcache status.
*
* @param \Symfony\Component\HttpFoundation\Request $request
* The Request.
*
* @return \Symfony\Component\HttpFoundation\JsonResponse
* JSON Response describing current OPcache status.
*/
public function controlGet(Request $request) { public function controlGet(Request $request) {
if (!function_exists('opcache_get_status')) { if (!function_exists('opcache_get_status')) {
return $this->createControlResponse(['error' => 'PHP OPcache not enabled'], Response::HTTP_INTERNAL_SERVER_ERROR); return $this->createControlResponse(['error' => 'PHP OPcache not enabled'], Response::HTTP_INTERNAL_SERVER_ERROR);
...@@ -94,6 +83,15 @@ class OpcacheCtlController extends ControllerBase { ...@@ -94,6 +83,15 @@ class OpcacheCtlController extends ControllerBase {
return $this->createControlResponse(['status' => opcache_get_status(FALSE)]); return $this->createControlResponse(['status' => opcache_get_status(FALSE)]);
} }
/**
* Request to reset OPcache.
*
* @param \Symfony\Component\HttpFoundation\Request $request
* The request.
*
* @return \Symfony\Component\HttpFoundation\JsonResponse
* JSON response describing operation error or current OPcache status.
*/
public function controlPurge(Request $request) { public function controlPurge(Request $request) {
if (!function_exists('opcache_get_status')) { if (!function_exists('opcache_get_status')) {
return $this->createControlResponse(['error' => 'PHP OPcache not enabled'], Response::HTTP_INTERNAL_SERVER_ERROR); return $this->createControlResponse(['error' => 'PHP OPcache not enabled'], Response::HTTP_INTERNAL_SERVER_ERROR);
......
<?php <?php
/**
* @file
* Contains \Drupal\opcachectl\Controller\OpcacheReportController.
*/
namespace Drupal\opcachectl\Controller; namespace Drupal\opcachectl\Controller;
use Drupal\Core\Controller\ControllerBase; use Drupal\Core\Controller\ControllerBase;
......
...@@ -16,7 +16,8 @@ class ConfirmResetOpcacheForm extends ConfirmFormBase { ...@@ -16,7 +16,8 @@ class ConfirmResetOpcacheForm extends ConfirmFormBase {
*/ */
public function buildForm(array $form, FormStateInterface $form_state) { public function buildForm(array $form, FormStateInterface $form_state) {
// TODO: check for opcache_get_status()[opcache_enabled] // TODO: check for opcache_get_status()[opcache_enabled]
// TODO: check for opcache_get_status()[restart_pending] or/and opcache_get_status()[restart_in_progress] // TODO: check for opcache_get_status()[restart_pending] or/and
// opcache_get_status()[restart_in_progress]
return parent::buildForm($form, $form_state); return parent::buildForm($form, $form_state);
} }
......
...@@ -2,23 +2,42 @@ ...@@ -2,23 +2,42 @@
namespace Drupal\opcachectl\Twig\Extension; namespace Drupal\opcachectl\Twig\Extension;
use Drupal\Core\StringTranslation\ByteSizeMarkup;
use Twig\Extension\AbstractExtension; use Twig\Extension\AbstractExtension;
use Twig\TwigFilter; use Twig\TwigFilter;
/**
* Twig extensions to format byte counts.
*/
class FormatSize extends AbstractExtension { class FormatSize extends AbstractExtension {
/**
* {@inheritdoc}
*/
public function getFilters() { public function getFilters() {
return [ return [
new TwigFilter('format_size', [$this, 'formatSize']), new TwigFilter('format_size', [$this, 'formatByteSize']),
]; ];
} }
/**
* {@inheritdoc}
*/
public function getName() { public function getName() {
return 'format_size'; return 'format_size';
} }
function formatSize($size) { /**
return format_size($size); * Format byte size in human-readable format.
*
* @param float|int $size
* Bytes.
*
* @return \Drupal\Core\StringTranslation\TranslatableMarkup
* Formatted string.
*/
public function formatByteSize($size) {
return ByteSizeMarkup::create($size ?? 0);
} }
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment