https://github.com/jaxon-php/jaxon-annotations
Annotations support for the Jaxon library.
https://github.com/jaxon-php/jaxon-annotations
Last synced: about 2 months ago
JSON representation
Annotations support for the Jaxon library.
- Host: GitHub
- URL: https://github.com/jaxon-php/jaxon-annotations
- Owner: jaxon-php
- License: bsd-3-clause
- Created: 2022-04-14T17:42:11.000Z (about 3 years ago)
- Default Branch: main
- Last Pushed: 2024-11-05T00:08:10.000Z (8 months ago)
- Last Synced: 2024-11-09T12:05:48.300Z (8 months ago)
- Language: PHP
- Size: 89.8 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
[](https://github.com/jaxon-php/jaxon-annotations/actions)
[](https://scrutinizer-ci.com/g/jaxon-php/jaxon-annotations/?branch=main)
[](https://styleci.io/repos/481695775)
[](https://codecov.io/gh/jaxon-php/jaxon-annotations)[](https://packagist.org/packages/jaxon-php/jaxon-annotations)
[](https://packagist.org/packages/jaxon-php/jaxon-annotations)
[](https://packagist.org/packages/jaxon-php/jaxon-annotations)
[](https://packagist.org/packages/jaxon-php/jaxon-annotations)Annotations for the Jaxon library
=================================This package provides annotation support for the Jaxon library.
The configuration options that are related to Jaxon classes can be set directly in the class files using annotations.Two different syntax are allowed for annotations: the default array-like syntax, and an alternative docblock-like syntax,
available since version `1.4`.Installation
------------Install this package with `composer`.
It requires `jaxon-php/jaxon-core` v4 or higher.```shell
composer require jaxon-php/jaxon-annotations
```Set the annotation config option to on.
```php
jaxon()->setOption('core.annotations.enabled', true);
```Usage
-----The following annotations are provided.
### @exclude
It prevents a method or a class from being exported to javascript.
It takes an optional boolean parameter.```php
/**
* @exclude(true)
*/
class JaxonExample
{
// This class will not be exported to javascript.
}
``````php
class JaxonExample
{
/**
* @exclude
*/
public function doNot()
{
// This method will not be exported to javascript.
}
}
```The PHP-DOC syntax can also be used.
```php
class JaxonExample
{
/**
* @exclude false
*/
public function do()
{
// This method will be exported to javascript.
}/**
* @exclude true
*/
public function doNot()
{
// This method will not be exported to javascript.
}
}
```### @upload
It adds file upload to an ajax request.
It takes the id of the HTML field as a mandatory option.
It applies only to methods.```php
class JaxonExample extends \Jaxon\App\CallableClass
{
/**
* @upload('field' => 'div-user-file')
*/
public function saveFile()
{
// Get the uploaded files.
$files = $this->upload()->files();
}
}
```The PHP-DOC syntax can also be used.
```php
class JaxonExample extends \Jaxon\App\CallableClass
{
/**
* @upload div-user-file
*/
public function saveFile()
{
// Get the uploaded files.
$files = $this->upload()->files();
}
}
```### @before
It defines a method of the class as a callback to be called before processing the request.
It takes the name of the method as a mandatory parameter, and an array as optional parameters to be passed to the callback.
It applies to methods and classes.```php
class JaxonExample
{
protected function funcBefore1()
{
// Do something
}protected function funcBefore2($param1, $param2)
{
// Do something with parameters
}/**
* @before('call' => 'funcBefore1')
* @before('call' => 'funcBefore2', 'with' => ['value1', 'value2'])
*/
public function action()
{
}
}
```The PHP-DOC syntax can also be used.
```php
class JaxonExample
{
protected function funcBefore1()
{
// Do something
}protected function funcBefore2($param1, $param2)
{
// Do something with parameters
}/**
* @before funcBefore1
* @before funcBefore2 ["value1", "value2"]
*/
public function action()
{
}
}
```### @after
It defines a method of the class as a callback to be called after processing the request.
It takes the name of the method as a mandatory parameter, and an array as optional parameters to be passed to the callback.
It applies to methods and classes.```php
class JaxonExample
{
protected function funcAfter1()
{
// Do something
}protected function funcAfter2($param)
{
// Do something with parameter
}/**
* @after('call' => 'funcAfter1')
* @after('call' => 'funcAfter2', 'with' => ['value'])
*/
public function action()
{
}
}
```The PHP-DOC syntax can also be used.
```php
class JaxonExample
{
protected function funcAfter1()
{
// Do something
}protected function funcAfter2($param)
{
// Do something with parameter
}/**
* @after funcAfter1
* @after funcAfter2 ["value"]
*/
public function action()
{
}
}
```### @callback
It defines a javascript object to be used as callback when processing the ajax request.
It was added in version 2.2.0.
```php
/**
* Default callback for all the requests to the class.
*
* @callback('name' => 'jaxon.ajax.callback.example')
*/
class JaxonExample
{
/**
* Specific callback for this method. It replaces the default class callback.
*
* @callback('name' => 'jaxon.ajax.callback.action')
*/
public function action()
{
}
}
```The PHP-DOC syntax can also be used.
```php
/**
* Default callback for all the requests to the class.
*
* @callback jaxon.ajax.callback.example
*/
class JaxonExample
{
/**
* Specific callback for this method. It replaces the default class callback.
*
* @callback jaxon.ajax.callback.action
*/
public function action()
{
}
}
```### @databag
It defines a data bag to be appended to ajax requests to a method.
It takes the name of the data bag as a mandatory parameter.
It applies to methods and classes.```php
class JaxonExample extends \Jaxon\App\CallableClass
{
/**
* @databag('name' => 'user')
*/
public function action()
{
// Update a value in the data bag.
$count = $this->bag('user')->get('count', 0);
$this->bag('user')->set('count', $count++);
}
}
```The PHP-DOC syntax can also be used.
```php
class JaxonExample extends \Jaxon\App\CallableClass
{
/**
* @databag user
*/
public function action()
{
// Update a value in the data bag.
$count = $this->bag('user')->get('count', 0);
$this->bag('user')->set('count', $count++);
}
}
```### @di
It defines an attribute that will be injected in a class.
When applied on methods and classes, it takes the name and the class of the attribute as parameters.
```php
class JaxonExample extends \Jaxon\App\CallableClass
{
/**
* @var \App\Services\Translator
*/
protected $translator;/**
* @di('attr' => 'translator', class => '\App\Services\Translator')
*/
public function translate(string $phrase)
{
// The $translator property is set from the DI container when this method is called.
$phrase = $this->translator->translate($phrase);
}
}
```The class parameter is optional, and can be omitted if it is already specified by a `@var` annotation.
```php
class JaxonExample extends \Jaxon\App\CallableClass
{
/**
* @var \App\Services\Translator
*/
protected $translator;/**
* @di('attr' => 'translator')
*/
public function translate(string $phrase)
{
// The $translator property is set from the DI container when this method is called.
$phrase = $this->translator->translate($phrase);
}
}
```When applied on attributes, it takes the class of the attribute as only parameter, which can be omitted if it is already specified by a `@var` annotation.
```php
class JaxonExample extends \Jaxon\App\CallableClass
{
/**
* @di(class => '\App\Services\Translator')
* @var \App\Services\Translator
*/
protected $translator;public function translate(string $phrase)
{
// The $translator property is set from the DI container when this method is called.
$phrase = $this->translator->translate($phrase);
}
}
``````php
class JaxonExample extends \Jaxon\App\CallableClass
{
/**
* @di
* @var \App\Services\Translator
*/
protected $translator;public function translate(string $phrase)
{
// The $translator property is set from the DI container when this method is called.
$phrase = $this->translator->translate($phrase);
}
}
```If the class name does not start with a `"\"`, then the corresponding fully qualified name (FQN) will be set using
either the `use` instructions or the `namespace` in its source file.```php
namespace App\Ajax;use App\Services\Translator;
class JaxonExample extends \Jaxon\App\CallableClass
{
/**
* @var Translator
*/
protected $translator;/**
* @var Formatter
*/
protected $formatter;/**
* @di('attr' => 'translator', class => 'Translator')
* @di('attr' => 'formatter', class => 'Formatter')
*/
public function translate(string $phrase)
{
// The Translator FQN is defined by the use instruction => App\Services\Translator.
// The Formatter FQN is defined by the current namespace => App\Ajax\Formatter.
$phrase = $this->formatter->format($this->translator->translate($phrase));
}
}
```The PHP-DOC syntax can also be used.
```php
namespace App\Ajax;use App\Services\Translator;
class JaxonExample extends \Jaxon\App\CallableClass
{
/**
* @var Translator
*/
protected $translator;/**
* @var Formatter
*/
protected $formatter;/**
* @di $translator Translator
* @di $formatter Formatter
*/
public function translate(string $phrase)
{
// The Translator FQN is defined by the use instruction => App\Services\Translator.
// The Formatter FQN is defined by the current namespace => App\Ajax\Formatter.
$phrase = $this->formatter->format($this->translator->translate($phrase));
}
}
``````php
namespace App\Ajax;use App\Services\Translator;
/**
* @di $translator Translator
* @di $formatter Formatter
*/
class JaxonExample extends \Jaxon\App\CallableClass
{
/**
* @var Translator
*/
protected $translator;/**
* @var Formatter
*/
protected $formatter;public function translate(string $phrase)
{
// The Translator FQN is defined by the use instruction => App\Services\Translator.
// The Formatter FQN is defined by the current namespace => App\Ajax\Formatter.
$phrase = $this->formatter->format($this->translator->translate($phrase));
}
}
``````php
namespace App\Ajax;use App\Services\Translator;
class JaxonExample extends \Jaxon\App\CallableClass
{
/**
* @di Translator
* @var Translator
*/
protected $translator;/**
* @di Formatter
* @var Formatter
*/
protected $formatter;public function translate(string $phrase)
{
// The Translator FQN is defined by the use instruction => App\Services\Translator.
// The Formatter FQN is defined by the current namespace => App\Ajax\Formatter.
$phrase = $this->formatter->format($this->translator->translate($phrase));
}
}
```