Leitgedanken/msd2/phpBB3/vendor/ocramius/proxy-manager/html-docs/access-interceptor-value-holder-proxy.html
2023-01-23 11:03:31 +01:00

209 lines
8.1 KiB
HTML

<!DOCTYPE html>
<html class="no-js" id="top">
<head>
<title>ProxyManager - Tuning the ProxyManager for production</title>
<meta name="description" content="A proxyManager write in php" />
<meta name="keywords" content="ProxyManager, proxy, manager, ocramius, Marco Pivetta, php, production" />
<meta name="viewport" content="width=device-width, initial-scale=1, minimum-scale=1" />
<link href='http://fonts.googleapis.com/css?family=Source+Sans+Pro:200,300,400,600' rel='stylesheet' type='text/css'>
<link href="css/styles.css" rel="stylesheet" />
<link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/highlight.js/8.3/styles/default.min.css">
<script src="//cdnjs.cloudflare.com/ajax/libs/highlight.js/8.3/highlight.min.js"></script>
<script>hljs.initHighlightingOnLoad();</script>
<link rel="shortcut icon" href="favicon.ico">
</head>
<body>
<header class="site-header">
<div class="container">
<h1><a href="index.html"><img alt="ProxyManager" src="img/block.png" /></a></h1>
<nav class="main-nav" role="navigation">
<ul>
<li><a href="https://github.com/Ocramius/ProxyManager" target="_blank">Github</a>
<div class="bcms-clearfix"></div>
</li>
</ul>
</nav>
</div>
</header>
<main role="main">
<section class="component-content">
<div class="component-demo" id="live-demo">
<div class="container">
<div class="main-wrapper" style="text-align: right">
<iframe src="http://ghbtns.com/github-btn.html?user=ocramius&amp;repo=ProxyManager&amp;type=fork&amp;count=true&amp;size=large"
allowtransparency="true" frameborder="0" scrolling="0" width="310" height="40"></iframe>
<iframe src="http://ghbtns.com/github-btn.html?user=ocramius&amp;repo=ProxyManager&amp;type=watch&amp;count=true&amp;size=large"
allowtransparency="true" frameborder="0" scrolling="0" width="200" height="40"></iframe>
</div>
<div class="bcms-clearfix bcms-clearfix"></div>
</div>
</div>
<div class="component-info">
<div class="container">
<aside class="sidebar">
<nav class="spy-nav">
<ul>
<li><a href="index.html">Intro</a></li>
<li><a href="virtual-proxy.html">Virtual Proxy</a></li>
<li><a href="null-object.html">Null Objects</a></li>
<li><a href="ghost-object.html">Ghost Objects</a></li>
<li><a href="remote-object.html">Remote Object</a></li>
<li><a href="contributing.html">Contributing</a></li>
<li><a href="credits.html">Credits</a></li>
<li><a href="copyright.html">Copyright</a></li>
</ul>
</nav>
<div class="bcms-clearfix bcms-clearfix"></div>
<a class="btn btn-action btn-full download-component"
href="download.html">Download</a>
<div class="bcms-clearfix"></div>
</aside>
<div class="content">
<div class="bcms-clearfix"></div>
<h3 class="section-title">Access Interceptor Value Holder Proxy</h3>
<p>An access interceptor value holder is a smart reference proxy that allows you to dynamically define logic to be executed before or after any of the wrapped object's methods logic.</p>
<p>It wraps around a real instance of the object to be proxied, and can be useful for things like:</p>
<ul>
<li>caching execution of slow and heavy methods</li>
<li>log method calls</li>
<li>debugging</li>
<li>event triggering</li>
<li>handling of orthogonal logic, and <a href="http://en.wikipedia.org/wiki/Aspect-oriented_programming" target="_blank">AOP</a> in general</li>
</ul>
<hr />
<h3>Example</h3>
<p>Here's an example of how you can create and use an access interceptor value holder:</p>
<pre>
<code class="php">
&lt;?php
use ProxyManager\Factory\AccessInterceptorValueHolderFactory as Factory;
require_once __DIR__ . '/vendor/autoload.php';
class Foo
{
public function doFoo()
{
echo "Foo!\n";
}
}
$factory = new Factory();
$proxy = $factory->createProxy(
new Foo(),
array('doFoo' => function () { echo "PreFoo!\n"; }),
array('doFoo' => function () { echo "PostFoo!\n"; })
);
$proxy->doFoo();
</code>
</pre>
<p>This send something like following to your output:</p>
<pre>
<code class="php">
PreFoo!
Foo!
PostFoo!
</code>
</pre>
<hr />
<h3>Implementing pre- and post- access interceptors</h3>
<p>A proxy produced by the <code><a href="https://github.com/Ocramius/ProxyManager/blob/master/src/ProxyManager/Factory/AccessInterceptorValueHolderFactory.php" target="_blank">ProxyManager\Factory\AccessInterceptorValueHolderFactory</a></code> implements both the <code><a href="https://github.com/Ocramius/ProxyManager/blob/master/src/ProxyManager/Proxy/ValueHolderInterface.php" target="_blank">ProxyManager\Proxy\ValueHolderInterface</a></code> and the <code><a href="https://github.com/Ocramius/ProxyManager/blob/master/src/ProxyManager/Proxy/ValueHolderInterface.php" target="_blank">ProxyManager\Proxy\AccessInterceptorInterface</a></code>.</p>
<p>Therefore, you can set an access interceptor callback by calling:</p>
<pre>
<code class="php">
$proxy->setMethodPrefixInterceptor('methodName', function () { echo 'pre'; });
$proxy->setMethodSuffixInterceptor('methodName', function () { echo 'post'; });
</code>
</pre>
<p>You can also listen to public properties access by attaching interceptors to <code>__get</code>, <code>__set</code>, <code>__isset</code> and <code>__unset</code>.</p>
<p>A prefix interceptor (executed before method logic) should have following signature:</p>
<pre>
<code class="php">
/**
* @var object $proxy the proxy that intercepted the method call
* @var object $instance the wrapped instance within the proxy
* @var string $method name of the called method
* @var array $params sorted array of parameters passed to the intercepted
* method, indexed by parameter name
* @var bool $returnEarly flag to tell the interceptor proxy to return early, returning
* the interceptor's return value instead of executing the method logic
*
* @return mixed
*/
$prefixInterceptor = function ($proxy, $instance, $method, $params, &amp; $returnEarly) {};
</code>
</pre>
A suffix interceptor (executed after method logic) should have following signature:
<pre>
<code class="php">
/**
* @var object $proxy the proxy that intercepted the method call
* @var object $instance the wrapped instance within the proxy
* @var string $method name of the called method
* @var array $params sorted array of parameters passed to the intercepted
* method, indexed by parameter name
* @var mixed $returnValue the return value of the intercepted method
* @var bool $returnEarly flag to tell the proxy to return early, returning the interceptor's
* return value instead of the value produced by the method
*
* @return mixed
*/
$suffixInterceptor = function ($proxy, $instance, $method, $params, $returnValue, & $returnEarly) {};
</code>
</pre>
<hr />
<h3>Tuning performance for production</h3>
<p>See <a href="production.html">Tuning ProxyManager for Production</a>.</p>
</main>
<footer class="site-footer" role="contentinfo">
<div class="container">
<div class="footer-logos">
<ul>
<li><a href="index.html">Intro</a> | </li>
<li><a href="virtual-proxy.html">Virtual Proxy</a> | </li>
<li><a href="null-object.html">Null Objects</a> | </li>
<li><a href="ghost-object.html">Ghost Objects</a> | </li>
<li><a href="remote-object.html">Remote Object</a> | </li>
<li><a href="contributing.html">Contributing</a> | </li>
<li><a href="credits.html">Credits</a> | </li>
<li><a href="copyright.html">Copyright</a></li>
</ul>
</div>
</div>
<div class="bcms-clearfix"></div>
</footer>
<div class="bcms-clearfix"></div>
</body>
</html>