Skip to content
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

Intercept function calls from the global namespace #53

Closed
lisachenko opened this issue May 17, 2013 · 2 comments
Closed

Intercept function calls from the global namespace #53

lisachenko opened this issue May 17, 2013 · 2 comments
Labels
Milestone

Comments

@lisachenko
Copy link
Member

According to the official documentation http://www.php.net/manual/en/language.namespaces.rules.php there is a name resolution rule:

Inside namespace (say A\B), calls to unqualified functions are resolved at run-time. Here is how a call to function foo() is resolved:

It looks for a function from the current namespace: A\B\foo().
It tries to find and call the global function foo().

This rule can be used by Go! library to dynamically intercept global function calls in the namespaces. For example, if we want to intercept a function file_get_contents() in the application, we can create this function in the current namespace, so it will be used instead of global function.

@lisachenko
Copy link
Member Author

Example of mocking usleep() function:

use Go\Aop\Framework\FunctionAroundInterceptor;
use Go\Aop\Framework\ReflectionFunctionInvocation;
use Go\Aop\Intercept\FunctionInvocation;


function usleep($microseconds)
{
    $interceptor = new FunctionAroundInterceptor(function (FunctionInvocation $invocation) {
        echo 'Hello!';
        return $invocation->proceed();
    });
    $joinpoint = new ReflectionFunctionInvocation('usleep', array($interceptor));
    return $joinpoint(array($microseconds));
}

lisachenko added a commit that referenced this issue Aug 4, 2013
* feature/function-interception:
  Add default value for flag to fix the tests
  Add flag to the container and to the options to switch on the support for function interception
  Update the demo aspect to use execution pointcut for functions
  Update the parse table and grammar for function executions
  Fix the tests (newline was added)
  Add PointcutParser class to automatically inject parse table and rename the test
  Add in-memory caching of function code to reduce the overhead of building the source code for same function
  Fix an error with composite function pointcuts
  Extend the ReflectionFunction with custom namespace names
  Add new demo for function interception
  Update production parse table for new grammar
  Add support for function interceptors
  Some cleaning and phpDocs
  Support for optional parameters and by reference passing
  Function proxy dirty implementation #53
  Add function around implementation to the framework, needed for #53
  Reflection function invocation class for #53
  Base interfaces for function invocation and interception #53
@lisachenko
Copy link
Member Author

Merged in 28e5e81

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Development

No branches or pull requests

1 participant