undf/connection-bundle

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

安装: 459

依赖者: 0

建议者: 0

安全: 0

星标: 2

关注者: 3

分支: 6

类型:symfony-bundle

dev-undf 2014-07-04 14:39 UTC

This package is not auto-updated.

Last update: 2024-09-14 12:57:08 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.ac.cn/schema/dic/services"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xsi:schemaLocation="https://symfony.ac.cn/schema/dic/services https://symfony.ac.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>

限制

  • 此包只能处理一个持久层。这意味着您不能将文档对象与实体对象连接起来。

许可

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

Resources/meta/LICENSE

测试

需要PHPUnit,phpunit/DbUnit

$ php composer.phar update --dev
$ phpunit