adadgio/rocket-bundle

许多酷炫的工具、助手和连接器

安装: 19

依赖: 0

建议者: 0

安全: 0

星标: 1

关注者: 1

分支: 0

类型:symfony-bundle

1.3 2018-11-12 10:49 UTC

This package is not auto-updated.

Last update: 2024-09-18 08:22:07 UTC


README

安装

使用 composer 安装。

composer require adadgio/gear-bundle

将包添加到您的应用内核。

new Adadgio\GearBundle\AdadgioGearBundle();

目录

  1. API注解和认证
  2. 配置
  3. 注解使用
  4. NodeRed连接器和循环
  5. 配置
  6. 使用
  7. CSV导出器
  8. CSV读取器
  9. 从数据中实体化
  10. 序列化器
  11. [其他]

API注解和认证

非常容易创建API端点并通过任何类型的认证系统来保护它们。

配置

# in config.yml (basic auth example)
adadgio_gear:
    auth:
        type: Basic     # options: Basic (more default types not available in current version)
        class: ~        # either define "class" or "provider", ex. "Adadgio\GearBundle\Component\Api\Authenticatir\AuthProvider"
        #provider: ~    # either define "class" or "provider", ex. "adadgio_gear.api.authenticator_example_service"
        user: benny
        password: test

# in config.yml (custom service auth example, like API client in database)
adadgio_gear:
    auth:
        #type: ~
        #class: ~       # either define "class" or "provider", ex. "Adadgio\GearBundle\Component\Api\Authenticatir\AuthProvider"
        provider: my_bundle.api.my_client_auth  # you create the service and define what to do: see "adadgio_gear.api.authenticator_example_service"

注解使用

use Adadgio\GearBundle\Component\Api\ApiRequest;
use Adadgio\GearBundle\Component\Api\ApiResponse;
use Adadgio\GearBundle\Component\Api\Annotation\Api;

/**
 * @Route("/test/gear/api", name="test_gear_api")
 * @Api(method={"POST","GET"}, enabled=true)
 */
public function myApiEndpointAction(Request $request, ApiRequest $apiRequest)
{
    return new ApiResponse(array('yes' => 'sir'));
}

示例:使用自定义认证器服务。

use Adadgio\GearBundle\Component\Api\Authenticator\AuthProvider;
use Adadgio\GearBundle\Component\Api\Authenticator\AuthProviderInterface;

class ExampleAuthProviderService extends AuthProvider implements AuthProviderInterface
{
    /**
     * Build your service like you build services every day!
     */
    public function __construct()
    {
        // inject anything in here, like doctrien.orm.entity_manager, or whatever.
    }

    /**
     * Checks auth. You could get request headers key and check that
     * the secret key and client id are in your database for example...
     *
     * @return boolean
     */
    public function authenticate()
    {
        // your owns logic here
        $request = $this->getRequest();
        $headers = $request->getHeaders();

        return true;
    }
}

NodeRed连接器和循环

配置

# import routing
_adadgio_gear:
    resource: "@AdadgioGearBundle/Resources/config/routing.yml"
# in config.yml
adadgio_gear:
    nodered:
        host: 127.0.0.1
        port: 1880          # optional
        protocol: http://   # optional
        http_auth:          # optional (depends on Node Red httpNodeAuth param)
            user: 
            pass: ~

然后您需要在NodeRed应用中安装流程。

$ php app/console adadgio:nodered:install --output=/destination/folder

您需要手动在NodeRed应用中导入流程(或在NodeRed设置.js中使用flows目录配置)。

alt tag

使用

要触发循环(或仅延迟消息),您需要创建一个 \Payload,NodeRed会将它发送回AdagagioGearBundle循环控制器(有关更多信息,请参阅routing.yml)。当控制器接收到回传的payload时,它会分发一个事件。您可以通过 监听事件 来修改payload以实现目标。

use Adadgio\GearBundle\Connector\NodeRed\Payload;

// payload contains 3 initial params you cannot override (pid, kill, iteration)
// and they change automatically during the loop lifecycle
$payload = new Payload();

// add more params
$payload->setParameter('my_name', 'Romain'); // nb: 3 params are here by default

// use the connector to start (trigger) the loop
$this->get('adadgio_gear.nodered.connector')->send('POST', '/adadgio/gear/loop/start', $payload); // @todo pass this more transparently

循环将永远不会停止,直到您更改payload的 kill 属性。现在 监听 循环回调。Nodered会无限期地调用它,除非您 kill payload。

// in some listener, far, far away, a long long time ago
// the listener must listen to "adadgio_gear.nodered.payload_received"
public function onPayloadReceived(\Adadgio\GearBundle\Connector\NodeRed\Event\PayloadEvent $event)
{
    // you might need the request, who knows
    $request = $event->getRequest();

    $payload = $event->Payload();
    // notice iteration changed to +1, pid always stays the same (unless you trigger another process)
    // otherwise you get back the parameters you defined earlier

    // if you wanna stop the flow
    if ($payload->getIteration() > 3) {
        $payload->kill();
    }

    // process... something, or modify your input params at runtime
    $name = $payload->getParameter('my_name');
    $name = ... // change your name!
    $payload->setParameter('my_name', $name);
}

CSV导出器

use Adadgio\GearBundle\Component\Reader\CsvExporter;

$exporter = $this->get('adadgio_gear.csv_exporter')
    ->setName("exportFileName")
    ->setColumns(array('label', 'views'))
    ->setData(array(array('one','two'),array('three','for')))
    ->generate();

CSV读取器

use Adadgio\GearBundle\Component\Reader\CsvReader;

$csv = new CsvReader('data/test.csv');

$data = $csv
    ->setDelimiter(';')
    ->read(5, 15) // reads rows from 5 to 15 included (pass null for no limit and offset)
    ->getData();

从数据中实体化

```php use Adadgio\GearBundle\Component\Hydration\EntityHydrator;

$hydrator = new EntityHydrator();

// $data = ... 前一个示例中的数据 $hydrator ->hydrate('Adadgio\DoctrineDQLBundle\Entity\TestEntity') ->with($data) ->map(0, 'id') // 将数组列索引映射到实体属性 ->map(1, 'name');

$entities = $hydrator->getEntities();

</sub> ## <a name="serializer"></a>序列化器 将对象转换为数组。可以转换单个对象或对象集合。 ```php use Adadgio\GearBundle\Component\Serialization\EntitySerializer; $entities = $em->getRepository('AppBundle:Product')->findAll(); $serializer = $this->get('adadgio_gear.entity_serializer'); $dataSerialized = $serializer->serialize($entitiesÂ); 

其他

在开发环境中使用自定义设置文件启动NodeRed(在此情况下使用Ngrok)。

# 使用自定义设置和pm2进程管理器启动node red pm2 start /usr/local/bin/node-red -- -v --settings=/Library/WebServer/home/symfony/360medical-v3/app/data/nodered/settings.js