https://github.com/jphp-group/jphp-annotations-ext
https://github.com/jphp-group/jphp-annotations-ext
Last synced: about 1 year ago
JSON representation
- Host: GitHub
- URL: https://github.com/jphp-group/jphp-annotations-ext
- Owner: jphp-group
- License: apache-2.0
- Created: 2018-12-02T12:20:33.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2019-05-26T11:50:14.000Z (about 7 years ago)
- Last Synced: 2025-03-24T20:38:36.423Z (about 1 year ago)
- Language: PHP
- Size: 18.6 KB
- Stars: 4
- Watchers: 4
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.MD
- License: LICENSE
Awesome Lists containing this project
README
## Annotations for JPHP
### Описание
Расширение позволяет использовать кастомные аннотации для классов, свойств и методов(как в Java)
### Установка
```
jppm add jphp-annotations-ext
```
### Примеры
#### Класс аннотации:
```php
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;
}
}
```
#### Определение аннотации для метода:
```php
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 :)";
}
}
```
#### Использование аннотаций:
```php
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](https://vk.com/venity)
[DevelNext - extensions & manuals.](https://vk.com/dn_extension)