Skip to content

Add Widget Items capacity to be read #97

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
May 15, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion Bundle/CoreBundle/Annotations/Reader/AnnotationReader.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use Victoire\Bundle\CoreBundle\Annotations\ReceiverProperty;
use Victoire\Bundle\WidgetBundle\Entity\Widget;
use Victoire\Bundle\WidgetBundle\Helper\WidgetHelper;
use Victoire\Bundle\WidgetBundle\Resolver\Chain\WidgetItemChain;

/**
* The annotation reader for the business entities
Expand All @@ -31,12 +32,13 @@ class AnnotationReader extends AnnotationDriver
* @param array $paths
* @param array $widgets
*/
public function __construct(Reader $reader, WidgetHelper $widgetHelper, $paths, $widgets)
public function __construct(Reader $reader, WidgetHelper $widgetHelper, $paths, $widgets, WidgetItemChain $widgetItemChain)
{
$this->reader = $reader;
$this->widgetHelper = $widgetHelper;
$this->widgets = $widgets;
$this->paths = $paths;
$this->widgetItemChain = $widgetItemChain;
}

/**
Expand Down Expand Up @@ -218,6 +220,9 @@ private function loadBusinessProperties(\ReflectionClass $class)
private function loadReceiverProperties()
{
$receiverProperties = array();
//Add widgetItems to widgets
$widgetItems = $this->widgetItemChain->getWidgetItems();
$widgets = array_merge($this->widgets, $widgetItems);
foreach ($this->widgets as $widget) {
$class = new \ReflectionClass($widget['class']);

Expand Down
1 change: 1 addition & 0 deletions Bundle/CoreBundle/Resources/config/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ services:
- "@victoire_widget.widget_helper"
- "%victoire_core.base_paths%"
- "%victoire_core.widgets%"
- "@victoire_widget.widget_item_content_resolver_chain"
calls:
- [ setCache, ["@victoire_core.apc.cache"] ]

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php
namespace Victoire\Bundle\WidgetBundle\DependencyInjection\Compiler;

use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Reference;

class WidgetItemPass implements CompilerPassInterface
{
/**
* Process filter
*
* @param ContainerBuilder $container
*/
public function process(ContainerBuilder $container)
{
if ($container->hasDefinition('victoire_widget.widget_item_content_resolver_chain')) {

$definition = $container->getDefinition(
'victoire_widget.widget_item_content_resolver_chain'
);

$taggedServices = $container->findTaggedServiceIds(
'victoire_widget.widget_item'
);

foreach ($taggedServices as $id => $attributes) {
foreach ($attributes as $attribute) {
$definition->addMethodCall(
'addWidgetItem', array(new Reference($id))
);
}

}
}
}
}
11 changes: 11 additions & 0 deletions Bundle/WidgetBundle/Entity/WidgetItemInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace Victoire\Bundle\WidgetBundle\Entity;

/**
* WidgetItemInterface
*/
interface WidgetItemInterface
{

}
35 changes: 35 additions & 0 deletions Bundle/WidgetBundle/Resolver/Chain/WidgetItemChain.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace Victoire\Bundle\WidgetBundle\Resolver\Chain;

use Victoire\Bundle\WidgetBundle\Entity\WidgetItemInterface;
/**
* WidgetItemChain
*/
class WidgetItemChain
{
private $widgetItems;

function __construct()
{
$this->widgetItems = array();
}

public function addWidgetItem(WidgetItemInterface $widgetItem)
{
$classname = get_class($widgetItem);
$parsedClassname = explode( '\\', $classname);
$name = strtolower(preg_replace('/Widget/', '', end($parsedClassname), 1));
$newWidgetItem = array(
'class' => $classname,
'name' => $name
);
$this->widgetItems[$name] = $newWidgetItem;

}

public function getWidgetItems()
{
return $this->widgetItems;
}
}
3 changes: 3 additions & 0 deletions Bundle/WidgetBundle/Resources/config/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ services:
arguments:
- @victoire_widget.widget_helper

victoire_widget.widget_item_content_resolver_chain:
class: Victoire\Bundle\WidgetBundle\Resolver\Chain\WidgetItemChain

victoire_widget.twig.link_extension:
class: Victoire\Bundle\WidgetBundle\Twig\LinkExtension
arguments:
Expand Down
3 changes: 3 additions & 0 deletions Bundle/WidgetBundle/VictoireWidgetBundle.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\HttpKernel\Bundle\Bundle;
use Victoire\Bundle\WidgetBundle\DependencyInjection\Compiler\WidgetContentResolverPass;
use Victoire\Bundle\WidgetBundle\DependencyInjection\Compiler\WidgetItemPass;

class VictoireWidgetBundle extends Bundle
{
Expand All @@ -19,5 +20,7 @@ public function build(ContainerBuilder $container)
parent::build($container);

$container->addCompilerPass(new WidgetContentResolverPass());
//get widgetItems
$container->addCompilerPass(new WidgetItemPass());
}
}