Home

Awesome

Build Status

Danslo_Aop

When creating Magento modules, you will often want to change core behavior. Magento provides several ways to do this:

The solution? AOP!

What is AOP?

Paraphrasing from the Go! AOP documentation:

AOP (Aspect-Oriented Programming) is an approach to cross-cutting concerns, where the concerns are designed and implemented in a "modular" way (that is, with appropriate encapsulation, lack of duplication, etc.), then integrated into all the relevant execution points in a succinct and robust way, e.g. through declarative or programmatic means.

What it comes down to is that we can register aspects that do method interception through things called Advices. These can go after, before, or around method calls. You can also sort multiple advices which is very useful for multiple modules wanting to modify the same methods. It's actually really similar to how Magento 2 does plugins / interception.

More information will follow...

How does it work?

Example

After installing this module using composer, let's create a sample module:

app/etc/modules/Danslo_TestModule.xml:

<?xml version="1.0" encoding="UTF-8"?>
<config>
    <modules>
        <Danslo_TestModule>
            <active>true</active>
            <codePool>local</codePool>
        </Danslo_TestModule>
    </modules>
</config>

app/code/local/Danslo/TestModule/etc/config.xml:

<?xml version="1.0" encoding="UTF-8"?>
<config>
    <global>
        <aspects>
            <!-- The only requirement is that 'example' is unique in your installation. -->
            <example>Danslo_TestModule_Aspect_Example</example>
        </aspects>
    </global>
</config>

app/code/local/Danslo/TestModule/Aspect/Example.php:

<?php

use Go\Aop\Aspect;
use Go\Lang\Annotation\Before;
use Go\Aop\Intercept\MethodInvocation;

class Danslo_TestModule_Aspect_Example
    implements Aspect
{

    /**
     * Intercept all public methods for Mage_Cms_Block_Page.
     *
     * @param MethodInvocation $invocation Invocation
     * @Before("execution(public Mage_Cms_Block_Page->*(*))")
     */
    public function beforeMethodExecution(MethodInvocation $invocation)
    {
        $obj = $invocation->getThis();
        echo 'Calling Before Interceptor for method: ',
             $invocation->getMethod()->getName(),
             ' with arguments: ',
             json_encode($invocation->getArguments());
    }

}

Flush the cache and reload the homepage.

You will notice that we are intercepting every method call for the Mage_Cms_Block_Page class without ever having modified it. Isn't that amazing?

License

The MIT License (MIT)

Copyright (c) 2015 Daniel Sloof

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.