Skip to content

Instantly share code, notes, and snippets.

View J7mbo's full-sized avatar
🔨
Working with distributed systems

James Mallison J7mbo

🔨
Working with distributed systems
View GitHub Profile
<?php
namespace Seedstream;
use React\EventLoop\LoopInterface,
React\Socket\ConnectionInterface;
class HandlerManager
{
private $loop;
@J7mbo
J7mbo / gist:8281652
Created January 6, 2014 11:42
PHP JSON Validator Class
namespace Seedstream\Websocket\Handler\Request\Validator;
/**
* {@inheritdoc}
*/
class JSONValidator implements ValidatorInterface
{
/**
* {@inheritdoc}
*/
@J7mbo
J7mbo / gist:8317804
Created January 8, 2014 14:41
Why no indented ifs?
/**
* {@inheritdoc}
*
* @note Image dimensions returned as array(width, height)
*/
public function fetchMetaDataFrom($filePath)
{
if (file_exists($filePath))
{
if ($metadata = @getimagesize($filePath) !== false)
@J7mbo
J7mbo / DigestRequestHandler.php
Last active August 29, 2015 14:04
An example DigestRequestHandler
<?php
namespace SomeEngineName\Api\DigestRequestHandler;
use SomeEngineName\Api\UserRepository\UserNotFoundException,
SomeEngineName\Api\UserRepository\UserRepository;
/**
* Class DigestRequestHandler
*
@J7mbo
J7mbo / gist:0f53e4589d441ee1e772
Created May 9, 2015 12:19
Dependency injection the ---right--- awesome way™

Dependency Injection is a posh word for a simple concept. Frameworks are starting to adopt the pattern and many developers want to use it, but simply don't know how or even why they should. For those who do, are you gaining the advantages that you should be? Should you use a service locator? How does this fit in with S.O.L.I.D? Are you doing it the awesome way?

This talk focuses on the journey I made through the discovery of the basics of DI through to how to use it to it's full potential, the misnomer that is the Dependency Injection Container, and how auto-wiring injectors can make you happy again. Learn how you can 'plug and play' third party libraries with only a few steps and reduce your development time and maintenance when working with object-oriented code.

@J7mbo
J7mbo / nogoto.php
Created August 19, 2015 15:37
Nah don't need that goto...
do {
$className = $this->jigConverter->getNamespacedClassNameFromFileName($templateFilename);
if ($this->jigConfig->compileCheck == Jig::COMPILE_CHECK_EXISTS) {
if (class_exists($className) == true) {
break
}
}
if ($this->jigConfig->compileCheck == Jig::COMPILE_CHECK_MTIME) {
if ($this->isGeneratedFileOutOfDate($templateFilename) == false) {
if (class_exists($className) == true) {
struct JsonDownloader
{
public func downloadContentsOfUrl(url: URL, onComplete: @escaping (_ url: URL) -> UIImage) -> Void {
// This async call will be thrown onto a thread and then this function will return immediately
DispatchQueue.global(qos: .background).async {
let json = try! Data(contentsOf: url)
let jsonData = try! JSONSerialization.jsonObject(with: json, options: []) as! [String: Any]
onComplete(URL(string: jsonData["image_url"] as! String)!)
class ViewController: UIViewController
{
override func viewDidLoad()
{
super.viewDidLoad()
let imageDownloadCallback: (URL) -> UIImage = { url in
return UIImage(data: Data(contentsOf: url))!
}
struct JsonDownloader
{
public func downloadContentsOfUrl(
url: URL,
onUrlRetrieved: @escaping (_ url: URL, (UIImage) -> Void) -> Void,
onImageDownloaded: @escaping (_ image: UIImage) -> Void
) -> Void {
// This async call will be thrown onto a thread and then this function will return immediately
DispatchQueue.global(qos: .background).async {
let json = try! Data(contentsOf: url)
class ViewController: UIViewController
{
override func viewDidLoad()
{
super.viewDidLoad()
let uiUpdatingCallback: (UIImage) -> Void = { image in
// Don't forget, we must do any UI stuff on the main thread
DispatchQueue.main.async {
let imageView = UIImageView(image: image)