contao / core-bundle

[READ-ONLY] Contao Core Bundle
GNU Lesser General Public License v3.0
122 stars 57 forks source link

[RFC] Quick draft of event helper #1562

Closed aschempp closed 5 years ago

aschempp commented 6 years ago

Just a quick idea I came up with to deal with the repetitive code when calling hooks and callbacks.

Example usage:

Call simple hook (https://github.com/contao/core-bundle/blob/fa05410fbd75cca7655599e59972475fe371ca0e/src/Resources/contao/pages/PageRegular.php#L77-L85)

if (isset($GLOBALS['TL_HOOKS']['getPageLayout']) && \is_array($GLOBALS['TL_HOOKS']['getPageLayout']))
{
    $helper->triggerAll(
        $GLOBALS['TL_HOOKS']['getPageLayout'],
        [$objPage, $objLayout, $this]
    );
}

Call hook with return value (https://github.com/contao/core-bundle/blob/60403e47690ffcc253096750719a7d310f5f4561/src/Resources/contao/classes/FrontendTemplate.php#L56-L64)

// HOOK: add custom parse filters
if (isset($GLOBALS['TL_HOOKS']['parseFrontendTemplate']) && \is_array($GLOBALS['TL_HOOKS']['parseFrontendTemplate']))
{
    $strBuffer = $helper->triggerAll(
        $GLOBALS['TL_HOOKS']['parseFrontendTemplate'],
        [$strBuffer, $this->strTemplate],
        0
    ];
}

Call hook with return validation (https://github.com/contao/core-bundle/blob/5a411a7383e7ef24289bc6a739269ae572a52db1/src/Resources/contao/library/Contao/InsertTags.php#L985-L999)

if (isset($GLOBALS['TL_HOOKS']['replaceInsertTags']) && \is_array($GLOBALS['TL_HOOKS']['replaceInsertTags']))
{
    $varValue = $helper->triggerAll(
        $GLOBALS['TL_HOOKS']['replaceInsertTags'],
        [$tag, $blnCache, $arrCache[$strTag], $flags, $tags, $arrCache, $_rit, $_cnt],
        null,
        function ($result) { return $varValue !== false; }
    );

    if ($varValue !== false)
    {
        $arrCache[$strTag] = $varValue;
        break 2;
    }
}

The same works for every DCA callback. We could also add a quick helper to call isset and is_array on hooks, but that won't work for callbacks.

leofeyer commented 6 years ago

The code for "hook with return validation" is more complex than the original code, so I'm not sure about this.

aschempp commented 6 years ago

The code for "hook with return validation" is more complex than the original code

Why is it more complex? The whole point is to get rid of the loop and System::importStatic in every implementation… This would also very much simplify things like a third option instead of array and callbacks (like triggering an event or whatever stupid thing we come up with 😁 )

m-vo commented 6 years ago

... and it would allow making changes/optimizing at a central place. Right now the code segments have no common semantic.

aschempp commented 5 years ago

@leofeyer @Toflar @ausi would anyone agree this is a nice feature, or should we close the PR?

Toflar commented 5 years ago

I don't see the use case, sorry. It's only for hooks and we don't want to extend any logic for hooks imho.

leofeyer commented 5 years ago

I also vote to close the PR, because

The code for "hook with return validation" is more complex than the original code