Skip to content

Commit

Permalink
Legacy support
Browse files Browse the repository at this point in the history
  • Loading branch information
Jakiboy committed Jul 27, 2024
1 parent a3e323b commit 80dbaae
Show file tree
Hide file tree
Showing 15 changed files with 930 additions and 410 deletions.
305 changes: 175 additions & 130 deletions src/VanillePluginConfig.php

Large diffs are not rendered by default.

115 changes: 38 additions & 77 deletions src/VanillePluginValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,105 +14,66 @@

namespace VanillePlugin;

use VanillePlugin\exc\ConfigException;
use VanillePlugin\inc\{
Validator, TypeCheck, File, GlobalConst
TypeCheck, Stringify, Arrayify
};
use VanillePlugin\exc\{
NamepsaceException, ConfigException
};
use JsonSchema\Validator as JsonValidator;
use JsonSchema\Validator;

final class VanillePluginValidator extends Validator
/**
* Configuration validator.
*/
final class VanillePluginValidator
{
/**
* Validate config data using schema.
*
* @access public
* @var mixed $plugin
* @var mixed $data
* @var string $schema
* @return void
* @throws NamepsaceException
* @throws ConfigException
*/
public static function checkNamespace($plugin)
public static function validate($data, string $schema)
{
if ( !self::isValidNamespace($plugin) ) {
$namespace = $plugin->getNameSpace();
if ( TypeCheck::isObject($namespace) ) {
$namespace = '{Object}';
} elseif ( TypeCheck::isArray($namespace) ) {
$namespace = '[Array]';
} else {
$namespace = (string)$namespace;
if ( !self::isValid($data, $schema, $error) ) {

if ( TypeCheck::isString($error) ) {
throw new ConfigException(
ConfigException::invalidConfig($error, $schema)
);
}
throw new NamepsaceException(
NamepsaceException::invalidPluginNamepsace($namespace)
);
}
}

/**
* @access public
* @var object $global,
* @var string $file
* @return void
* @throws ConfigException
*/
public static function checkConfig($global, $file = null)
{
$error = self::isValidConfig($global);
if ( TypeCheck::isString($error) ) {
throw new ConfigException(
ConfigException::invalidConfig($error,$file)
);
} elseif ( $error === false ) {
throw new ConfigException(
ConfigException::invalidConfigFormat($file)
ConfigException::invalidConfigFormat($schema)
);
}
}

/**
* Validate plugin namespace.
*
* Check whether config has valid schema.
*
* @access private
* @var object $plugin
* @var mixed $data
* @var string $schema
* @var string $error
* @return bool
*/
private static function isValidNamespace($plugin)
private static function isValid($data, string $schema, &$error = null) : bool
{
if ( TypeCheck::isObject($plugin) ) {
if ( TypeCheck::isString($plugin->getNameSpace()) ) {
if ( !empty($namespace = $plugin->getNameSpace()) ) {
return File::exists(
GlobalConst::pluginDir("/{$namespace}/{$namespace}.php")
);
}
}
}
return false;
}
$validator = new Validator();
$path = Stringify::formatPath(__DIR__ . "/bin/{$schema}.schema.json");
$validator->validate($data, (object)[
'$ref' => "file://{$path}"
]);

/**
* Validate plugin configuration.
*
* @access private
* @var object $config
* @return mixed
*/
private static function isValidConfig($config)
{
if ( TypeCheck::isObject($config) ) {
$validator = new JsonValidator;
$validator->validate($config, (object)[
'$ref' => 'file://' . dirname(__FILE__).'/config.schema.json'
]);
if ( $validator->isValid() ) {
return true;
} else {
$errors = [];
foreach ($validator->getErrors() as $error) {
$errors[] = sprintf("[%s] %s",$error['property'],$error['message']);
}
return implode("\n", $errors);
}
if ( !$validator->isValid() ) {
$errors = $validator->getErrors();
$error = Arrayify::shift($errors);
$error = "{$error['message']} [{$error['property']}]";
return false;
}
return false;

return true;
}
}
23 changes: 23 additions & 0 deletions src/bin/ajax.schema.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"$id": "plugin.ajax.schema.json",
"type": "object",
"properties": {
"admin": {
"type": "array",
"items": {
"type": "string"
}
},
"front": {
"type": "array",
"items": {
"type": "string"
}
}
},
"required": [
"admin",
"front"
]
}
57 changes: 57 additions & 0 deletions src/bin/cron.schema.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"$id": "plugin.cron.schema.json",
"type": "object",
"properties": {
"schedules": {
"type": "array",
"items": [
{
"type": "object",
"properties": {
"name": {
"type": "string"
},
"display": {
"type": "string"
},
"interval": {
"type": "integer"
}
}
}
],
"required": [
"name",
"interval"
]
},
"events": {
"type": "array",
"items": [
{
"type": "object",
"properties": {
"name": {
"type": "string"
},
"schedule": {
"type": "string"
},
"callback": {
"type": "string"
}
}
}
],
"required": [
"name",
"schedule"
]
}
},
"required": [
"schedules",
"events"
]
}
105 changes: 105 additions & 0 deletions src/bin/global.schema.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"$id": "plugin.global.schema.json",
"type": "object",
"properties": {
"name": {
"type": "string"
},
"version": {
"type": "string"
},
"author": {
"type": "string"
},
"description": {
"type": "string"
},
"path": {
"type": "object",
"properties": {
"asset": {
"type": "string"
},
"view": {
"type": "string"
},
"migrate": {
"type": "string"
},
"logs": {
"type": "string"
},
"temp": {
"type": "string"
},
"cache": {
"type": "string"
}
},
"required": [
"asset",
"view",
"migrate",
"logs",
"temp",
"cache"
]
},
"options": {
"type": "object",
"properties": {
"ttl": {
"type": "number"
},
"timeout": {
"type": "number"
},
"secret": {
"type": "string"
},
"footprint": {
"type": "boolean"
},
"debug": {
"type": "boolean"
},
"multiling": {
"type": "boolean"
},
"multisite": {
"type": "boolean"
},
"view": {
"type": "object",
"properties": {
"extension": {
"type": "string"
}
},
"required": [
"extension"
]
}
},
"required": [
"ttl",
"timeout",
"secret",
"footprint",
"debug",
"multiling",
"multisite",
"view"
]
}
},
"required": [
"name",
"version",
"author",
"description",
"path",
"options"
]
}
Loading

0 comments on commit 80dbaae

Please sign in to comment.