thomasvargiu / pami-module
ZF2 模块,用于 PAMI Asterisk 库
Requires
- php: ^7.4
- ext-json: *
- marcelog/pami: ^2.0.2
- psr/container: ^1.0
- psr/event-dispatcher: ^1.0
- psr/simple-cache: ^1.0
Requires (Dev)
- phpspec/prophecy-phpunit: ^2.0
- phpstan/phpstan: ^0.12.29
- phpunit/phpunit: ^9.2
This package is auto-updated.
Last update: 2024-08-29 03:36:00 UTC
README
为 PAMI 库编写的 ZF 模块。
配置
首先,你应在配置中定义连接和客户端选项。客户端选项都是可选的。
return [ 'pami_module' => [ 'connection' => [ 'default' => [ 'host' => '', // IP or hostname of asterisk server 'port' => 5038, // (optional) Asterisk AMI port (default: 5038) 'username' => '', // Username for asterisk AMI 'secret' => '', // Password for asterisk AMI 'scheme' => 'tcp://', // (optional) Connection scheme (default: tcp://) 'connect_timeout' => 10000, // (optional) Connection timeout in ms (default: 10000) 'read_timeout' => 10000 // (optional) Read timeout in ms (default: 10000) ] ], 'client' => [ 'default' => [] ] ] ]
然后你可以从服务定位器获取两个服务
pami.client.default
:PamiModule 客户端
PamiModule 客户端
你可以从服务定位器获取客户端。
use PamiModule\Service\Client; use PAMI\Client\Impl\ClientImpl; // Getting the PamiModule client /** @var Client $client */ $client = $serviceLocator->get('pami.client.default');
方法
原始的 Pami
客户端(连接)被注入到 PamiModule
中,而 PamiModule
动作委托给原始客户端。
映射动作
事件
PamiModule
客户端有一个 EventManager
实例被注入其中。
以下方法将触发与方法相同名称的事件以及 .pre
和 .post
后缀
connect()
disconnect()
process()
sendAction()
sendAction()
事件在 sendAction.pre
事件中有 action
参数,在 sendAction.post
事件中有 action
和 response
参数,允许你在动作被分发之前修改动作或缓存响应。
PAMI 事件
所有 PAMI 事件都会转发到将触发事件的(PamiModule\Event\PamiEvent
)事件管理器。
事件名称将是 event.<name>
(例如:event.ExtensionStatus
)。
当然,你可以访问原始事件以检索事件数据(见下面示例)。
事件目标是 PamiModule
客户端。
示例
use PamiModule\Service\Client; use PamiModule\Event\PamiEvent; /* @var Client $client */ $client = $serviceLocator->get('pami.client.default'); $client->getEventManager()->attach('event.Bridge', function(PamiEvent $event) { // Getting the client /* @var Client $client */ $client = $event->getTarget(); // Getting the original Event /* @var \PAMI\Message\Event\BridgeEvent $pamiEvent */ $pamiEvent = $event->getEvent(); });
多个客户端
return [ 'pami_module' => [ 'connection' => [ 'default' => [ // configuration ], 'asterisk2' => [ // configuration ] ], 'client' => [ 'default' => [], 'asterisk2 => [] ] ] ]
你可以通过服务名称检索客户端
pami.client.default
pami.client.asterisk2
共享相同连接的多个客户端
你可以创建另一个与另一个客户端相同的连接的客户端
return [ 'pami_module' => [ 'connection' => [ 'default' => [ // configuration ] ], 'client' => [ 'default' => [ 'connection' => 'default' ], 'client2' => [ 'connection' => 'default' ] ] ] ]
$client1 = $serviceLocator->get('pami.client.default'); $client2 = $serviceLocator->get('pami.client.client2'); $client1->getConnection() === $client2->getConnection(); // true
获取原始 PAMI 客户端
你可以通过两种方式检索原始 PAMI 客户端
从服务定位器
use PAMI\Client\Impl\ClientImpl; /* @var ClientImpl $connection */ $connection = $serviceLocator->get('pami.connection.default');
从 PamiModule
客户端
use PamiModule\Service\Client; use PAMI\Client\Impl\ClientImpl; // Getting the PamiModule client /* @var Client $client */ $client = $serviceLocator->get('pami.client.default'); // Getting the PAMI client /* @var ClientImpl $connection */ $connection = $client->getConnection();
可用监听器
有一些监听器可供使用。
PamiModule\Listener\ConnectionStatusListener
PamiModule\Listener\CacheListener
ConnectionStatusListener
此监听器负责维护连接状态,并在需要时调用 connect()
方法。
当你在一些服务之间共享客户端时,这可能很有用,因为你不需要在不知道连接状态的情况下调用 connect()
方法。你可以直接调用 process()
和 sendAction()
方法,如果尚未打开连接,它将自动调用 connect()
。
你还可以在任何应用程序点使用 connect()
和 disconnect()
方法,监听器将只在实际需要时打开或关闭连接。
为了使用此监听器,你不能直接使用连接(原始的 PAMI
客户端),因为连接状态是通过监听 PamiModule
客户端事件来维护的。
如果你想在多个客户端中使用监听器,你需要为每个客户端附加一个新实例。
在调用任何方法之前,你必须将其附加到客户端,所以最好的方法是使用委托器工厂。此库提供了一个现成的委托器工厂
return [ 'service_manager' => [ 'delegators' => [ 'pami.client.default' => [ 'PamiModule\\Service\\ConnectionStatusDelegatorFactory' ] ] ] ];
注意:如果您在客户端之间共享某些连接,则必须将相同的监听器附加到客户端,因此您需要创建自己的DelegatorFactory。
CacheListener
您可以使用CacheListener来缓存某些操作的输出。构造函数需要一个缓存存储实例以及监听器可以缓存响应的操作名称。
use PamiModule\Listener\CacheListener; use Zend\Cache\Storage\StorageInterface; $client = $serviceLocator->get('pami.client.default') /* @var StorageInterface $cache */ $cache = $serviceLocator->get('asterisk_sippeers_cache'); $actionsToCache = ['SIPPeers', 'ShowPeer']; $cacheListener = new CacheListener($cache, $actionsToCache); $client->getEventManager()->attachAggregate($cacheListener);