Skip to content

Instantly share code, notes, and snippets.

@abcarroll
Last active November 8, 2019 23:31
Show Gist options
  • Select an option

  • Save abcarroll/1d764bf64f21715dd3fb895ae4ee1e11 to your computer and use it in GitHub Desktop.

Select an option

Save abcarroll/1d764bf64f21715dd3fb895ae4ee1e11 to your computer and use it in GitHub Desktop.
Nasty Nate's Pipelines
<?php
/*
* So, I have no idea what exactly you could do with this.
* I'm sure it's been done before, as well.
* But, I've never thought about a 'multi-prong' call pipeline ...
* where you have
* the return value
* parameter arg
* the normal __get() magic
* .. PLUS taking over the array offset get
* .. PLUS, in some circumstances also being able to take over
* .. __call() ... In this case, the previous stanza must've
* .. resulted in a string (see BUTTER! below)
*/
$proc = (new Procedure(0))
['hey']->there("peanut")->butter->how("you")->{"doing"}('!')
->Im->Doing->Fine->Im['doing']->JustFreaking("Great!")
->I->Said
->{'HEY!'}->There['Peanut']['Butter!']("HOW ARE YOU DOING!?"); // last () wouldn't normally be possible
// -- Results in --
/*
Procedure::offsetGet: 0 (Offset: hey)
Procedure::__call: 0 (Name: there, Value: peanut)
Procedure::__get: 0 (Name: butter)
Procedure::__call: 1 (Name: how, Value: you)
Procedure::__call: 2 (Name: doing, Value: !)
Procedure::__get: 3 (Name: Im)
Procedure::__get: 4 (Name: Doing)
Procedure::__get: 5 (Name: Fine)
Procedure::__get: 6 (Name: Im)
Procedure::offsetGet: 7 (Offset: doing)
Procedure::__call: 7 (Name: JustFreaking, Value: Great!)
Procedure::__get: 8 (Name: I)
Procedure::__get: 9 (Name: Said)
Procedure::__get: 10 (Name: HEY!)
Procedure::__get: 11 (Name: There)
Procedure::offsetGet: 12 (Offset: Peanut)
Procedure::offsetGet: 12 (Offset: Butter!)
butter-magic invoked!
args
array(1) {
[0]=>
string(19) "HOW ARE YOU DOING!?"
}
*/
<?php
class Procedure implements \ArrayAccess
{
/**
* This has no need to use statics except an easy method of tracking where we are.
*
* @var array
*/
static $bigGlobalVariableToShowThatThisMightBeBarelyUseful = [];
/**
* This has no need to use statics except an easy method of tracking where we are.
* @var int
*/
static private $incr = 0;
/**
* @var int
*/
private $generation = 0;
public function __construct(int $generation = 0)
{
$this->generation = $generation;
}
public function __call($name, $arguments)
{
echo __METHOD__ . ": " . $this->generation . " (Name: $name, Value: ";
echo (is_scalar($arguments[0]) ? $arguments[0] : 't: ' . gettype($arguments[0])) . ")\n";
self::$bigGlobalVariableToShowThatThisMightBeBarelyUseful[] = ['__set', $name, $arguments];
return new Procedure(self::$incr++);
}
public static function __callStatic($name, $arguments)
{
echo __METHOD__ . ": " . self::$incr . " (Name: $name, Value: ";
echo (is_scalar($arguments[0]) ? $arguments[0] : 't: ' . gettype($arguments)) . ")\n";
self::$bigGlobalVariableToShowThatThisMightBeBarelyUseful[] = ['__set', $name, $arguments];
return new Procedure(self::$incr++);
}
public function __get($name)
{
echo __METHOD__ . ": " . $this->generation . " (Name: $name)\n";
self::$bigGlobalVariableToShowThatThisMightBeBarelyUseful[] = ['__get', $name];
return new Procedure(self::$incr++);
}
public function __set($name, $value)
{
echo __METHOD__ . ": " . $this->generation . " (Name: $name, Value: ";
echo (is_scalar($value) ? $value : 't: ' . gettype($value)) . ")\n";
self::$bigGlobalVariableToShowThatThisMightBeBarelyUseful[] = ['__set', $name, $value];
return $this->checkIfSendSomeSpecialSignalForParensNext($name);
}
private function checkIfSendSomeSpecialSignalForParensNext($val)
{
if (stripos($val, 'butter') !== false) {
return function(...$args) {
echo "butter-magic invoked!\n";
echo "args\n";
var_dump($args);
return 'butter';
};
} else {
return $this;
}
}
public function offsetExists($offset)
{
return true;
}
public function offsetGet($offset)
{
echo __METHOD__ . ": " . $this->generation . " (Offset: $offset)\n";
return $this->checkIfSendSomeSpecialSignalForParensNext($offset);
}
public function offsetSet($offset, $value)
{
$offset = $this;
$value = $this;
}
public function offsetUnset($offset)
{
// ?
}
/**
* Only occurs if we use () right after the object new
*/
public function __invoke(...$args)
{
echo __METHOD__ . ": " . $this->generation . " ";
echo "(Value: " . (is_scalar($args) ? $args : 't: ' . gettype($args)) . ")\n";
}
public function __toString()
{
return "generation" . self::$incr;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment