wordpress-like model to view api event system (MVC) - php

Wordpress has a nice api system. I'm struggling in creating an equal flexible one using a more traditional MVC interpretation. A typical view class could be like this:
class View
{
public function set($name, $value)
{
$this->data[$name] = $value
}
public function __get($name) ... you know how it works
}
A use case would be a controller adding an order contain order lines:
$view->add('order', $orderModel);
Then the template file could have something like
foreach ($this->order->getOrderLines() as $orderLine)
{
?><div><?php echo $orderLine->getItemName(); ?><div><?php
}
At least this is what is commonly seen in many PHP MVC frameworks. I'm open to alternative implementations that solve the question:
How to add an event system like wordpress has? Eg a filter OrderLineItemName.

Okay,
I am not 100% exactly what you want to do, but I have an idea.
Maybe you mean something like this:
class View {
public $hooks = array('getsomeVar');
public $hooks_functions = array();
public $attributes = array();
public function __set($k,$v) {
$this->attributes[$k] = $v;
}
public function __get($k) {
if (isset($this->attributes[$k])){
$hooks = $this->get_functions_by_hook('get' . $k);
if (!empty($hooks)){
foreach ($hooks as $klass=>$methods) {
if (class_exists($klass)){
$class = new $klass();
foreach ($methods as $method) {
if (method_exists($class,$method)){
$this->attributes[$k] = $class->$method($this->attributes[$k]);
}
}
}
}
}
return $this->attributes[$k];
} else {
throw new Exception($k . " is not a view variable");
}
}
public function register_filter($name,$class,$method) {
if (in_array($name,$this->hooks)){
$this->hooks_functions[$name][$class][] = $method;
} else {
throw new Exception($name . ' is not a valid hook');
}
}
public function get_functions_by_hook($name) {
if (array_key_exists($name,$this->hooks_functions)){
return $this->hooks_functions[$name];
}
return array();
}
}
class MyPlugin {
public function fix_string($str) {
return str_replace("ruby",'php',$str);
}
}
$v = new View();
$v->someVar = 'ruby is great';
$v->register_filter('getsomeVar','MyPlugin','fix_string');
echo $v->someVar;
or you can use this method, which is more event like. Again sample code, but you should be able to cannabalize it.
class EventDispatcher {
public static $listeners = array();
public static function registerListener(&$instance) {
if (!in_array($isntance,self::$listeners)){
array_push(self::$listeners,$instance);
}
}
public static function dispatchEvent($name,&$value) {
foreach (self::$listeners as $listener) {
if (method_exists($listener,'interests')){
$funcs = $listener->interests();
if (array_key_exists($name,$funcs)){
foreach ($funcs as $f) {
$value = $listener->$f($value);
}
}
}
}
}
}
class Plugin {
public static function registerPlugin($class_name) {
if (class_exists($class_name)){
EventDispatcher::registerListener(new $class_name());
}
}
}
class Model {
public function __construct() {
EventDispatcher::registerListener($this);
}
public function special($value) {
echo "I got called too!\n\n";
return $value;
}
public function interests() {
return array(
"getsomeVar" => "special",
);
}
}
class View {
public $attributes = array();
public function __set($k,$v) {
$this->attributes[$k] = $v;
}
public function __get($k) {
if (isset($this->attributes[$k])){
EventDispatcher::dispatchEvent('get' . $k,$this->attributes[$k]);
return $this->attributes[$k];
} else {
throw new Exception($k . " is not a view variable");
}
}
}
class MyPlugin {
public function fix_string($str) {
return str_replace("ruby",'php',$str);
}
public function interests() {
return array(
"getsomeVar" => "fix_string",
);
}
}
Plugin::registerPlugin('MyPlugin');
$model = new Model();
$v = new View();
$v->someVar = 'ruby is great';
echo $v->someVar;
This is just some sample code, I don't do it this way at all, but it seems like it may be what you are talking about.
Cheers,
Jason
Addendum:
Most of this stuff is about the WP codebase.
WP accesses variables set in the global scope that are modified like so:
function add_filter($tag, $function_to_add, $priority = 10, $accepted_args = 1) {
global $wp_filter, $merged_filters;
$idx = _wp_filter_build_unique_id($tag, $function_to_add, $priority);
$wp_filter[$tag][$priority][$idx] = array('function' => $function_to_add, 'accepted_args' => $accepted_args);
unset( $merged_filters[ $tag ] );
return true;
}
It has some other functions like, has_filter etc...
function apply_filters($tag, $value) {
global $wp_filter, $merged_filters, $wp_current_filter;
$args = array();
$wp_current_filter[] = $tag;
// Do 'all' actions first
if ( isset($wp_filter['all']) ) {
$args = func_get_args();
_wp_call_all_hook($args);
}
if ( !isset($wp_filter[$tag]) ) {
array_pop($wp_current_filter);
return $value;
}
// Sort
if ( !isset( $merged_filters[ $tag ] ) ) {
ksort($wp_filter[$tag]);
$merged_filters[ $tag ] = true;
}
reset( $wp_filter[ $tag ] );
if ( empty($args) )
$args = func_get_args();
do {
foreach( (array) current($wp_filter[$tag]) as $the_ )
if ( !is_null($the_['function']) ){
$args[1] = $value;
$value = call_user_func_array($the_['function'], array_slice($args, 1, (int) $the_['accepted_args']));
}
} while ( next($wp_filter[$tag]) !== false );
array_pop( $wp_current_filter );
return $value;
}
This is not an event driven system, it's a method lookup table which is just a giant hash that looks up the user defined functions to call.
apply_filters, and all plugin like functions are called procedurally as the code is rendered, here is an example
if ( $prefixed ) {
$value = apply_filters("pre_$field", $value);
$value = apply_filters("${field_no_prefix}_save_pre", $value);
} else {
$value = apply_filters("pre_post_$field", $value);
$value = apply_filters("${field}_pre", $value);
}
Or for actions, in an actual template view like so:
<p class="submit"><input type="submit" class="button" name="submit" value="<?php esc_attr_e('Add Category'); ?>" /></p>
<?php do_action('edit_link_category_form', $category); ?>
</form>

Related

Call a method inside an object that's inside a php class

I need to call a function that's inside an object, inside a class. Of course, for "On The Fly" class methods I'm able to call it using using __call & __set magics but not on this case. Below is the example for this situation.
class mainclass
{
public $v1 = "Hello";
public $fn = null;
function __construct( )
{
$this->fn = (object) [ "fn1" => null,
"fn2" => null,
];
}
public function __call( $name, array $args )
{
return call_user_func_array( $this->$name, $args );
}
public function fn3()
{
echo "This of course works! <br />";
}
}
$main = new mainclass();
$main->fn4 = function()
{
echo "Even this works! <br />";
};
$main->fn->fn1 = function()
{
echo $this->v1 . " World :)";
};
$main->fn3(); // This of course works!
$main->fn4(); // Even this works!
$main->fn->fn1(); //Call to undefined method stdClass::fn1()
There is a possibility to call the function "f1" this way: $main->fn->fn1() ?
If not, any suggestion without drastic changes?
Unfortunately this is not JavaScript and don't like the way is handled this class but I have to give it a try
The only and easy workaround I have for this case is to change the object in anonymous class. During this process you have to store the scope of main class on the internal anonymous class(es) using a similar variable name, "$_this".
class mainclass
{
public $v1 = "Hello";
public $fn = null;
function __construct( )
{
$this->fn = new class( $this)
{
public $_this = null;
public function __construct( $mainscope )
{
$this->_this = &$mainscope;
}
public function __call( $method, array $args )
{
if ( isset( $this->{ $method } ) )
{
return call_user_func_array( $this->$method, $args );
}
elseif ( isset( $this->_this->{ $name } ) )
{
return call_user_func_array( $this->_this->{ $name }, $args);
}
}
public function __set( $name, $value )
{
$this->{ $name } = is_callable( $value ) ? $value->bindTo( $this, $this ) : $value;
}
};
}
public function __call( $method, array $args )
{
return call_user_func_array( $this->{ $method }, $args );
}
public function __set( $name, $value )
{
$this->{ $name } = is_callable( $value ) ? $value->bindTo( $this, $this ) : $value;
}
public function fn3()
{
echo "This of course works! <br />";
}
}
$main = new mainclass();
$main->fn4 = function()
{
echo "Even this works! <br />";
};
$main->fn->fn1 = function()
{
echo $this->_this->v1 . " World :)";
};
$main->fn3(); // This of course works!
$main->fn4(); // Even this works!
$main->fn->fn1(); //Hello World :)
It turns out not very ugly and also manageable. Anyway this is the only option for now.
($main->fn->fn1)(); should be working. However, you can't access $this in the anonymous function
$main->fn->fn1();
fn1 is an attribute try using $main->fn.

Cannot use [] for reading when including

I have this PHP file:
<?php
namespace FrameWork\Controller;
abstract class ControllerBase
{
protected $action;
protected $vars;
public function __construct($action, $vars = NULL)
{
$this->action = $action;
$this->vars = $vars;
$this->populateVars();
}
public function run()
{
****BETWEEN HERE****
$r = new \ReflectionMethod($this, $this->action);
$params = $r->getParameters();
$funcParams[];
foreach($params as $param)
{
$paramName = $param->getName();
$funcParams[$paramName] = $this->vars[$paramName];
}
****AND HERE****
call_user_func_array(array($this, $this->action), $funcParams);
}
private function PopulateVars()
{
foreach($_GET as $key => $getVar)
{
$this->vars[$key] = $getVar;
}
foreach($_POST as $key => $postVar)
{
$this->vars[$key] = $postVar;
}
}
}
It is includeed in another file, and for some reason I am getting an exception thrown on the include.
Cannot use [] for reading
When I remove everything between ****BETWEEN HERE**** and ****AND HERE****, it works (or at least doesn't throw the same exception).
Any idea what's going on?
Have you tried replacing
$funcParams[];
with
$funcParams = array();
I think it will solve your problem.

How to use SplObserver for Hook system?

I code a class for Hook system. But this is outdated. I want to use splObserver to code it.
<?php
class Event
{
private static $filters = [];
private static $actions = [];
public static function addAction($name, $callback, $priority = 10)
{
if (! isset(static::$actions[$name])) {
static::$actions[$name] = [];
}
static::$actions[$name][] = [
'priority' => (int)$priority,
'callback' => $callback,
];
}
public function doAction($name, ...$args)
{
$actions = isset(static::$actions[$name]) ? static::$actions[$name] : false;
if (! $actions) {
return;
}
// sort actions by priority
$sortArr = array_map(function ($action) {
return $action['priority'];
}, $actions);
\array_multisort($sortArr, $actions);
foreach ($actions as $action) {
\call_user_func_array($action['callback'], $args);
}
}
}
Event::addAction('action1', function(){
echo 'balabala1';
});
Event::addAction('action1', function(){
echo 'balabala2';
});
Event::doAction('action1');
Output: balabala1 balabala2
It works good.
I want to use SplObserver to re-code it and try to code but no idea.
I don't really know whether this implementation could be useful in a real life application or not but, for the sake of answering your question, here we go...
Let's imagine we have a User class that we'd like to hook with our custom functions.
First, we create a reusable trait containing the Subject logic, capable of managing "event names" to whom we can hook our actions.
trait SubjectTrait {
private $observers = [];
// this is not a real __construct() (we will call it later)
public function construct()
{
$this->observers["all"] = [];
}
private function initObserversGroup(string $name = "all")
{
if (!isset($this->observers[$name])) {
$this->observers[$name] = [];
}
}
private function getObservers(string $name = "all")
{
$this->initObserversGroup($name);
$group = $this->observers[$name];
$all = $this->observers["all"];
return array_merge($group, $all);
}
public function attach(\SplObserver $observer, string $name = "all")
{
$this->initObserversGroup($name);
$this->observers[$name][] = $observer;
}
public function detach(\SplObserver $observer, string $name = "all")
{
foreach ($this->getObservers($name) as $key => $o) {
if ($o === $observer) {
unset($this->observers[$name][$key]);
}
}
}
public function notify(string $name = "all", $data = null)
{
foreach ($this->getObservers($name) as $observer) {
$observer->update($this, $name, $data);
}
}
}
Next, we use the trait in our SplSubject User class:
class User implements \SplSubject
{
// It's necessary to alias construct() because it
// would conflict with other methods.
use SubjectTrait {
SubjectTrait::construct as protected constructSubject;
}
public function __construct()
{
$this->constructSubject();
}
public function create()
{
// User creation code...
$this->notify("User:created");
}
public function update()
{
// User update code...
$this->notify("User:updated");
}
public function delete()
{
// User deletion code...
$this->notify("User:deleted");
}
}
The last step is to implement a reusable SplObserver. This observer is able to bind himself to a Closure (anonymous function).
class MyObserver implements SplObserver
{
protected $closure;
public function __construct(Closure $closure)
{
$this->closure = $closure->bindTo($this, $this);
}
public function update(SplSubject $subject, $name = null, $data = null)
{
$closure = $this->closure;
$closure($subject, $name, $data);
}
}
Now, the test:
$user = new User;
// our custom functions (Closures)
$function1 = function(SplSubject $subject, $name, $data) {
echo $name . ": function1\n"; // we could also use $data here
};
$function2 = function(SplSubject $subject, $name, $data) {
echo $name . ": function2\n";
};
// subscribe the first function to all events
$user->attach(new MyObserver($function1), 'all');
// subscribe the second function to user creations only
$user->attach(new MyObserver($function2), 'User:created');
// run a couple of methods to see what happens
$user->create();
$user->update();
The output will be:
User:created: function2
User:created: function1
User:updated: function1
NOTE: we could use SplObjectStorage instead of an array, to store observers in the trait.

Uncertain number objects owned by another object

I have a Project class/object that needs to have (own) an uncertain number of Phase objects.
I don't know the number of phases the project object will have when it is created, so I didn't want to put Phase object creation in the constructor function of Project.
My classes:
class Project {
//some properties
}
class Phase {
public $property;
}
And I'd like to do this:
$foo = $myProject->phase01->property;
$bar = $myProject->phase06->property;
//etc...
I wouldn't use dynamic properties.
If the phases are a collection, would treat them as such, it could come handy later on. E.g.:
class Project {
private $phases = [];
public function __get($property)
{
// if begins with "phase" and some number
if ( preg_match("/^phase(\d+)$/", $property, $matches) ) {
// if is set already, we return it
if ( isset($this->phases[$matches[1]]) ) {
return $this->phases[$matches[1]];
}
// if it isn't, it isn't :)
return null;
}
}
public function __set($property, $value)
{
if ( preg_match("/^phase(\d+)$/", $property, $matches) ) {
$this->phases[$matches[1]] = $value;
}
}
public function addPhase(Phase $phase, $phase_number = null)
{
if ($phase_number !== null) {
$this->phases[$phase_number] = $phase;
}
else {
$this->phases[] = $phase;
}
return $this;
}
public function getPhases()
{
return $this->phases;
}
// etc
}
class Phase {
public $property = "";
public function __construct($property) {
$this->property = $property;
}
}
$myProject = new Project();
$myProject->phase1 = new Phase('startup');
$myProject
->addPhase(new Phase('build'))
->addPhase(new Phase('cleanup'));
foreach ($myProject->getPhases() as $key => $phase) {
echo "Phase $key: {$phase->property}", "\n";
}
You could implement one of php's magic methods, in particular __get
<?php
class Project {
//some properties
public function __get($property)
{
// if begins with "phase" and some number
if ( preg_match("/^phase\d+$/", $property) === 1 ) {
if ( !isset($this->$property) ) {
$this->$property = new Phase;
}
return $this->$property;
}
}
}
class Phase {
public $property;
}
$myProject = new Project;
//And I'd like to do this:
$foo = $myProject->phase01->property;
$bar = $myProject->phase06->property;
//etc...

Php: turning it into a recursive function

I have currently two classes.
the ArrayCompare class:
<?php
namespace App\Tools\RegexExtract;
class ArrayCompare
{
public function compare(Array $arrayToCompare)
{
$elementData = new ElementMetaData();
$metaData = $elementData->extract($arrayToCompare[0], [], $initial=true);
foreach ($arrayToCompare as $currentElement) {
$metaData = $elementData->extract($currentElement, $metaData);
}
return $metaData;
}
}
which uses the ElementMetaData class
<?php
/**
* A class for extracting meta data from an element.
*/
namespace App\Tools\RegexExtract;
class ElementMetaData
{
public function extract($element, $metaDataToCompare = [], $initial = false)
{
if ($initial == true) {
$this->isInteger($element) ? $returnMetaData['isInteger'] = $this->isInteger($element) : null;
$returnMetaData['length'] = $this->length($element);
}
else {
$returnMetaData=$metaDataToCompare;
if ($returnMetaData != []) {
if (isset ($returnMetaData['isInteger']) && !$this->isInteger($element)) {
unset($returnMetaData['isInteger']);
}
if (isset ($returnMetaData['length']) && $this->length($element) != $returnMetaData['length']) {
unset($returnMetaData['length']);
}
}
}
return $returnMetaData;
}
private function isInteger($element)
{
return is_int($element);
}
private function length($element)
{
return strlen($element);
}
}
the basic functionality is:
given I have an array
$arr=[1,2,3];
I want to get the "similarities" between ALL Elements. According to a an array i Predefine...so this would deliver this result:
$metaArray=['isInteger'=>true,'length'=>1];
and this would deliver just length as similarity:
$arr=[1,2,'D'];
$metaArray=['length'=>1];
While this array would deliver an empty result []
$arr=[1,2,'3D']; // result is [] since not all integers or not all of same length.
Now my solution does not use recursive functions...but I am sure it can be used somehow.
Also, I want to add more "criteria"....So "isEmailAdress", "beginswithA"....etc....and this would make my if statements a horror....so what is the best strategy/design pattern to follow here?
#deceze beat me to it by fair margin... but I'll still post my solution that works basically with the same principles.
abstract class abstractComparer
{
private $array;
private $result = true;
protected $name;
public function compareArray($array)
{
$current = null;
foreach ($array as $index => $value)
{
$this->result = $this->result && $this->compareValues($index, $current, $value);
$current = $value;
}
}
public function getResult()
{
return $this->result;
}
public function getName()
{
return $this->name;
}
public abstract function compareValues($index, $value1, $value2);
public abstract function getSuccessValue();
}
class intComparer extends abstractComparer
{
protected $name = "isInteger";
public function compareValues($index, $value1, $value2)
{
return is_int($value2);
}
public function getSuccessValue()
{
return true;
}
}
class lengthComparer extends abstractComparer
{
protected $name = "length";
protected $length = 0;
public function compareValues($index, $value1, $value2)
{
$this->length = strlen($value2);
return $index == 0 || strlen($value1) == $this->length;
}
public function getSuccessValue()
{
return $this->length;
}
}
And do the actual processing like this:
$temp = [1,2,3];
$comparers = [new intComparer(), new lengthComparer()];
$result = array();
foreach ($comparers as $comparer)
{
$comparer->compareArray($temp);
if ($comparer->getResult())
{
$result[$comparer->getName()] = $comparer->getSuccessValue();
}
}
//var_dump($result);
I don't see any need for recursion here, so I'll just make a suggestion for a design approach:
Implement each criterion as a class:
abstract class Criterion {
protected $valid = true;
abstract public function initialize($value);
abstract public function check($value);
public function isValid() {
return $this->valid;
}
}
class Length extends Criterion {
protected $length;
public function initialize($value) {
$this->length = strlen($value);
}
public function check($value) {
if ($this->length != strlen($value)) {
$this->valid = false;
}
}
}
You then make an array of all your criteria:
$criteria = [new Length, ...];
foreach ($criteria as $criterion) {
$criterion->initialize($values[0]);
}
And slowly whittle them down through your values:
foreach ($values as $value) {
foreach ($criteria as $criterion) {
$criterion->check($value);
}
}
$commonCriteria = array_filter($criteria, function (Criterion $criterion) {
return $criterion->isValid();
});

Categories