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.


This was posted 2 weeks ago. php doctrine

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,
This was posted 3 weeks ago. mysql

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');.

This was posted 1 month ago. php

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

This was posted 3 months ago. sublime text 2

Internet Explorer under Linux or Mac OS X

  • Download and install VirtualBox 
  • Download Internet Explorer
  • 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 –rearm from terminal (to run terminal just type cmd into start menu input).
  • Add your hosts to C:\Windows\System32\drivers\etc\hosts file, 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).

This was posted 4 months ago. linux mac os x internet explorer

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.

This was posted 5 months ago. javascript

Call class method via instance in Ruby

This was posted 6 months ago. ruby

CSS Image

Using Data URI scheme you can embed images directly in HTML or CSS. It can be useful for two things:

  1. To save on requests to image files (images are loaded and cached only in one single request together with CSS file).
  2. 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.

This was posted 6 months ago. css htmlemail

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.

This was posted 7 months ago. php ruby codility

Devise: find by username OR e-mail

This was posted 8 months ago. ruby devise rails