kitano/connection-bundle

用于创建两个对象之间任何连接的定向图实现(例如,社交网络中的关注/被关注关系)

安装: 100

依赖项: 0

建议者: 0

安全: 0

星级: 13

关注者: 6

分支: 6

开放问题: 11

类型:symfony-bundle

dev-master 2013-04-10 18:25 UTC

This package is not auto-updated.

Last update: 2020-01-10 14:51:00 UTC


README

状态:正在工作。不稳定。

对象之间建立连接的定向图实现。

例如,连接可以表示

  • 社交网络中两个用户之间的关注/被关注关系
  • 用户“关注”一个“标签”
  • 实际上,任何需要连接的根对象之间的关系,无论出于何种原因 ;-)

此包的目的不是为了提供一个现成的实现,而至少是一个代码库,以简化在 Symfony 2 应用程序中集成此类系统。

用例

<?php
namespace Acme\DemoBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;

class HomeController extends Controller {

    public function indexAction() 
    {
        $connectionManager = $this->get("kitano_connection.manager.connection");

        $userA = $userRepository->find(1);
        $userB = $bookRepository->find(42);

        // User A wants to "follow" User B activity
        // User A clics "follow" button on User B profile page
        $connectionManager->connect($userA, $userB, 'follow');
    }

    public function newPostAction() 
    {
        $connectionManager = $this->get("kitano_connection.manager.connection");

        $userA = $userRepository->find(1);

        // User B does something like creating a new Post
        // We notify all users connected to B
        $connections = $connectionManager->getConnectionsFrom($userB, array('type' => 'follow'));

        foreach($connections as $connection) {
            // Notify !
        }
    }
}

配置

a) 简单功能示例

一个简单的配置可能如下所示

kitano_connection:
    persistence:
        type: doctrine_orm

使用此配置,您可以将 KitanoConnectionBundle 用作 用例 示例。

配置后,别忘了更新您的 RDBMS 架构

$ php app/console doctrine:schema:update

b) 自定义连接

如果您想使用自定义连接实体。

kitano_connection:
    persistence:
        type: doctrine_orm
        managed_class: "Acme\Entity\Connection"

在这种情况下,别忘了为 Acme\Entity\Connection 定义一个实体模式

c) 带自定义持久层的自定义连接

如果您想使用自定义仓库,请使用上述配置。

kitano_connection:
    persistence:
        type: custom
        managed_class: "Acme\Entity\Connection"

定义一个名为: kitano_connection.repository.connection 的服务,该服务实现了 Kitano\ConnectionBundle\Repository\ConnectionRepositoryInterface

事件

如果您想挂钩系统,则可用事件。

namespace Acme\DemoBundle\Event;

use Symfony\Component\EventDispatcher\EventSubscriberInterface;

use Kitano\ConnectionBundle\Event\ConnectionEvent;

class ConnectionSubscriber implements EventSubscriberInterface
{
    public static function getSubscribedEvents()
    {
        return array(
            ConnectionEvent::CONNECTED => array('onConnected', 0),
            ConnectionEvent::DISCONNECTED => array('onDisconnected', 0),
        );
    }

    public function onConnected(ConnectionEvent $event)
    {
        $connection = $event->getConnection();
        // ...
    }

    public function onDisconnected(ConnectionEvent $event)
    {
        $connection = $event->getConnection();
        // ...
    }
}
<?xml version="1.0" ?>
<container xmlns="https://symfony.com.cn/schema/dic/services"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xsi:schemaLocation="https://symfony.com.cn/schema/dic/services https://symfony.com.cn/schema/dic/services/services-1.0.xsd">
    <parameters>
        <parameter key="acme.demo.connection_subscriber.class">Acme\DemoBundle\Event\ConnectionSubscriber</parameter>
    </parameters>

    <services>
        <!-- listener -->
        <service id="acme.demo.connection_subscriber" class="%acme.demo.connection_subscriber.class%" public="false">
            <tag name="kernel.event_subscriber" />
        </service>
    </services>
</container>

限制

  • 此包只能处理一个持久层。这意味着您不能连接一个 Document 对象与一个 Entity 对象。

许可证

此包受 MIT 许可证的保护。请参阅包中的完整许可证。

Resources/meta/LICENSE

测试

需要 PHPUnit, phpunit/DbUnit

$ php composer.phar update --dev
$ phpunit