Skip to content

Instantly share code, notes, and snippets.

View SamMousa's full-sized avatar
🎯
Focusing

Sam SamMousa

🎯
Focusing
  • Sam-IT
  • Netherlands
View GitHub Profile
<?xml version="1.0"?>
<catalog>
<book id="bk101">
<author>Gambardella, Matthew</author>
<title>XML Developer's Guide</title>
<genre>Computer</genre>
<price>44.95</price>
<publish_date>2000-10-01</publish_date>
<description>An in-depth look at creating applications
with XML.</description>
@SamMousa
SamMousa / NotificationService.php
Last active August 19, 2019 12:12
NotificationService that can be easily decoupled from the session
<?php
namespace ;
use bundles\PNotifyBundle;
use yii\authclient\AuthAction;
use yii\base\Component;
use yii\base\Event;
@SamMousa
SamMousa / InfoRecordSet.php
Created April 15, 2019 11:28
Multiple response sets
<?php
class InfoRecordSet extends \ArrayObject
{
public function getIterator()
{
$this->ksort();
return parent::getIterator();
}
@SamMousa
SamMousa / benchmark.php
Last active April 4, 2019 09:49
Benchmarks 2 approaches of reflection
<?php
$precision = 'ms';
$inner = 50000;
$outer = 50;
class Provider
{
/**
* Derives the interface type of the first argument of a callable.
@SamMousa
SamMousa / context-switching.md
Created March 15, 2019 12:52
Scoping containers: context switching

Scoping containers: context switching

For Yii3 we have been working to split the framework up into different parts that can be used separately. One of the parts I've been working on a lot is the DI implementation. Traditionally Yii uses arrays for configuration, often these arrays are defined in multiple files and recursively merged to obtain the final configuration. The goal of this DI implementation is to use DI containers to configure different parts of an application differently.

Injecting the DI container

One of the first things they tell you about DI is that if you're injecting a container you're doing it wrong. While this is a good rule to live by, exceptions exist. Specifically parts of your application that decide the execution path will often use a DI container as a service locator to instantiate the request handler and inject its dependencies.

In Yii this routing happens in modules. A module parses a request and routes it to an action. This action has dependencies which are injected by the

@SamMousa
SamMousa / break-a-worker.php
Last active February 22, 2018 13:48
Breaking a PHPFPM worker...
<?php
if ($_SERVER['testcase'] === 'test') {
echo 'Everything is fine; trying to break your env...';
foreach($_SERVER as $key => &$value) {
putenv("$key=$value");
}
} else {
phpinfo(INFO_ENVIRONMENT);
}
@SamMousa
SamMousa / android_call.sh
Created February 2, 2018 08:28
Call via android phone from linux
#!/bin/bash
if [ ! -z "$1" ]; then
NUMBER=`zenity --entry --text "Enter phone number" --entry-text ${1:4}`
else
NUMBER=`zenity --entry --text "Enter phone number"`
fi
if [ $? -eq 0 ]
then
beep -f 600 -l 400 -r 2 &
@SamMousa
SamMousa / eh.php
Last active January 12, 2018 07:50
<?php
class EventHandler.php {
private $handlerClass;
public function __construct(string $handlerClass) {
$this->handlerClass = $handlerClass
}
public function __invoke(... $args) {
$instance = new {$this->handlerClass};
$instance->handle(... $args);
class EventHandler.php {
private $handlerClass;
public function __construct(string $handlerClass) {
$this->handlerClass = $handlerClass
}
public function __invoke(... $args) {
$instance = new {$this->handlerClass};
$instance->handle(... $args);
}
<?php
namespace SamIT\Yii2\Behaviors;
use yii\base\Behavior;
use yii\base\InvalidConfigException;
use yii\base\View;
use yii\base\ViewEvent;