PHP code example of xiaozhuai / php_simple_annotation
1. Go to this page and download the library: Download xiaozhuai/php_simple_annotation library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
xiaozhuai / php_simple_annotation example snippets
bash
$ composer
php
huai\AnnotationParser;
final class RouteParams
{
public $group;
public $method;
public $pattern;
public $isGroup = false;
const ALL_METHODS = ['GET', 'POST', 'PUT', 'PATCH', 'DELETE', 'OPTIONS'];
public function __construct($args)
{
$this->pattern = $args['pattern'] ?? '';
$this->group = $args['group'] ?? null;
$this->isGroup = !empty($this->group);
$this->method = $args['method'] ?? null;
if ($this->method !== null) {
if ($this->method === 'ANY') {
$this->method = static::ALL_METHODS;
}
if (!is_array($this->method)) {
$this->method = [$this->method];
}
$this->method = array_map('strtoupper', $this->method);
foreach ($this->method as $m) {
if (!$this->isGroup && !in_array($m, static::ALL_METHODS, true)) {
throw new RuntimeException('Invalid method ' . $m);
}
}
}
}
}
class AuthParams
{
public $auth;
public $permission;
public $desc;
}
/**
* @Route {
* group : /order
* }
*/
class TestAnnotationOrderController
{
/**
* @Route {
* method : post
* pattern : /create
* }
* @Auth {
* auth : true
* }
*/
public function create()
{
}
/**
* @Route {
* method : GET
* pattern : /delete
* }
* @Auth {
* auth : true
* permission : delete_order
* desc : delete order
* }
*/
public function delete()
{
}
}
// return array if no classmap provided
$parser = new AnnotationParser([
'Route' => RouteParams::class,
'Auth' => AuthParams::class,
]);
$reflectionClass = new ReflectionClass('TestAnnotationOrderController');
print_r($parser->parse($reflectionClass->getDocComment()));
$methods = $reflectionClass->getMethods(ReflectionMethod::IS_PUBLIC);
foreach ($methods as $method) {
print_r($parser->parse($method->getDocComment()));
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.