Skip to content

Instantly share code, notes, and snippets.

@juzna
juzna / example.php
Created June 25, 2011 15:59
Lazy-loading for PHP
<?php
class Address {
public $street;
public $town;
}
class Customer {
public $id;
public $name;
@juzna
juzna / flow.md
Last active March 14, 2016 17:28
Cooperative Multitasking Components in Nette Framework example

Cooperative Multitasking, Components, Nette & Flow

On GitHub I provide example or Cooperative Multitasking components on a single site served by Nette Framework. These components need to process several network requests to render themselves, which is normally slow.

This example takes advantage of yield operator (available since PHP 5.5) to switch between tasks, Flow as scheduler and Rect as parallel http client.

This post introduces the Flow framework and cooperative multitasking in general.

The Example Application

@juzna
juzna / db-schema-dump.php
Last active December 17, 2015 08:19
automatic db schema dump via Adminer, possibly on commit
<?php
/**
* Dump database schema
*
* Better than mysqldump, because of the nice output :)
* You can run this from CLI, it will output the db schema.
* Consider adding it as pre-commit hook.
*
* Example:
* php tools/db-schema-dump.php > db/schema.sql
@juzna
juzna / .phpstorm.meta.php
Last active December 17, 2015 03:09
intellij-neon autocompletion mapping ideas
<?php
/**
* Inspired by Factory methods (http://confluence.jetbrains.com/display/PhpStorm/PhpStorm+Advanced+Metadata)
* which are now built in PhpStorm.
*
* We use meta-PHP code as a config - so we can use existing API to analyse it.
* The specific format is chosen also to facilitate existing editor features to help as much as possible
* - completion works in places when you enter class references
* - references are resolved and thus clickable
* - usage search and refactoring will work too
#!/bin/sh
# download
wget http://download.elasticsearch.org/elasticsearch/elasticsearch/elasticsearch-0.20.4.zip
# unzip and start
unzip elasticsearch-0.20.4.zip
cd elasticsearch-0.20.4
# remove data in case you have defined some analyzers in the past (e.g. stop/start)
rm -rf data/
@juzna
juzna / 2012-11-17-intellij-plugins-lexer-tests.md
Created November 17, 2012 19:51
IntelliJ plugins - Lexer + Tests

IntelliJ plugins - Lexer + Tests

We're writing support for Neon language into PhpStorm and we want to have it heavily tested with automated unit tests. First step when processing any programming language is a lexical analyzer or shortly lexer, which takes source code and splits into individual words of the programming language, which are called tokens (actually, also symbols, punctuation and whitespace count as tokens).

JFlex

Because parsing strings manually is tedious and boring, clever people made tools to help us a little bit. Flex is de facto standard language for writing lexers, but we'll use its port to Java called JFlex. In flex files you describe patterns for several types of tokens and associate a piece of code with each. Have a look at flex file for very simple [properties](https://github.com/JetBrains/intellij-community/blob/master/plugins/properties/src/com/intellij/lang/properties/pa

@juzna
juzna / retina.js
Created October 12, 2012 22:47
Retina images quality simulator
/**
* Simulates how normal low-res images look on retina display
*
* It resamples all images (within <img> tags) to a lower resolution.
* We should use half the resolution of original image to be precise (because retina has 2x the density of pixels),
* but that doesn't show * perceived* effect. It's enough to scale by 1.75.
*
* Also, it would be good to resample also images used in CSS background, but I dunno how to do it :/
*
* Requires jQuery
@juzna
juzna / utils.php
Created October 2, 2012 17:06
unserialize() with encoding conversion
<?php
/**
* Hacked unserialize: works when encoding changed after serialization
* Problem: serialize cp1250, convert to utf8, unserialize. Length in bytes is bigger now!
*
* Also, it throws when something else is wrong
*
* @param string $s
@juzna
juzna / tst.php
Created September 3, 2012 21:56
PHP WTF? Error handler is not called, when display_errors=On
<?php
/**
* WTF? Error handler is not called, when display_errors=On
*/
function _errorHandler($severity, $message, $file, $line, $context) {
$GLOBALS['err'] = func_get_args();
// echo "Error ($severity) $message in $file:$line\n";
}
@juzna
juzna / php-enhancer.md
Created August 23, 2012 20:54
PHP Enhancer - Enhance PHP syntax now

PHP Enhancer

Enhance PHP syntax now.

Motivation

Is there something you don't like about PHP's syntax? Would you like to upgrade it a little bit, if it wouldn't be that difficult? PHP itself is written in C, so if you wanna change something, you'd have to learn C, right?

"No, thanks. I don't wanna learn C and spend long nights debugging memory leaks etc. I wanna just try something out and then probably discard it anyway. Just play around, experiment..."