Archive for the ‘Programming’ Category

How Ars Does Live-blogging With WordPress

Monday, March 4th, 2013

A great summary on using WordPress, S3 and a few other tools to build a live-blogging post for real-time event blogging.

http://arstechnica.com/staff/2013/03/how-ars-slayed-the-moonshark-on-building-and-running-liveblogs/

Slim Globalization Plugin

Saturday, December 29th, 2012

Check out our latest bit of original code. This time for the PHP Slim framework. It’s called SlimGlobalization and it enables standard Internationalization capabilities to your Slim app.

The plugin is available over at Github or it can be downloaded as a compressed file.

Performance Tuning SQLite Inserts

Monday, November 26th, 2012

Found on StackOverflow: How do I improve the performance of SQLite?

Web-based API platforms

Friday, October 26th, 2012

There is something nice about Google’s App Engine. Deployment to the GA cloud is done through a desktop application, the database schema is mutable and the environment scales to meet any load. The developer deploys the app without the mess of custom built scripts and server administration. The only downside to Google’s product is the choice of programming language. At Spafax, we use PHP almost exclusively for web development. To switch to either Python or Java would be impractical and expensive.

For our needs, I set out to find a platform that could act as a back end to our web and mobile applications. Each utility application would serve mainly as a data store and business logic repository. Potentially, they could emulate the type of service provided by AddThis: limited in scope by providing a very specific function via JavaScript or REST API.

The database model is not expected to grow past 10 models. There would not be a need for complex queries. There is, however, a need for performance as any web-based service naturally suffers from latency of the network.

Out of the needs grew the following check list:

  • Push of a button deployment: we are not a big team
  • NoSQL-style DB for persistent data storage: there will be few joins
  • File hosting: serving static content is a plus
  • Custom domains: this is a must
  • Supports PHP development: our team is uses primarily PHP
  • Production ready: no services in Beta
  • Easy enough for a junior developer: setting up a local environment should be fast and easy

Following is a list of solutions I found that meet at least some of the features above. Each solution is evaluated based on available documentation, not on experience.

AppFog

AppFog is the most mature PHP cloud host. Their free server includes 2GB of memory and 512MB of storage. Prices quickly escalate to $100/month for 4GB of memory and go up from there.

AppFog includes a versatile command line tool. With the command `af`, a programmer can deploy, start and stop an app. She may also check the crash logs, boot additional servers and much more. A full list of features is available at the support section of their site.

Unlike Google’s App Engine, AppFog does not come with a proprietary framework. This is both a blessing and a curse. Though you can use any PHP library or Framework, I was looking for a service with tight integration between the code and the storage mechanism. Otherwise, the ease of development is reduced by having to install a local MySQL instance.

A huge setback is the database MySQL and PostgreSQL are there, so is MongoDB. Though MongoDB is a schema-less solution, recent blog posts have pointed some of its immaturity. The other two database solutions can be made almost schema-less with RedBean, but it would still require configuring a local instance of MySQL.

More at AppFog.com.

PHP Cloud

Next up is Zend’s own PHP cloud. Though it is promising, given the Zend name, the service is in beta and does not consider itself a product environment. Instead, Zend expects you to develop and test on their cloud, then move the code to a production-ready environment.

More at PHPCloud.com

Orchestra IO

At first glance, Orchestra seems like another virtual host provider. It enables easy project initialization and publishing via Git. The free application provides a MySQL database but no storage.

Orchestra is probably the closest to our needs. However, once again, the MySQL database solution is a setback as configuring a MySQL instance for every developer seems like a hassle.

More at Orchestra.io

Microsoft Azure

Microsoft’s Azure service is an unexpected entry in the cloud PHP provider list. Their service offers the standards MS stack of development tool and support for some open source technologies. Specifically, Azure includes PHP with an API to their unstructured storage mechanism called Azure Storage.

Azure offers publishing via Git.

Unfortunately, the Azure Storage emulation environment is limited to Windows 2008 servers and as we are a Mac-only shop, this limits our ability to develop on that platform.

More at windowsazure.com

AppEngine with Quercus

The final contestant is AppEngine. It does not support PHP natively but with the help of Quercus, PHP can be interpreted by the JVM. This way, PHP has access to the full Java library provided by AppEngine.

Though this solution is very appealing, going through another programming language takes something away from the ease of building PHP applications.

More on Quercus

Summary

Though many services now offer easy deployment of PHP applications and dynamic scaling, most fail to make the distribution process invisible. Setting up a local sever and MySQL (or MongoDB) instance is still necessary, so is maintaining MySQL migration scripts. You can sidestep assist with most of the problems using tools such as MAMP or RedBean but, ultimately, no environment will satisfy all of our needs the way AppEngine could in Python.

In a future post, I will discuss the decision I took and the tools I used.

 

 

 

The PHP getter/setter

Monday, March 12th, 2012

Perl has a neat way to define both a getter and a setter in the same class method. The basic definition goes a little like this:

sub color {
  my $shift;
  if (@_) { # are there any more parameters?
    # yes, it's a setter:
    $self->{Color} = shift;
  } else {
    # no, it's a getter:
    $self->{Color};
  }
}

PHP can do something similar with a structure that matches quite cleanly to the code above. The example below is taken from our upcoming stand-alone form library.

public function name() {
    if ( func_num_args() ) {
        $this->_name = func_get_arg( 0 );
        return $this;
    } else {
        return $this->_name;
    }
}

In the example above, passing a value to the method named name() will set the value and return the object (to allow chaining). If the method is called without a parameter, it return the current value of the object’s name.

This structure is preferable to the magic methods __set and __get for its clarity.

Easy PHP templates

Tuesday, February 21st, 2012

Found this browsing the Sitepoint forums. A great and simple method for building templates in PHP:

class Template extends ArrayObject {
  protected $file = null;
 
  public function setFile( $file ) {
    $this->file = $file;
  }
 
  public function __toString() {
    extract($this->getArrayCopy());
 
    ob_start();
    include($this->file);
    return ob_get_clean();
  }
}
 
// In your controller...
 
$template = new Template();
$template->setFile( 'myTemplate.phtml' );
 
// Fictional model method to get rowset from a query
$template['rows'] = $db->getRows( $query );
 
$template['title'] = 'Hello World';
 
echo $template;

Thanks, Michael Morris.

Original post by Michael Morris

Fun With Less: Buttons

Friday, February 10th, 2012

This first of a series of articles that deal with Less CSS we’ll introduce a fun way to turn an element into a button with the help of a sprite graphic. First comes the sprite map:

Sprite Buttons

Each of the buttons are stacked on top of each other and every one has the same height: 27 pixels.

Next comes the Less mixin. This is where the magic happens:

@webroot: "http://domain.com"
 
.button (@position, @width: 180px) {
    @buttonHeight: 27px;
 
    display:     block;
    height:      @height;
    width:       @width;
    text-indent: -100em;
    overflow:    hidden;
    border:      0;
 
    background:  url("@{webroot}/images/buttons-en.png") no-repeat left -(@position * @buttonHeight);
}

The above code does a number of things worth going over in detail. @webroot is the root URL of the website. Less compiles all CSS into the current page making it necessary to use absolute URLs instead or URLs relative to the CSS file’s path.

Next, is the mixin declaration. It defines two parameters. The position of the button (starting with 0 for button 1) and the width of the button—set to width of the sprite map.

The button height is then hard-coded and the CSS following uses one of the common image replacement techniques.

Finally comes the background attribute. It says, use the image map positioned to the left of the container. The top of the background is determined by the button number (starting with 0) times the height of each button, subtracted from 0. In other words, button 1 will be at top 0, button 2 at top -27, button 3 at top -54, etc.

The resulting mixin is easy to use.

/**
* Makes all buttons of class 'search' use the last image in the sprite set.
* Uses the actual width of the 'search' button graphic to prevent trailing white space.
*/
button.search {
    .button(3, 82px);
}
 
/**
* All links of class 'more' use the first button in the set.
*/
a.more {
    .button(1, 91px);
}

MySQL’s “Warning: Field ‘id’ doesn’t have a default value”

Tuesday, January 31st, 2012

The following error came up on a project built on Django and MySQL:
“Warning: Field ‘id’ doesn’t have a default value”

At first, it seemed as though Django was responsible. However, a quick Google search proved this was an issue with MySQL.

Some solutions returned by the search required the programmer/administrator to export and recreate the database, but the solution proved a lot more simple. For a table named auth_user, the following MySQL command fixes the above error message:

ALTER TABLE auth_user MODIFY `id` INT(11) NOT NULL AUTO_INCREMENT;

The above command resets the ID field of the auth_user table to an auto-incremented integer of length 11, fixing the error message for good.

Variable height UIView

Sunday, September 4th, 2011

In the latest iPhone app, it was necessary for a particular view to grow or shrink with respect to its visible subviews. Bellow is a quick snippet from a class called VariableHeightView, a subclass of UIView.

- (CGRect)frame
{
    CGRect frame = [super frame];
 
    float height = 0.0f;
    for (UIView *asubview in self.subviews) {
        if (![asubview isHidden]) {
            float subviewBottom = asubview.frame.size.height
                                    + asubview.frame.origin.y;
            if (subviewBottom > height) {
                height = subviewBottom;
            }
        }
    }
 
    CGRect newFrame = CGRectMake(frame.origin.x, frame.origin.y,
                                 frame.size.width, height);
    return newFrame;
}

The code is fairly simple. For a view built using the Interface Builder, the VariableHeightView’s (a superview) frame function will iterative over all subviews, looking for the one lowest in the screen. Once found, it will expand to match that sub-view’s Y origin plus height.

The drawRect function of UIView will take care of rendering the VariableHeightView using the dimension of the frame function.

PHP array_remove and array_remove_assoc

Thursday, August 25th, 2011

The array_remove and array_remove_assoc PHP functions allow your code to remove an element from an array (or associative array) given the element’s value. See the comments in the code bellow on how to use the two functions.

Download the code

<?php
/**
 * Removes the given value from the given array.
 *
 * Returns either FALSE if the value was not found in the array
 * or the index at which the value was found and removed.
 * 
 * $array = array('a', 'b', 'c', 'd');
 * assert( array_remove( 'b', $array ) == 1 );
 * assert( array( 'a', 'c', 'd' ) == $array );
 * assert( array_remove( 'z', $array ) === false );
 * assert( array( 'a', 'c', 'd' ) == $array );
 *
 * @param mixed $val The value to remove
 * @param array $array The array from which to remove the value
 * @author Dimitry Zolotaryov, http://old.webit.ca
 * @returns FALSE or the index at which the value was found
 */
function array_remove( $val, &$array ) {
    foreach ( $array as $i => $v ) {
        if ( $v == $val ) {
            array_splice( $array, $i, 1 );
            return $i;
        }
    }
    return false;
}
 
/**
 * Removes the given value from the given associative array.
 *
 * Returns either FALSE if the value was not found in the array
 * or the key at which the value was found and removed.
 *
 * $array2 = array( 'a' => 1, 'b' => 2, 'c' => 3, 'd' => 4 );
 * assert( array_remove_assoc( 1, $array2 ) == 'a' );
 * assert( array( 'b' => 2, 'c' => 3, 'd' => 4 ) == $array2 );
 * 
 * @param mixed $val The value to remove
 * @param array $array The associative array from which to remove the value
 * @author Dimitry Zolotaryov, http://old.webit.ca
 * @returns FALSE or the index at which the value was found
 */
function array_remove_assoc( $val, &$array ) {
    foreach ( $array as $key => $value ) {
        if ( $value == $val ) {
            unset( $array[ $key ] );
            return $key;
        }
    }
    return false;
}

Download the code