This commit is contained in:
Loïc Guibert
2022-09-30 20:02:02 +01:00
commit 66dafc36c3
2561 changed files with 454489 additions and 0 deletions

View File

@@ -0,0 +1,141 @@
<?php
namespace Grav\Plugin\Problems\Base;
use JsonSerializable;
/**
* Class Problem
* @package Grav\Plugin\Problems\Base
*/
class Problem implements JsonSerializable
{
const LEVEL_CRITICAL = 'critical';
const LEVEL_WARNING = 'warning';
const LEVEL_NOTICE = 'notice';
/** @var string */
protected $id = '';
/** @var int */
protected $order = 0;
/** @var string */
protected $level = '';
/** @var bool */
protected $status = false;
/** @var string */
protected $msg = '';
/** @var array */
protected $details = [];
/** @var string */
protected $help = '';
/** @var string */
protected $class = '';
/**
* @param array $data
* @return void
*/
public function load(array $data): void
{
$this->set_object_vars($data);
}
/**
* @return $this
*/
public function process()
{
return $this;
}
/**
* @return string
*/
public function getId(): string
{
return $this->id;
}
/**
* @return int
*/
public function getOrder(): int
{
return $this->order;
}
/**
* @return string
*/
public function getLevel(): string
{
return $this->level;
}
/**
* @return bool
*/
public function getStatus(): bool
{
return $this->status;
}
/**
* @return string
*/
public function getMsg(): string
{
return $this->msg;
}
/**
* @return array
*/
public function getDetails(): array
{
return $this->details;
}
/**
* @return string
*/
public function getHelp(): string
{
return $this->help;
}
/**
* @return string
*/
public function getClass(): string
{
return $this->class;
}
/**
* @return array
*/
public function toArray(): array
{
return get_object_vars($this);
}
/**
* @return array
*/
public function jsonSerialize(): array
{
return $this->toArray();
}
/**
* @param array $vars
*/
protected function set_object_vars(array $vars): void
{
$has = get_object_vars($this);
foreach ($has as $name => $oldValue) {
$this->{$name} = $vars[$name] ?? null;
}
}
}

View File

@@ -0,0 +1,159 @@
<?php
namespace Grav\Plugin\Problems\Base;
use Grav\Common\Cache;
use Grav\Common\Grav;
use RocketTheme\Toolbox\Event\Event;
/**
* Class ProblemChecker
* @package Grav\Plugin\Problems\Base
*/
class ProblemChecker
{
/** @var string */
const PROBLEMS_PREFIX = 'problem-check-';
/** @var array */
protected $problems = [];
/** @var string */
protected $status_file;
public function __construct()
{
/** @var Cache $cache */
$cache = Grav::instance()['cache'];
$this->status_file = CACHE_DIR . $this::PROBLEMS_PREFIX . $cache->getKey() . '.json';
}
/**
* @return bool
*/
public function load(): bool
{
if ($this->statusFileExists()) {
$json = file_get_contents($this->status_file) ?: '';
$data = json_decode($json, true);
if (!is_array($data)) {
return false;
}
foreach ($data as $problem) {
$class = $problem['class'];
$this->problems[] = new $class($problem);
}
}
return true;
}
/**
* @return string
*/
public function getStatusFile():string
{
return $this->status_file;
}
/**
* @return bool
*/
public function statusFileExists(): bool
{
return file_exists($this->status_file);
}
/**
* @return void
*/
public function storeStatusFile(): void
{
$problems = $this->getProblemsSerializable();
$json = json_encode($problems);
file_put_contents($this->status_file, $json);
}
/**
* @param string|null $problems_dir
* @return bool
*/
public function check($problems_dir = null): bool
{
$problems_dir = $problems_dir ?: dirname(__DIR__);
$problems = [];
$problems_found = false;
$iterator = new \DirectoryIterator($problems_dir);
foreach ($iterator as $file) {
if (!$file->isFile() || $file->getExtension() !== 'php') {
continue;
}
$classname = 'Grav\\Plugin\\Problems\\' . $file->getBasename('.php');
if (class_exists($classname)) {
/** @var Problem $problem */
$problem = new $classname();
$problems[$problem->getId()] = $problem;
}
}
// Fire event to allow other plugins to add problems
Grav::instance()->fireEvent('onProblemsInitialized', new Event(['problems' => $problems]));
// Get the problems in order
usort($problems, function($a, $b) {
/** @var Problem $a */
/** @var Problem $b */
return $b->getOrder() - $a->getOrder();
});
// run the process methods in new order
foreach ($problems as $problem) {
$problem->process();
if ($problem->getStatus() === false && $problem->getLevel() === Problem::LEVEL_CRITICAL) {
$problems_found = true;
}
}
$this->problems = $problems;
return $problems_found;
}
/**
* @return array
*/
public function getProblems(): array
{
if (empty($this->problems)) {
$this->check();
}
$problems = $this->problems;
// Put the failed ones first
usort($problems, function($a, $b) {
/** @var Problem $a */
/** @var Problem $b */
return $a->getStatus() - $b->getStatus();
});
return $problems;
}
/**
* @return array
*/
public function getProblemsSerializable(): array
{
if (empty($this->problems)) {
$this->getProblems();
}
$problems = [];
foreach ($this->problems as $problem) {
$problems[] = $problem->toArray();
}
return $problems;
}
}