Doctrine\Common\Cache\CacheProvider performance-aware adapter
In doctrine/common project, with introduction of cache auto-prefix support (called namespaces by Doctrine guys, but it has nothing to do with PHP namespaces), unfortunately there appeared quite serious performance issue. Every single cache call always resulted in 2 additional cache storage calls due to checks for prefix (cache namespace). Quite a big overhead, imagine your memcached instance is called like this. And it just shows that object-oriented programming itself cannot save you from many headaches. Common interface and clean API are just not everything, and it’s soooo easy to get carried away. Anyway, patch has been already provided, but if you’re using older version or you don’t need namespaces support at all, here’s quick workaround.
MySQL: BLOB vs TEXT
The only difference between the BLOB and TEXT types is that:
- BLOB types store binary data with no collation or character set,
- TEXT types have a character set and collation,
PHP: Call to undefined method on tr_TR locale
It’s one of weirdest errors in PHP - if not the weirdest - I’ve ever seen. If you set your locale to tr_TR, suddenly some classes or methods names are not available anymore. Really.
Following code:
<?php
setlocale(LC_ALL, 'tr_TR');
class SomeClass {}
$r = new ReflectionClass('SomeClass');
$r->newInstanceArgs(array());
results in Fatal error: Call to undefined method ReflectionClass::newInstanceArgs() error.
After quick research it turned out that it’s actually known bug, yet still not fixed since 2002! See #18556. In short, PHP simply uses internal tolower() function, which is locale-aware, but in Turkish there are some specific rules for I (undotted ı is the lowercase of I, and the dotted İ is the uppercase of i). So if you have class name of method name having upper I (newInstanceArgs, Image, etc), you’re affected.
The only workaround for now seems to be setting LC_CTYPE to some working locale, eg. setlocale(LC_ALL, 'tr_TR'); setlocale(LC_CTYPE, 'en_US');.
Sublime Text 2: sync project side bar with active file
It is simple like adding tiny plugin:
import sublime
import sublime_plugin
class SideBarListener(sublime_plugin.EventListener):
def on_activated(self, view):
view.window().run_command('reveal_in_side_bar')
Plugin: https://github.com/sobstel/SyncedSideBar.
Actual author: http://www.sublimetext.com/for….#p19213
Internet Explorer under Linux or Mac OS X
- Download and install VirtualBox
- Download Internet Explorer
curl -s https://raw.github.com/xdissent/ievms/master/ievms.sh | bash- or
curl -s https://raw.github.com/xdissent/ievms/master/ievms.sh | IEVMS_VERSIONS="9" bash(IE9 only) - or
curl -s https://raw.github.com/xdissent/ievms/master/ievms.sh | IEVMS_VERSIONS="8" bash(IE8 only) - or
curl -s https://raw.github.com/xdissent/ievms/master/ievms.sh | IEVMS_VERSIONS="7" bash(IE7 only)
- Adjust RAM settings as it’s only 256MB by default (I’ve used 1GB for each on box with 8GB RAM and 768MB for each on box with 4GB RAM).
- Run virtual machine and log in to Windows as Administrator (password: Password1).
- Execute
slmgr –rearmfrom terminal (to run terminal just typecmdinto start menu input). - Add your hosts to
C:\Windows\System32\drivers\etc\hostsfile, eg.10.0.2.2 my-super-site.local(10.0.2.2 is your host’s localhost). - Install VirtualBox guest additions.
- Shutdown and make a snapshot, so you can use it as a starting point if the trial period expires (it’s not illegal, even Microsoft themselves tell us to do it this way, which is understandable as it’s all about adjusting our websites to work on their faulty browsers).
Javascript lazy loading with dependencies
Here it is, yet another javascript lazy loader. Sadly, none of existing ones has been solving my problem and I was even using great $script.js for some time, but eventually written new one.
Why?
- extremely lightweight (less than 0.5KB gzipped)
- lazy (on-demand) loading
- support for google.load out of the box
- any code (not only loaders) can be executed
Link: https://github.com/sobstel/scru.js.
Sample usage:
// Adding to queue:
$scru.queue('google:jsapi', $scru.fn.async_load('http://www.google.com/jsapi'));$scru.queue('google:visualization', $scru.fn.google_load('visualization', '1', {packages:['corechart']}), ['google:jsapi']);
// Somewhere in some script (called only when needed)
$scru.execute(drawChart, ['google:visualization']);
All jsapi, visualization js are loaded only when “execute” is called.
CSS Image
Using Data URI scheme you can embed images directly in HTML or CSS. It can be useful for two things:
- To save on requests to image files (images are loaded and cached only in one single request together with CSS file).
- To embed images in HTML emails and bypass filters that normally would prevent them from displaying.
You can encode string with Base64.encode64 (Ruby) or base64_encode (PHP).
<?php
$image = base64_encode(file_get_contents('image.jpg'));
echo '<img src="data:image/png;base64,'.$image.'/>';
?>
See the output by inspecting source tag for the image.
Codility sample demo test
I have taken Codility sample demo test at last. I was delaying this memorable moment for a few months, probably for fear that it could somehow turn out Im not that good as I think I am, which is surely the case for most developers. So I did the test and I’m going to take more tests there as it’s quite good fun. And here are my solutions for sample demo test below, both in PHP and Ruby.