Skip to content

jphp-group/jphp-annotations-ext

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Annotations for JPHP

Описание

Расширение позволяет использовать кастомные аннотации для классов, свойств и методов(как в Java)

Установка

jppm add jphp-annotations-ext

Примеры

Класс аннотации:

namespace test\annotations;


class Route{
    public $path;
    private $methods = ['GET'];


    public function getMethods(): array{
        return $this->methods;
    }
    public function setMethods(array $methods){
        $this->methods = $methods;
    }
}

Определение аннотации для метода:

namespace test\controllers;

use test\annotations\Route;

class SampleController{
    private $path;


    /**
     * @Route(path='/')
     */
    public function index(): void{
        echo "Hello, world!";
    }
    /**
     * @Route(path='/about')
     */
    public function about(): void{
        echo "http://vk.com/dn_extension";
    }
    /**
     * @Route(path='/login', methods=['GET', 'POST'])
     */
    public function login(): void{
        echo "he-he, not implemented :)";
    }
}

Использование аннотаций:

use test\controllers\SampleController;
use test\annotations\Route;
use annotations\AnnotatedReflectionClass;

$class = new AnnotatedReflectionClass(SampleController::class);

foreach($class->getMethods() as $method){
    /** @var Route $route */
    $route = $class->getMethodAnnotationByClass($method->getName(), Route::class);
    if($route){
        echo "{$route->path} - ".str::join($route->getMethods(), ', ')."\n";
    }
}

Результат:

/ - GET
/about - GET
/login - GET, POST

Дополнительно

Venity

DevelNext - extensions & manuals.

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages