Page MenuHomeDevCentral

D2598.id6554.diff
No OneTemporary

D2598.id6554.diff

diff --git a/.arcconfig b/.arcconfig
new file mode 100644
--- /dev/null
+++ b/.arcconfig
@@ -0,0 +1,5 @@
+{
+ "repository.callsign": "KHEALTH",
+ "phabricator.uri": "https://devcentral.nasqueron.org",
+ "unit.engine": "PhpunitTestEngine"
+}
diff --git a/.arclint b/.arclint
new file mode 100644
--- /dev/null
+++ b/.arclint
@@ -0,0 +1,32 @@
+{
+ "exclude": [
+ "(^vendor/)"
+ ],
+ "linters": {
+ "chmod": {
+ "type": "chmod"
+ },
+ "filename": {
+ "type": "filename"
+ },
+ "merge-conflict": {
+ "type": "merge-conflict"
+ },
+ "php": {
+ "type": "php",
+ "include": "(\\.php$)"
+ },
+ "phpcs": {
+ "type": "phpcs",
+ "bin": "vendor/bin/phpcs",
+ "phpcs.standard": "phpcs.xml",
+ "include": [
+ "(^src/.*\\.php$)",
+ "(^tests/.*\\.php$)"
+ ]
+ },
+ "spelling": {
+ "type": "spelling"
+ }
+ }
+}
diff --git a/.gitignore b/.gitignore
new file mode 100644
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,2 @@
+vendor/
+composer.lock
diff --git a/bin/health b/bin/health
new file mode 100755
--- /dev/null
+++ b/bin/health
@@ -0,0 +1,46 @@
+#!/usr/bin/env php
+<?php
+
+# -------------------------------------------------------------
+# Health command
+# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
+# Project: Keruald
+# Description: Generate site health report
+# License: BSD-2-Clause
+# -------------------------------------------------------------
+
+use Keruald\Health\CLI\HealthCommand;
+
+# -------------------------------------------------------------
+# Register classes autoloader
+#
+# Sources candidates for Composer autoloader:
+# :: Case 1 - the library itself
+# :: Case 2 - a project importing this library ad dependency
+#
+# Code solution from The League of Extraordinary Packages.
+# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
+
+function includeIfExists (string $file) : bool {
+ if (file_exists($file)) {
+ return include $file;
+ }
+}
+
+if ((!$loader = includeIfExists(__DIR__ . '/../vendor/autoload.php')) &&
+ (!$loader = includeIfExists(__DIR__ . '/../../../autoload.php'))) {
+ die(<<<EOF
+You must set up the project dependencies, run the following commands:
+$ wget http://getcomposer.org/composer.phar
+OR
+$ curl -sS https://getcomposer.org/installer | php
+$ php composer.phar install --dev
+EOF
+ );
+}
+
+# -------------------------------------------------------------
+# Run command
+# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
+
+HealthCommand::run($argc, $argv);
diff --git a/composer.json b/composer.json
new file mode 100644
--- /dev/null
+++ b/composer.json
@@ -0,0 +1,32 @@
+{
+ "name": "keruald/health",
+ "description": "Perform health checks on a website to help detect configuration issues.",
+ "type": "library",
+ "repositories": [{
+ "type": "path",
+ "url": "/home/dereckson/dev/nasqueron/keruald/commands"
+ }, {
+ "type": "path",
+ "url": "/home/dereckson/dev/nasqueron/keruald/report"
+ }],
+ "require": {
+ "keruald/commands": "dev-master ",
+ "keruald/omnitools": "dev-main",
+ "keruald/report": "dev-main"
+ },
+ "require-dev": {
+ "phpunit/phpunit": "^9.5",
+ "phan/phan": "^5.3",
+ "nasqueron/codestyle": "^0.0.1",
+ "squizlabs/php_codesniffer": "^3.6"
+ },
+ "license": "BSD-2-Clause",
+ "autoload": {
+ "psr-4": {
+ "Keruald\\Health\\": "src/"
+ }
+ },
+ "bin": [
+ "bin/health"
+ ]
+}
diff --git a/phpcs.xml b/phpcs.xml
new file mode 100644
--- /dev/null
+++ b/phpcs.xml
@@ -0,0 +1,7 @@
+<?xml version="1.0"?>
+<ruleset name="Nasqueron">
+ <rule ref="vendor/nasqueron/codestyle/CodeSniffer/ruleset.xml" />
+
+ <file>src</file>
+ <file>tests</file>
+</ruleset>
diff --git a/src/CLI/HealthCommand.php b/src/CLI/HealthCommand.php
new file mode 100644
--- /dev/null
+++ b/src/CLI/HealthCommand.php
@@ -0,0 +1,45 @@
+<?php
+
+namespace Keruald\Health\CLI;
+
+use Keruald\Commands\Command;
+use Keruald\Commands\ExitCode;
+
+class HealthCommand extends Command {
+
+ public function main () : int {
+ if ($this->argc < 2) {
+ $this->usage();
+ return ExitCode::FAILURE;
+ }
+
+ $class = $this->argv[1];
+
+ if (!class_exists($class)) {
+ $this->display->error("Class not found: " . $class);
+ return ExitCode::ILLEGAL_OPTION;
+ }
+
+ return $this->printReport($class);;
+ }
+
+ public function usage () : void {
+ $usage = sprintf(
+ "Usage: %s <health report class>",
+ $this->getCommandName()
+ );
+
+ $this->display->error($usage);
+ }
+
+ private function printReport (string $class) : int {
+ $healthReport = new $class();
+ $report = $healthReport->compile();
+
+ $report->properties["Quux"] = 42;
+ echo $report->render("markdown");
+
+ return ExitCode::fromBoolean($healthReport->isHealthy);
+ }
+
+}
diff --git a/src/Check.php b/src/Check.php
new file mode 100644
--- /dev/null
+++ b/src/Check.php
@@ -0,0 +1,33 @@
+<?php
+
+namespace Keruald\Health;
+
+use Keruald\Reporting\ReportSection;
+
+abstract class Check {
+
+ ///
+ /// Properties
+ ///
+
+ public ReportSection $report;
+
+ ///
+ /// Abstract part
+ ///
+
+ public abstract function check () : bool;
+
+ ///
+ /// Common methods for all checks
+ ///
+
+ public function __construct () {
+ $this->report = new ReportSection(static::class, []);
+ }
+
+ protected function report (string $title, string $text) {
+ $this->report->push($title, $text);
+ }
+
+}
diff --git a/src/HealthReport.php b/src/HealthReport.php
new file mode 100644
--- /dev/null
+++ b/src/HealthReport.php
@@ -0,0 +1,87 @@
+<?php
+
+namespace Keruald\Health;
+
+use Keruald\OmniTools\OS\CurrentProcess;
+use Keruald\Reporting\Report;
+
+abstract class HealthReport {
+
+ ///
+ /// Properties
+ ///
+
+ /**
+ * @var bool Indicates if the site is healthy, ie each check was green.
+ */
+ public bool $isHealthy = true;
+
+ ///
+ /// Methods to implement
+ ///
+
+ /**
+ * An array with the fully qualified name of the check classes to run.
+ *
+ * Each class should extend the Check class.
+ *
+ * @return string[]
+ */
+ public abstract function getChecks () : array;
+
+ // You can also override getTitle() and getExtraProperties()
+ // to customize the report.
+
+ ///
+ /// Prepare report
+ ///
+
+ public function compile () : Report {
+ $report = new Report($this->getTitle());
+
+ foreach ($this->getChecks() as $class) {
+ $check = new $class();
+
+ $this->isHealthy &= $check->check();
+ $report->pushIfNotEmpty($check->report);
+ }
+
+ $report->properties = $this->getExtraProperties()
+ + $this->getProperties();
+
+ return $report;
+ }
+
+ /**
+ * @return string The title of the report
+ */
+ public function getTitle () : string {
+ return "Site health";
+ }
+
+ /**
+ * @return array<string, mixed>
+ */
+ public function getProperties() : array {
+ return [
+ 'time' => time(),
+ 'hostname' => gethostname(),
+ 'user' => CurrentProcess::getUsername(),
+ 'php_version' => phpversion(),
+ 'healthy' => $this->isHealthy,
+ ];
+ }
+
+ /**
+ * A key/value hashmap of the metadata to add to the report.
+ *
+ * If the key is already defined in the default properties,
+ * the value set in getExtraProperties will be used instead.
+ *
+ * @return array<string, mixed>
+ */
+ public function getExtraProperties() : array {
+ return [];
+ }
+
+}

File Metadata

Mime Type
text/plain
Expires
Sun, Dec 22, 12:31 (1 h, 27 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
2310638
Default Alt Text
D2598.id6554.diff (7 KB)

Event Timeline