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

Thelia loop Overriding #1787

Merged
merged 6 commits into from Nov 26, 2015

Conversation

Projects
None yet
4 participants
@bibich
Contributor

bibich commented Nov 23, 2015

This PR is a refactoring and a new way to extends Thelia loops. This is more classical than the #1781 and it use the event dispatcher.

In this PR, the loop name is automatically added according to the loops definitions.

Here is an example on how to extends loops in your module :

first the config.xml :

    <services>
        <service id="loopevent.loop.overrides" class="LoopEvent\EventListeners\Loop" scope="request">
            <argument type="service" id="request"/>
            <tag name="kernel.event_subscriber" />
        </service>
    </services>

The class :

/**
 * Extends Lang and cart Loops
 *
 * Class Loop
 * @author Julien Chanséaume <julien@thelia.net>
 */
class Loop implements EventSubscriberInterface
{
    /** @var Request $request */
    protected $request = null;

    public function __construct(Request $request)
    {
        $this->request = $request;
    }

    /**
     * Add output variable JSON for all loops containing the data in JSON
     */
    public function addJSONOutput(LoopExtendsParseResultsEvent $event)
    {
        $loopResult = $event->getLoopResult();

        foreach ($loopResult as $row) {
            $row->set('JSON', json_encode($row->getVarVal()));
        }
    }

    /**
     * Add a new parameter for loop lang
     */
    public function langArgDefinitions(LoopExtendsArgDefinitionsEvent $event)
    {
        $argument = $event->getArgumentCollection();
        $argument->addArgument(Argument::createBooleanTypeArgument('uuid', false));
    }

    /**
     * Set the uuid parameters form the query string
     */
    public function langInitializeArgs(LoopExtendsInitializeArgsEvent $event)
    {
        $parameters = $event->getLoopParameters();
        if ($this->request->query->has('loop-uuid')) {
            $parameters['uuid'] = 1;
            $event->setLoopParameters($parameters);
        }
    }

    /**
     * Change the query search of the loop lang
     */
    public function langBuildModelCriteria(LoopExtendsBuildModelCriteriaEvent $event)
    {
        $event->getModelCriteria()->orderBy('id', Criteria::DESC);
    }

    /**
     * Add the UUID variable to the output variables of the loop lang
     */
    public function langParseResults(LoopExtendsParseResultsEvent $event)
    {
        $loopResult = $event->getLoopResult();
        $arguments = $event->getLoop()->getArgumentCollection();
        if ($arguments->get('uuid')->getValue()) {
            foreach ($loopResult as $row) {
                $row->set('UUID', uniqid());
            }
        }
    }

    public function cartBuildArray(LoopExtendsBuildArrayEvent $event)
    {
        $elements = $event->getArray();

        /** @var CartItem $item */
        foreach ($elements as $item) {
            $item->setPrice("0");
            var_dump($item);
        }

        //$event->getModelCriteria()->orderBy('id', Criteria::DESC);
    }

    /**
     * @inheritdoc
     */
    public static function getSubscribedEvents()
    {
        return [
            // Lang
            TheliaEvents::getLoopExtendsEvent(TheliaEvents::LOOP_EXTENDS_ARG_DEFINITIONS, 'lang') => ['langArgDefinitions', 128],
            TheliaEvents::getLoopExtendsEvent(TheliaEvents::LOOP_EXTENDS_INITIALIZE_ARGS, 'lang') => ['langInitializeArgs', 128],
            TheliaEvents::getLoopExtendsEvent(TheliaEvents::LOOP_EXTENDS_BUILD_MODEL_CRITERIA, 'lang') => ['langBuildModelCriteria', 128],
            TheliaEvents::getLoopExtendsEvent(TheliaEvents::LOOP_EXTENDS_PARSE_RESULTS, 'lang') => ['langParseResults', 128],
            // Cart
            TheliaEvents::getLoopExtendsEvent(TheliaEvents::LOOP_EXTENDS_BUILD_ARRAY, 'cart') => ['cartBuildArray', 128],
        ];
    }
}

What do you think about it ?

@bcbrr

This comment has been minimized.

Show comment
Hide comment
@bcbrr

bcbrr Nov 23, 2015

Contributor

👍

Contributor

bcbrr commented Nov 23, 2015

👍

bibich added some commits Nov 20, 2015

Updated BaseLoop to dispatch event to extends it
Updated BaseLoop to dispatch event to extends it

Fixed dispatch for null search
Added action for loop to trigger the dispatch for specific loop
Added action for loop to trigger the dispatch for specific loop
@roadster31

This comment has been minimized.

Show comment
Hide comment
@roadster31

roadster31 Nov 24, 2015

Set this to null and ... (see intialize)

Set this to null and ... (see intialize)

@roadster31

This comment has been minimized.

Show comment
Hide comment
@roadster31

roadster31 Nov 24, 2015

if (null === self::$loopDefinitions) is more efficient.

if (null === self::$loopDefinitions) is more efficient.

@roadster31

This comment has been minimized.

Show comment
Hide comment
@roadster31

roadster31 Nov 24, 2015

IMO, this event listener is useless. The dispatching of the "extend event" could be done in the BaseLoop class, thus saving 5 event dispatching for each loop usage.

roadster31 commented on 1d64145 Nov 24, 2015

IMO, this event listener is useless. The dispatching of the "extend event" could be done in the BaseLoop class, thus saving 5 event dispatching for each loop usage.

@gillesbourgeat gillesbourgeat added this to the 2.3.0-alpha1 milestone Nov 24, 2015

@bibich bibich changed the title from [WIP] Thelia loop Overriding - version 2 to [WIP] Thelia loop Overriding Nov 25, 2015

@gillesbourgeat gillesbourgeat changed the title from [WIP] Thelia loop Overriding to Thelia loop Overriding Nov 26, 2015

gillesbourgeat added a commit that referenced this pull request Nov 26, 2015

@gillesbourgeat gillesbourgeat merged commit c868671 into thelia:master Nov 26, 2015

3 checks passed

Scrutinizer Analysis 5 new issues, 30 updated code elements
Details
Scrutinizer Tests Tests are not configured
continuous-integration/travis-ci/pr The Travis CI build passed
Details
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment