Skip to content

Repository files navigation

ConnectionBundle Build Status

State : Working. Unstable.

Implementation of a directed Graph for establishing connections between objects.

For instance, a connection may represent:

  • follower/following relationship between two users on a social network
  • an user "following" a "tag"
  • in fact, any relation between any root objects which need to be connected for any reason ;-)

The purpose of this bundle is not to get a ready-to-use implementation but at the minimum a code base to ease the integration of such a system in a Symfony 2 application.

Use case

<?phpnamespaceAcme\DemoBundle\Controller;
useSymfony\Bundle\FrameworkBundle\Controller\Controller;
class HomeController extends Controller {
publicfunctionindexAction() {
$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');
}
publicfunctionnewPostAction() {
$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($connectionsas$connection) {
// Notify !
}
}
}

Configuration

a) Short functional example

A simple configuration could be something like that

kitano_connection:
persistence:
type: doctrine_orm

Using this configuration, you can use KitanoConnectionBundle as the Use Case example.

After configuration, don't forget to update your RDBMS schema

$ php app/console doctrine:schema:update

b) Custom connection

If you want a custom connection entity.

kitano_connection:
persistence:
type: doctrine_ormmanaged_class: "Acme\Entity\Connection"

In this case, don't forget to define an entity schema for Acme\Entity\Connection

c) Custom connection with a custom persistance layer

If you want to use a custom repository, use the above configuration.

kitano_connection:
persistence:
type: custommanaged_class: "Acme\Entity\Connection"

Define a service named : kitano_connection.repository.connection which implement Kitano\ConnectionBundle\Repository\ConnectionRepositoryInterface

Events

Events are availble if you want to hook the system.

namespaceAcme\DemoBundle\Event;
useSymfony\Component\EventDispatcher\EventSubscriberInterface;
useKitano\ConnectionBundle\Event\ConnectionEvent;
class ConnectionSubscriber implements EventSubscriberInterface
{
publicstaticfunctiongetSubscribedEvents()
{
returnarray(
ConnectionEvent::CONNECTED => array('onConnected', 0),
ConnectionEvent::DISCONNECTED => array('onDisconnected', 0),
);
}
publicfunctiononConnected(ConnectionEvent$event)
{
$connection = $event->getConnection();
// ...
}
publicfunctiononDisconnected(ConnectionEvent$event)
{
$connection = $event->getConnection();
// ...
}
}
<?xml version="1.0" ?>
<containerxmlns="http://symfony.com/schema/dic/services"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
<parameters>
<parameterkey="acme.demo.connection_subscriber.class">Acme\DemoBundle\Event\ConnectionSubscriber</parameter>
</parameters>
<services>
<!-- listener -->
<serviceid="acme.demo.connection_subscriber"class="%acme.demo.connection_subscriber.class%"public="false">
<tagname="kernel.event_subscriber" />
</service>
</services>
</container>

Limitations

  • This bundle can deal with only one persistance layer. It means that you can't Connect a Document object with an Entity object.

License

This bundle is under the MIT license. See the complete license in the bundle:

Resources/meta/LICENSE

Testing

Require PHPUnit, phpunit/DbUnit

$ php composer.phar update --dev
$ phpunit

About

Implementation of a directed Graph for establishing connections between objects

Resources

Stars

13 stars

Watchers

6 watching

Forks

Releases

Packages

Used by

Contributors

Languages