Skip to content

Commit

Permalink
Move item and bootstrap behavior examples to separate files
Browse files Browse the repository at this point in the history
  • Loading branch information
Horat1us committed Mar 6, 2019
1 parent 0b2e659 commit b263bbe
Show file tree
Hide file tree
Showing 3 changed files with 179 additions and 179 deletions.
184 changes: 5 additions & 179 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@ $staticBehavior->attach();
$staticBehavior->detach();
```

[Detailed example](./examples/static-behavior.php)

### Bootstrap

To bootstrap your application you should use [Bootstrap](./src/StaticBehavior/Bootstrap.php).
Expand Down Expand Up @@ -108,67 +110,7 @@ Main purpose of items - lazy dependency injection for event handlers.
You can define dependencies in constructor or use yii2-way configuration (prefered).

Item that handles `yii\db\ActiveRecord` events:
```php
<?php

namespace Example;

use Horat1us\Yii\StaticBehavior;
use yii\base;
use yii\web;
use yii\db;
use yii\di;

/**
* Class Item
* @package Example
*/
class Item extends StaticBehavior\Item
{
/**
* Configurable dependency
* @var string|array|db\Connection reference
*/
public $db;

/**
* Constructor dependency
* @var web\Request
*/
protected $request;

public function init(): void
{
parent::init();
// Ensuring dependencies
$this->db = di\Instance::ensure($this->db, db\Connection::class);
}

public function handlers(): array
{
return [
// using method handler defined as string with name
db\ActiveRecord::EVENT_BEFORE_INSERT => 'beforeInsert',
// using method handler defined as array callable
db\ActiveRecord::EVENT_AFTER_INSERT => 'afterInsert',
// using \Closure handler
db\ActiveRecord::EVENT_INIT => function(base\Event $event): void {
// handle event
},
];
}

protected function beforeInsert(base\Event $event): void
{
// handle event
}

protected function afterInsert(db\AfterSaveEvent $event): void
{
// handle evnet
}
}
```
[Example (handle `yii\db\ActiveRecord` events)](./examples/item.php)

### Bootstrap\Behavior
This behavior should be used when some handlers will be used only in one module.
Expand All @@ -178,126 +120,10 @@ For example:

It can be used with controller or any another component (you will need to configure events).

```php
<?php

namespace Example;

use Horat1us\Yii\StaticBehavior;
use yii\base;
use yii\web;

interface Delivery {
public function send(string $recipient, string $message);
}

interface Token {
public function getValue(): string;
public function getOwner(): string;
}

/**
* Class ActionLogin
* @package Example
*/
class ActionLogin extends base\Action
{
public const EVENT_TOKEN = 'token';

public function run() {
// Create token here
/** @var Token $token */

$this->trigger(static::EVENT_TOKEN, new base\Event([
'data' => $token,
]));

return ['state' => 'ok'];
}
}

/**
* Class AuthenticationController
* @package Example
*/
class AuthenticationController extends web\Controller
{
public function actions(): array
{
return [
'login' => ActionLogin::class,
];
}
}

/**
* Class TokenItem
* @package Example
*/
class TokenItem extends StaticBehavior\Item
{
/** @var Delivery */
protected $delivery;

public function __construct(Delivery $delivery, array $config = [])
{
parent::__construct($config);
$this->delivery = $delivery;
}

public function handlers(): array {
return [
ActionLogin::EVENT_TOKEN => 'handleTokenEvent',
];
}

/**
* @param base\Event $event
* @throws \InvalidArgumentException
*/
public function handleTokenEvent(base\Event $event): void
{
if(!$event->data instanceof Token) {
throw new \InvalidArgumentException("Cannot handle event without token data.");
}
$token = $event->data;
$this->delivery->send(
$recipient = $token->getOwner(),
$message = $token->getValue()
);
}
}

class Module extends base\Module {
public $controllerMap = [
'class' => AuthenticationController::class,
'as staticLogin' => [
'class' => StaticBehavior\Bootstrap\Behavior::class,
'events' => [
// handlers will be attached only before controller run
base\Controller::EVENT_BEFORE_ACTION => 'beforeAction',
base\Controller::EVENT_AFTER_ACTION => 'afterAction',
],
'behaviors' => [
[
'class' => StaticBehavior::class,
'target' => ActionLogin::class,
'items' => [
'sendToken' => TokenItem::class,
],
],
],
],
];
}

// append module to your application
```

[Detailed example](./examples/static-behavior.php)
[Example (authorization tokens)](./examples/bootstrap-behavior.php)

## Contributors
- [Alexander <Horat1us> Letnikow](mailto:[email protected])

## License
[MIT](./LICENSE)
[MIT](./LICENSE)
115 changes: 115 additions & 0 deletions examples/bootstrap-behavior.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
<?php

namespace Example;

use Horat1us\Yii\StaticBehavior;
use yii\base;
use yii\web;

// Delivery service to send message to user (recipient)
interface Delivery {
public function send(string $recipient, string $message);
}

// Token entity
interface Token {
public function getValue(): string;
public function getOwner(): string;
}

/**
* Class ActionLogin
* @package Example
*/
class ActionLogin extends base\Action
{
public const EVENT_TOKEN = 'token';

public function run() {
// Create token here
/** @var Token $token */

$this->trigger(static::EVENT_TOKEN, new base\Event([
'data' => $token,
]));

return ['state' => 'ok'];
}
}

/**
* Class AuthenticationController
* @package Example
*/
class AuthenticationController extends web\Controller
{
public function actions(): array
{
return [
'login' => ActionLogin::class,
];
}
}

/**
* Class TokenItem
* @package Example
*/
class TokenItem extends StaticBehavior\Item
{
/** @var Delivery */
protected $delivery;

public function __construct(Delivery $delivery, array $config = [])
{
parent::__construct($config);
$this->delivery = $delivery;
}

public function handlers(): array {
return [
ActionLogin::EVENT_TOKEN => 'handleTokenEvent',
];
}

/**
* @param base\Event $event
* @throws \InvalidArgumentException
*/
public function handleTokenEvent(base\Event $event): void
{
if(!$event->data instanceof Token) {
throw new \InvalidArgumentException("Cannot handle event without token data.");
}
$token = $event->data;
$this->delivery->send(
$recipient = $token->getOwner(),
$message = $token->getValue()
);
}
}

class Module extends base\Module {
public $controllerMap = [
'class' => AuthenticationController::class,
'as staticLogin' => [
'class' => StaticBehavior\Bootstrap\Behavior::class,
'events' => [
// handlers will be attached only before controller run
base\Controller::EVENT_BEFORE_ACTION => 'beforeAction',
base\Controller::EVENT_AFTER_ACTION => 'afterAction',
],
'behaviors' => [
[
'class' => StaticBehavior::class,
'target' => ActionLogin::class,
'items' => [
'sendToken' => TokenItem::class,
],
],
],
],
];
}

// append module to your application
59 changes: 59 additions & 0 deletions examples/item.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php

namespace Example;

use Horat1us\Yii\StaticBehavior;
use yii\base;
use yii\web;
use yii\db;
use yii\di;

/**
* Class Item
* @package Example
*/
class Item extends StaticBehavior\Item
{
/**
* Configurable dependency
* @var string|array|db\Connection reference
*/
public $db;

/**
* Constructor dependency
* @var web\Request
*/
protected $request;

public function init(): void
{
parent::init();
// Ensuring dependencies
$this->db = di\Instance::ensure($this->db, db\Connection::class);
}

public function handlers(): array
{
return [
// using method handler defined as string with name
db\ActiveRecord::EVENT_BEFORE_INSERT => 'beforeInsert',
// using method handler defined as array callable
db\ActiveRecord::EVENT_AFTER_INSERT => 'afterInsert',
// using \Closure handler
db\ActiveRecord::EVENT_INIT => function(base\Event $event): void {
// handle event
},
];
}

protected function beforeInsert(base\Event $event): void
{
// handle event
}

protected function afterInsert(db\AfterSaveEvent $event): void
{
// handle evnet
}
}

0 comments on commit b263bbe

Please sign in to comment.