Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@
"bin": ["bin/wave"],

"require": {
"php": ">=7.4",
"twig/twig": "1.*",
"monolog/monolog": "1.*",
"php": ">=8.1",
"twig/twig": "3.*",
"monolog/monolog": "2.*",
"dragonmantank/cron-expression": "^3.3"
},

Expand Down
34 changes: 22 additions & 12 deletions src/Wave/Annotation.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
use Wave\Annotation\Validate;
use Wave\DB\Model;

class Annotation {
class Annotation
{

const FOR_CLASS = 'class';

Expand All @@ -33,19 +34,21 @@ class Annotation {
'schedule' => Schedule::class,
);

public static function factory($key, $value, $from_class = null){
public static function factory($key, $value, $from_class = null)
{

$class = __CLASS__;
if(isset(self::$handlers[$key])){
if (isset(self::$handlers[$key])) {
$class = self::$handlers[$key];
}

return new $class($key, $value, $from_class);
}

public static function parse($block, $originating_class){
public static function parse($block, $originating_class)
{

if(empty($block)) return array();
if (empty($block)) return array();

$block = self::sanitizeDocBlock($block);

Expand All @@ -68,34 +71,41 @@ public static function parse($block, $originating_class){
* @param $docblock
* @return mixed
*/
protected static function sanitizeDocBlock($docblock){
protected static function sanitizeDocBlock($docblock)
{
return preg_replace('/^([\t\f ]*\*[\t\f ]+)/m', '', $docblock);
}

public function __construct($key, $value, $from_class = null) {
public function __construct($key, $value, $from_class = null)
{
$this->key = $key;
$this->value = $value;
$this->from_class = $from_class;
}

public function apply(Router\Action &$action){}
public function apply(Router\Action &$action)
{
}

/**
* @return mixed
*/
public function getKey() {
public function getKey()
{
return $this->key;
}

/**
* @return mixed
*/
public function getValue() {
public function getValue()
{
return $this->value;
}

protected function validOnSubclassesOf($annotatedClass, $baseClass) {
if( $annotatedClass != $baseClass && !is_subclass_of($annotatedClass, $baseClass) )
protected function validOnSubclassesOf($annotatedClass, $baseClass)
{
if ($annotatedClass != $baseClass && !is_subclass_of($annotatedClass, $baseClass))
throw new InvalidAnnotationException(get_class($this) . " is only valid on objects of type {$baseClass}.");

}
Expand Down
77 changes: 46 additions & 31 deletions src/Wave/Annotation/ArrayArguments.php
Original file line number Diff line number Diff line change
@@ -1,44 +1,50 @@
<?php



namespace Wave\Annotation;

use Wave\Annotation;

abstract class ArrayArguments extends Annotation {
abstract class ArrayArguments extends Annotation
{

protected $parameters;
protected $errors = array();

public function __construct($key, $value, $from_class = null) {
public function __construct($key, $value, $from_class = null)
{
parent::__construct($key, $value, $from_class);

$this->parameters = $this->parseParameters();

$this->validate($from_class);
if(!empty($this->errors)){
throw new InvalidAnnotationException('Annotation format error, '.implode(', ', $this->errors), 0);
if (!empty($this->errors)) {
throw new InvalidAnnotationException('Annotation format error, ' . implode(', ', $this->errors), 0);
}

$this->build();
}

protected function validate($class){}
protected function build(){}
protected function validate($class)
{
}

protected function parseParameters(){
protected function build()
{
}

protected function parseParameters()
{

$arguments = array();
foreach(explode(',', $this->value) as $argument){
foreach (explode(',', $this->value) as $argument) {
// attempt to explode the argument into a key:value
list($k, $value) = explode(':', $argument) + array(null, null);
// no value means it was just a plain argument, so just clean it and insert as normal
$k = trim($k, ' \'"');
if($value === null){
if ($value === null) {
$arguments[] = $k;
}
else {
} else {
$arguments[strtolower($k)] = trim($value, ' \'"');
}
}
Expand All @@ -47,61 +53,70 @@ protected function parseParameters(){

}

protected function acceptedKeys($keys) {
foreach($this->parameters as $key => $value) {
protected function acceptedKeys($keys)
{
foreach ($this->parameters as $key => $value) {
if (is_string($key) && !in_array($key, $keys)) {
$this->errors[] = "Invalid parameter: \"$key\".";
}
}
}

protected function requiredKeys($keys) {
foreach($keys as $key) {
if(!array_key_exists($key, $this->parameters)) {
protected function requiredKeys($keys)
{
foreach ($keys as $key) {
if (!array_key_exists($key, $this->parameters)) {
$this->errors[] = get_class($this) . " requires a '$key' parameter.";
}
}
}

protected function acceptedKeylessValues($values) {
foreach($this->parameters as $key => $value) {
if(!is_string($key) && !in_array($value, $values)) {
protected function acceptedKeylessValues($values)
{
foreach ($this->parameters as $key => $value) {
if (!is_string($key) && !in_array($value, $values)) {
$this->errors[] = "Unknown parameter: \"$value\".";
}
}
}

protected function acceptedIndexedValues($index, $values, $optional = true) {
if($optional && !isset($this->parameters[$index])) return;
protected function acceptedIndexedValues($index, $values, $optional = true)
{
if ($optional && !isset($this->parameters[$index])) return;

if(!in_array($this->parameters[$index],$values)) {
if (!in_array($this->parameters[$index], $values)) {
$this->errors[] = "Parameter $index is set to \"" . $this->parameters[$index] . "\". Valid values: " . implode(', ', $values) . '.';
}
}

protected function acceptsNoKeylessValues() {
protected function acceptsNoKeylessValues()
{
$this->acceptedKeylessValues(array());
}

protected function acceptsNoKeyedValues() {
protected function acceptsNoKeyedValues()
{
$this->acceptedKeys(array());
}


protected function minimumParameterCount($count) {
if( ! (count($this->parameters) >= $count) ) {
protected function minimumParameterCount($count)
{
if (!(count($this->parameters) >= $count)) {
$this->errors[] = get_class($this) . " takes at least $count parameters.";
}
}

protected function maximumParameterCount($count) {
if( ! (count($this->parameters) <= $count) ) {
protected function maximumParameterCount($count)
{
if (!(count($this->parameters) <= $count)) {
$this->errors[] = get_class($this) . " takes at most $count parameters.";
}
}

protected function exactParameterCount($count) {
if ( count($this->parameters) != $count ) {
protected function exactParameterCount($count)
{
if (count($this->parameters) != $count) {
$this->errors[] = get_class($this) . " requires exactly $count parameters.";
}
}
Expand Down
11 changes: 7 additions & 4 deletions src/Wave/Annotation/BaseRoute.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,18 @@
use Wave\Annotation;
use Wave\Router\Action;

class BaseRoute extends ArrayArguments {

protected function validate($class) {
class BaseRoute extends ArrayArguments
{

protected function validate($class)
{
$this->minimumParameterCount(1);
$this->maximumParameterCount(1);
$this->validOnSubclassesOf($class, Annotation::CLASS_CONTROLLER);
}

public function apply(Action &$action) {
public function apply(Action &$action)
{
$action->addBaseRoute($this->parameters[0]);
}

Expand Down
29 changes: 16 additions & 13 deletions src/Wave/Annotation/BaseURL.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,21 @@
use Wave\Annotation;
use Wave\Router\Action;

class BaseURL extends ArrayArguments {

const DEFAULT_KEYWORD = 'default';

public function validate($class) {
$this->minimumParameterCount(1);
$this->maximumParameterCount(1);
$this->validOnSubclassesOf($class, Annotation::CLASS_CONTROLLER);
}

public function apply(Action &$action) {
$action->setProfile($this->parameters[0]);
}
class BaseURL extends ArrayArguments
{

const DEFAULT_KEYWORD = 'default';

public function validate($class)
{
$this->minimumParameterCount(1);
$this->maximumParameterCount(1);
$this->validOnSubclassesOf($class, Annotation::CLASS_CONTROLLER);
}

public function apply(Action &$action)
{
$action->setProfile($this->parameters[0]);
}

}
4 changes: 3 additions & 1 deletion src/Wave/Annotation/InvalidAnnotationException.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,6 @@

use Wave\Exception;

class InvalidAnnotationException extends Exception {}
class InvalidAnnotationException extends Exception
{
}
46 changes: 25 additions & 21 deletions src/Wave/Annotation/RequiresLevel.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,30 @@
use Wave\Annotation;
use Wave\Router\Action;

class RequiresLevel extends ArrayArguments {

const DEFAULT_KEYWORD = 'default';

public function validate($class) {
$this->minimumParameterCount(1);
$this->validOnSubclassesOf($class, Annotation::CLASS_CONTROLLER);
}

public function build(){
$this->inherit = true;
if(isset($this->parameters['inherit'])){
$this->inherit = $this->parameters['inherit'] == 'true';
unset($this->parameters['inherit']);
}
$this->methods = $this->parameters;
}

public function apply(Action &$action){
return $action->setRequiresLevel($this->methods, $this->inherit);
}
class RequiresLevel extends ArrayArguments
{

const DEFAULT_KEYWORD = 'default';

public function validate($class)
{
$this->minimumParameterCount(1);
$this->validOnSubclassesOf($class, Annotation::CLASS_CONTROLLER);
}

public function build()
{
$this->inherit = true;
if (isset($this->parameters['inherit'])) {
$this->inherit = $this->parameters['inherit'] == 'true';
unset($this->parameters['inherit']);
}
$this->methods = $this->parameters;
}

public function apply(Action &$action)
{
return $action->setRequiresLevel($this->methods, $this->inherit);
}

}
Loading