-
-
Save mindplay-dk/ebd5e4f7da51da3c4e56232adef41b46 to your computer and use it in GitHub Desktop.
annotations
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class Route | |
{ | |
public $pattern; | |
public $method; | |
public function __construct($pattern, $method) | |
{ | |
$this->pattern = $pattern; | |
$this->method = $method; | |
} | |
public static function get($pattern) { | |
return new self($pattern, "GET"); | |
} | |
} | |
class Filter | |
{ | |
public $name; | |
public function __construct($name) | |
{ | |
$this->name = $name; | |
} | |
} | |
class UserController | |
{ | |
<< Route::get('^/users/\w+$') >> | |
<< new Filter("membership") >> | |
public function showProfile() | |
{ | |
// ... | |
} | |
} | |
$method = new ReflectionMethod(UserController::class, "showProfile"); | |
var_dump($method->getAnnotations()); // => array(0 => {Route}, 1 => {Filter}) | |
var_dump($method->getAnnotations(Filter::class)); // => array(0 => {Filter}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// C# style annotation rules can be implemented entirely in userland | |
class Usage | |
{ | |
public $where; | |
public function __construct($where) { | |
$this->where = $where; | |
} | |
public static $classes = new Usage("classes"); | |
public static $methods = new Usage("methods"); | |
public static $properties = new Usage("properties"); | |
public static $multiple = new Usage("multiple"); | |
public static $inherit = new Usage("inherit"); | |
} | |
class Annotations | |
{ | |
public function getFromClass($class) { | |
// get class reflection | |
// collect annotations from class and recursively from parents | |
// for each annotation | |
// get Usage annotations from the annotation-class | |
// validate usage | |
// validate cardinality (single or multiple) | |
// apply inheritance logic (filter overridden annotations) | |
// return annotations | |
} | |
public function getFromMethod($class, $method) { | |
// ... | |
} | |
public function getFromProperty($class, $property) { | |
// ... | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I agree <<>> is ugly, I much prefer @ or @@
using @ to suppress errors is super bad in my opinion, I last used that in 2002 when I was young and stupid, I now handle that error instead of sweep it under the rug.
I'm honestly ok with busting BC as if someone is going to upgrade to a new version of PHP, they should expect legacy code to ge a little bruised up in the process and its more of knowing whats going to break before upgrading your system. especially if this makes the cut for PHP 8, I'd like to see a lot of things get disrupted to clean up the API finally.