heristop/webservice-bundle

此包允许与 soap web 服务同步一个表。

dev-master / 1.0.x-dev 2014-02-06 17:25 UTC

This package is not auto-updated.

Last update: 2024-09-14 16:50:09 UTC


README

此包允许与 soap web 服务同步一个表。

安装

从 GitHub 下载源代码

    [HeriJobQueueBundle]
        git=https://github.com/heristop/HeriWebServiceBundle.git
        target=/bundles/Heri/WebServiceBundle/

或者使用 composer 添加以下要求

{
    "require": {
        "heristop/webservice-bundle": "*"
    }
}

在自动加载中注册命名空间

    $loader->registerNamespaces(array(
        ...
        'Heri' => __DIR__.'/../vendor/bundles',
    ));

在 AppKernel 中加载

    $bundles[] = new Heri\WebServiceBundle\HeriWebServiceBundle();

使用方法

首先,在你的实体定义中添加 toUpdate 列表。同步后,此字段将被设置为 false

    /**
     * @ORM\Column(name="to_update", type="boolean")
     */
    protected $toUpdate;

生成获取器和设置器

    app/console doctrine:generate:entities %YourBundle%

%YourBundle%/Service 目录中创建一个类,用于与 WSDL 应用映射。该包包含一个示例

    namespace Heri\Bundle\WebServiceBundle\Service;
    
    use Heri\Bundle\WebServiceBundle\ClientSoap\ClientObject;
    
    class Sample extends ClientObject
    {
        public function configure()
        {
            $this->name  = 'sample';
            $this->table = 'HeriWebServiceBundle:Sample';
            $this->func  = 'addSample';
        }
        
        public function hydrate($record)
        {  
            $this->params = array(
                'id'    => $record->getId(),
                'label' => $record->getLabel(),
            );
        }
    
    }

在 config.yml 中配置 web 服务连接

    heri_web_service:  
        namespaces:             [ %YourBundleNamespace%\Service ]
        authentication:                      # optional
            login:              %login%
            password:           %password%
        webservices:
            brand:
                name:           brand
                url:            %soap_url%
                authentication: true         # optional

然后,使用以下命令调用 web 服务并检索所有 toUpdate 设置为 true 的记录

    app:console webservice:load %Service%

要查看可用函数的列表,请添加 list 选项。

配置

编辑 config.yml 添加 SyncListener

    services:
       sync.listener:
            class: Heri\Bundle\WebServiceBundle\Listener\SyncListener
            tags:
                - { name: doctrine.event_listener, event: prePersist, connection: default }
                - { name: doctrine.event_listener, event: postPersist, connection: default }

作业队列

此包可以与 HeriJobQueueBundle 一起使用,以管理多个 web 服务连接。

覆盖配置并在 config.yml 中添加对 jobqueue 服务的依赖

    services:
        sync.listener:
            class: Heri\Bundle\WebServiceBundle\Listener\SyncListener
            arguments: [@jobqueue]
            tags:
                - { name: doctrine.event_listener, event: prePersist, connection: default }
                - { name: doctrine.event_listener, event: postPersist, connection: default }
        jobqueue:
            class: Heri\Bundle\JobQueueBundle\Service\QueueService
            arguments: [@logger]
            tags:
                - { name: monolog.logger, channel: jobqueue }

在对象中添加一个名为 synchronize() 的方法,返回队列名称

    /**
     * Adds synchronization in specified queue
     * 
     * @return string
     */
    public function synchronize()
    {
        return '%queue_name%';
    }

当记录保存在数据库中时,同步到 web 服务的操作将被推送到队列中。

注意

您可以覆盖 ClientObject 类以应用特定的配置。

以下示例显示了连接您的应用程序到 Magento 平台的方法

heri_web_service:
    namespaces:             [ %YourBundleNamespace%\Service ]
    authentication:
        login:              sampleuser
        password:           123456
    webservices:
        magento:
            name:           api
            url:            http://myshop-local.com/index.php/api/
abstract class ClientMagento extends ClientObject
{
    protected $name = 'api';
    
    protected function callFunction($func, array $params = array())
    {
        $connection = $this->getContainer()->getConnection();
        $sessionId = $this->client->login($connection->getLogin(), $connection->getPassword());
        
        return $this->client->call(
            $sessionId,
            $func,
            $params
        );
    }
}