- First draft of current WIP
This commit is contained in:
commit
b73d046e0a
47 files changed
+2797
No files matched your search
@@ -0,0 +1,5 @@
|
||||
<?php
|
||||
|
||||
namespace Ulmus\Annotation;
|
||||
|
||||
interface Annotation {}
|
||||
@@ -0,0 +1,103 @@
|
||||
<?php
|
||||
|
||||
namespace Ulmus\Annotation;
|
||||
|
||||
use Reflector, ReflectionClass, ReflectionProperty, ReflectionMethod;
|
||||
|
||||
/**
|
||||
* This class exists while waiting for the official RFC [ https://wiki.php.net/rfc/annotations_v2 ]
|
||||
*/
|
||||
class AnnotationReader
|
||||
{
|
||||
const PHP_TYPES = [ "string", "int", "float", "object", "double", "closure", ];
|
||||
|
||||
public string $class;
|
||||
|
||||
public function __construct($class) {
|
||||
$this->class = $class;
|
||||
}
|
||||
|
||||
public static function fromClass($class) : self
|
||||
{
|
||||
return new static($class);
|
||||
}
|
||||
|
||||
public function getProperty(ReflectionProperty $property)
|
||||
{
|
||||
return $this->parseDocComment($property);
|
||||
}
|
||||
|
||||
public function getClass(ReflectionClass $class)
|
||||
{
|
||||
return $this->parseDocComment($class);
|
||||
}
|
||||
|
||||
public function getMethod(ReflectionMethod $method)
|
||||
{
|
||||
return $this->parseDocComment($method);
|
||||
}
|
||||
|
||||
protected function parseDocComment(Reflector $reflect)
|
||||
{
|
||||
$namespace = $this->getObjectNamespace($reflect);
|
||||
$tags = [];
|
||||
|
||||
foreach(preg_split("/\r\n|\n|\r/", $reflect->getDocComment()) as $line) {
|
||||
$line = ltrim($line, "* \t\/");
|
||||
$line = rtrim($line, "\t ");
|
||||
|
||||
if ( substr($line, 0, 1) === '@' ) {
|
||||
$line = ltrim($line, '@');
|
||||
|
||||
$open = strpos($line, "(");
|
||||
$close = strrpos($line, ")");
|
||||
|
||||
if ( ! in_array(false, [ $open, $close ], true) && ( ++$open !== $close ) ) {
|
||||
$arguments = substr($line, $open, $close - $open);
|
||||
|
||||
try {
|
||||
$tags[] = [
|
||||
'tag' => substr($line, 0, $open - 1),
|
||||
'arguments' => eval("namespace $namespace; return [ $arguments ];"),
|
||||
];
|
||||
}
|
||||
catch(\Throwable $error) {
|
||||
throw new \InvalidArgumentException("An error occured while parsing annotation from '" . $this->getObjectName($reflect) . "' : @$line -- " . $error->getMessage());
|
||||
}
|
||||
}
|
||||
else {
|
||||
$tags[] = [
|
||||
'tag' => $line,
|
||||
'arguments' => [],
|
||||
];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $tags;
|
||||
}
|
||||
|
||||
protected function getObjectName(Reflector $reflect) : string
|
||||
{
|
||||
switch(true) {
|
||||
case $reflect instanceof ReflectionMethod :
|
||||
case $reflect instanceof ReflectionProperty :
|
||||
return $reflect->class . "::" . $reflect->name;
|
||||
|
||||
case $reflect instanceof ReflectionClass :
|
||||
return $reflect->name;
|
||||
}
|
||||
}
|
||||
|
||||
protected function getObjectNamespace(Reflector $reflect) : string
|
||||
{
|
||||
switch(true) {
|
||||
case $reflect instanceof ReflectionMethod :
|
||||
case $reflect instanceof ReflectionProperty :
|
||||
return $reflect->getDeclaringClass()->getNamespaceName();
|
||||
|
||||
case $reflect instanceof ReflectionClass :
|
||||
return $reflect->getNamespaceName();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
<?php
|
||||
|
||||
namespace Ulmus\Annotation\Classes;
|
||||
|
||||
class Collation implements \Ulmus\Annotation\Annotation {
|
||||
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
<?php
|
||||
|
||||
namespace Ulmus\Annotation\Classes;
|
||||
|
||||
class Function implements \Ulmus\Annotation\Annotation {
|
||||
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
<?php
|
||||
|
||||
namespace Ulmus\Annotation\Classes;
|
||||
|
||||
class Table implements \Ulmus\Annotation\Annotation {
|
||||
|
||||
public string $name;
|
||||
|
||||
public function __construct($name = null)
|
||||
{
|
||||
if ( $name !== null ) {
|
||||
$this->name = $name;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
<?php
|
||||
|
||||
namespace Ulmus\Annotation\Property;
|
||||
|
||||
class Field implements \Ulmus\Annotation\Annotation {
|
||||
|
||||
public string $type;
|
||||
|
||||
public string $name;
|
||||
|
||||
public int $length;
|
||||
|
||||
public array $attributes = [];
|
||||
|
||||
public bool $nullable = false;
|
||||
|
||||
public function __construct(string $type = null, int $length = null)
|
||||
{
|
||||
switch(true) {
|
||||
case $type !== null:
|
||||
$this->type = $type;
|
||||
break;
|
||||
|
||||
case $length !== null:
|
||||
$this->length = $length;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
<?php
|
||||
|
||||
namespace Ulmus\Annotation\Property\Field;
|
||||
|
||||
class CreatedAt extends \Ulmus\Annotation\Property\Field {
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->nullable = false;
|
||||
$this->type = "timestamp";
|
||||
$this->attributes['default'] = "CURRENT_TIMESTAMP";
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
<?php
|
||||
|
||||
namespace Ulmus\Annotation\Property\Field;
|
||||
|
||||
/**
|
||||
* Since we need consistancy between the declaration of our ID and FK fields, it
|
||||
* was decided to extend the Id class instead of Field for this case.
|
||||
*/
|
||||
class ForeignKey extends Id {
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
parent::__construct();
|
||||
|
||||
unset($this->nullable);
|
||||
$this->attributes['primary_key'] = false;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
<?php
|
||||
|
||||
namespace Ulmus\Annotation\Property\Field;
|
||||
|
||||
class Id extends \Ulmus\Annotation\Property\Field {
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->nullable = false;
|
||||
$this->type = "int";
|
||||
$this->attributes['unsigned'] = true;
|
||||
$this->attributes['primary_key'] = true;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
<?php
|
||||
|
||||
namespace Ulmus\Annotation\Property\Field;
|
||||
|
||||
class UpdatedAt extends \Ulmus\Annotation\Property\Field {
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->nullable = true;
|
||||
$this->type = "timestamp";
|
||||
$this->attributes['update'] = "CURRENT_TIMESTAMP";
|
||||
$this->attributes['default'] = null;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
<?php
|
||||
|
||||
namespace Ulmus\Annotation\Property;
|
||||
|
||||
class GroupBy implements \Ulmus\Annotation\Annotation {
|
||||
|
||||
public array $fields = [];
|
||||
|
||||
public function __construct(...$field)
|
||||
{
|
||||
if ( $field ) {
|
||||
$this->fields = $field;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
<?php
|
||||
|
||||
namespace Ulmus\Annotation\Property;
|
||||
|
||||
class OrderBy implements \Ulmus\Annotation\Annotation {
|
||||
|
||||
public string $field;
|
||||
|
||||
public string $order = "ASC";
|
||||
|
||||
public function __construct(string $field = null, string $order = null)
|
||||
{
|
||||
switch(true) {
|
||||
case $field !== null:
|
||||
$this->field = $field;
|
||||
break;
|
||||
|
||||
case $order !== null:
|
||||
$this->order = $order;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
<?php
|
||||
|
||||
namespace Ulmus\Annotation\Property;
|
||||
|
||||
class Relation implements \Ulmus\Annotation\Annotation {
|
||||
|
||||
public string $type;
|
||||
|
||||
public string $key;
|
||||
|
||||
public string $foreignKey;
|
||||
|
||||
public array $foreignKeys;
|
||||
|
||||
public string $bridge;
|
||||
|
||||
public string $bridgeKey;
|
||||
|
||||
public string $bridgeForeignKey;
|
||||
|
||||
public function __construct(string $type = null)
|
||||
{
|
||||
switch(true) {
|
||||
case $type !== null:
|
||||
$this->type = $type;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
<?php
|
||||
|
||||
namespace Ulmus\Annotation\Property;
|
||||
|
||||
class Where implements \Ulmus\Annotation\Annotation {
|
||||
|
||||
public array $comparisons = [];
|
||||
|
||||
public function __construct(...$comparisons)
|
||||
{
|
||||
if ( $comparisons ) {
|
||||
$this->comparisons = $comparisons;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user