Skip to content

Commit

Permalink
Merge pull request #43 from antecedent/redefinition-of-internals
Browse files Browse the repository at this point in the history
Merge redefinition of internals into master
  • Loading branch information
antecedent committed Aug 12, 2016
2 parents 7e71caa + 3b0b72c commit 698b39d
Show file tree
Hide file tree
Showing 27 changed files with 1,122 additions and 249 deletions.
250 changes: 136 additions & 114 deletions Patchwork.php
Original file line number Diff line number Diff line change
@@ -1,114 +1,136 @@
<?php

/**
* @author Ignas Rudaitis <ignas.rudaitis@gmail.com>
* @copyright 2010-2016 Ignas Rudaitis
* @license http://www.opensource.org/licenses/mit-license.html
*/
namespace Patchwork;

require_once __DIR__ . '/src/Exceptions.php';
require_once __DIR__ . '/src/CallRerouting.php';
require_once __DIR__ . '/src/CodeManipulation.php';
require_once __DIR__ . '/src/Utils.php';
require_once __DIR__ . '/src/Stack.php';
require_once __DIR__ . '/src/Config.php';

function redefine($what, callable $asWhat)
{
return CallRerouting\connect($what, $asWhat);
}

function relay(array $args = null)
{
return CallRerouting\relay($args);
}

function fallBack()
{
throw new Exceptions\NoResult;
}

function restore(CallRerouting\Handle $handle)
{
$handle->expire();
}

function restoreAll()
{
CallRerouting\disconnectAll();
}

function silence(CallRerouting\Handle $handle)
{
$handle->silence();
}

function getClass()
{
return Stack\top('class');
}

function getCalledClass()
{
return Stack\topCalledClass();
}

function getFunction()
{
return Stack\top('function');
}

function getMethod()
{
return getClass() . '::' . getFunction();
}

function configure()
{
Config\locate();
}

function hasMissed($callable)
{
return Utils\callableWasMissed($callable);
}

Utils\alias('Patchwork', [
'redefine' => ['replace', 'replaceLater'],
'relay' => 'callOriginal',
'fallBack' => 'pass',
'restore' => 'undo',
'restoreAll' => 'undoAll',
]);

configure();

Utils\markMissedCallables();

if (Utils\runningOnHHVM()) {
# no preprocessor needed on HHVM;
# just let Patchwork become a wrapper for fb_intercept()
spl_autoload_register('Patchwork\CallRerouting\deployQueue');
return;
}

CodeManipulation\Stream::wrap();

CodeManipulation\register([
CodeManipulation\Actions\CodeManipulation\propagateThroughEval(),
CodeManipulation\Actions\CallRerouting\injectCallInterceptionCode(),
CodeManipulation\Actions\CallRerouting\injectQueueDeploymentCode(),
]);

CodeManipulation\onImport([
CodeManipulation\Actions\CallRerouting\markPreprocessedFiles(),
]);

Utils\clearOpcodeCaches();

register_shutdown_function('Patchwork\Utils\clearOpcodeCaches');

if (Utils\wasRunAsConsoleApp()) {
require __DIR__ . '/src/Console.php';
}
<?php

/**
* @author Ignas Rudaitis <ignas.rudaitis@gmail.com>
* @copyright 2010-2016 Ignas Rudaitis
* @license http://www.opensource.org/licenses/mit-license.html
*/
namespace Patchwork;

require_once __DIR__ . '/src/Exceptions.php';
require_once __DIR__ . '/src/CallRerouting.php';
require_once __DIR__ . '/src/CodeManipulation.php';
require_once __DIR__ . '/src/Utils.php';
require_once __DIR__ . '/src/Stack.php';
require_once __DIR__ . '/src/Config.php';

function redefine($subject, callable $content)
{
$handle = null;
foreach (array_slice(func_get_args(), 1) as $content) {
$handle = CallRerouting\connect($subject, $content, $handle);
}
$handle->silence();
return $handle;
}

function relay(array $args = null)
{
return CallRerouting\relay($args);
}

function fallBack()
{
throw new Exceptions\NoResult;
}

function restore(CallRerouting\Handle $handle)
{
$handle->expire();
}

function restoreAll()
{
CallRerouting\disconnectAll();
}

function silence(CallRerouting\Handle $handle)
{
$handle->silence();
}

function assertEventuallyDefined(CallRerouting\Handle $handle)
{
$handle->unsilence();
}

function getClass()
{
return Stack\top('class');
}

function getCalledClass()
{
return Stack\topCalledClass();
}

function getFunction()
{
return Stack\top('function');
}

function getMethod()
{
return getClass() . '::' . getFunction();
}

function configure()
{
Config\locate();
}

function hasMissed($callable)
{
return Utils\callableWasMissed($callable);
}

function always($value)
{
return function() use ($value) {
return $value;
};
}

Utils\alias('Patchwork', [
'redefine' => ['replace', 'replaceLater'],
'relay' => 'callOriginal',
'fallBack' => 'pass',
'restore' => 'undo',
'restoreAll' => 'undoAll',
]);

configure();

Utils\markMissedCallables();

if (Utils\runningOnHHVM()) {
# no preprocessor needed on HHVM;
# just let Patchwork become a wrapper for fb_intercept()
spl_autoload_register('Patchwork\CallRerouting\deployQueue');
return;
}

CodeManipulation\Stream::wrap();

CodeManipulation\register([
CodeManipulation\Actions\CodeManipulation\propagateThroughEval(),
CodeManipulation\Actions\CallRerouting\injectCallInterceptionCode(),
CodeManipulation\Actions\CallRerouting\injectQueueDeploymentCode(),
CodeManipulation\Actions\RedefinitionOfInternals\spliceNamedFunctionCalls(),
CodeManipulation\Actions\RedefinitionOfInternals\spliceDynamicCalls(),
]);

CodeManipulation\onImport([
CodeManipulation\Actions\CallRerouting\markPreprocessedFiles(),
]);

Utils\clearOpcodeCaches();

register_shutdown_function('Patchwork\Utils\clearOpcodeCaches');

CallRerouting\createStubsForInternals();
CallRerouting\connectDefaultInternals();

if (Utils\wasRunAsConsoleApp()) {
require __DIR__ . '/src/Console.php';
}
41 changes: 0 additions & 41 deletions README.md

This file was deleted.

1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"name": "antecedent/patchwork",
"homepage": "http://patchwork2.org/",
"description": "Method redefinition (monkey-patching) functionality for PHP.",
"keywords": ["testing", "redefinition", "runkit", "monkeypatching", "interception", "aop", "aspect"],
"license": "MIT",
Expand Down
Loading

0 comments on commit 698b39d

Please sign in to comment.