Fast and secure Composer for PHP dependencies

I am going to introduce you two small tweaks that will help you to use composer. Every PHP developer should know Composer – it’s really useful tool for managing dependencies in your PHP projects. But it’s not perfect as is. Composer is really slow (and sometimes there’s a lot of memory consumption). You can really fix this issue by installing prestissimo. Just run this command:

composer global require "hirak/prestissimo:^0.3"

Your composer is as fast as hell now but there is one other thing. You may install libraries with known security bugs and you are not going to get noticed on this. That’s probably not what you want at all. And we can fix that as well.

You may use security advisories by Friends of PHP (was developed by Fabien Potencier / Symfony). This is pretty great tool but you have to download it and run the security checker to check if your composer.lock does not contain any libraries with security vulnerabilities. It’s not problem to hook this action to precommit hook in GIT but I think it’s not really comfortable. I am using Roave Security Advisories and I recommend you to use this library too.

This package should not be installed globally, so you have to add this as requirement for all your projects where you want to use it:

composer require roave/security-advisories:dev-master

Since that it won’t let you install library with known vulnerabilities and that’s exactly what you want. It will look like this:

Specify Roboto font in Android

There is a pretty nice font in Android called Roboto. Introduced in version 4.1 and enhanced in 4.2 and 5.0. Specimen and download is available at android design guidelines. What’s kinda missing is the way how to use the Roboto font in your app.

android:fontFamily="sans-serif"           // roboto regular
android:fontFamily="sans-serif-light"     // roboto light
android:fontFamily="sans-serif-condensed" // roboto condensed
android:fontFamily="sans-serif-thin"      // roboto thin (android 4.2)
android:fontFamily="sans-serif-medium"    // roboto medium (android 5.0)

Combine above with font weight specification and you will get all the possible variants:

android:textStyle="normal|bold|italic"

Update PHP to 5.6 @ debian wheezy

I’ve just updated PHP version to 5.6. Check the migration guide for incompatibilities (no big thing there). You will get slightly better performance and some new features.

# sudo nano /etc/apt/sources.list.d/dotdeb.list

And insert these two lines:

deb http://packages.dotdeb.org wheezy-php56 all
deb-src http://packages.dotdeb.org wheezy-php56 all

And add the key:

# wget http://www.dotdeb.org/dotdeb.gpg -O- |apt-key add -

You are ready to go, just update & upgrade your system.

# apt-get update && apt-get upgrade

If you get The following packages have been kept back: you have to run:

# apt-get dist-upgrade

 

Giveaway – Avocode Beta Invite

I have one beta invite to Avocode and I would love to share it with you. Avocode is an awesome tool created by madebysource.com (you may know their other products – CSS Hat, PNG Hat or LESS Hat). And what is the Avocode?

If you code websites you will love it. It’s a way how to get rid of Photoshop because you are not going to need Photoshop anymore. You just open the psd file in Avocode and extract all needed resources simply by clicking the right buttons. And there is more. You will get resources for all resolutions, CSS file or even icon font. Check out the video below or the website wit all avocodes features:

How to get an invite?

Post comment to this article (with your email address). By the 7th September I am going to announce the winner (will be drawn from a hat) and I will send the invite to his/her email account.

Simple like that.

Note: Avocode is now available only for Mac, however even if you are not a Mac user, you are eligible to try to get the invite. But you will have to wait for Linux/Windows version .

7 tools every PHP developer should know

GIT | Bitbucket | GitHub

Distributed revision control and source code management system is what wikipedia says about GIT.  GIT is powerful tool giving you an opportunity to version your code and much more (for example deployment – try it and you’ll love it).

Bitbucket and GitHub are web based hosting services for your repos. Bitbucket gives you unlimited number of private repos (up to 5 users) and GitHub is mostly used for open sourced projects (public repos), so you are able to find there almost every source code you need.

Do you still use Subversion? It’s time to give a try to a GIT.  Are you not versioning your files? You should start with GIT right away!

FTP | Filezilla

GIT deployment is totally cool, but there’s sometimes situation you are not able to use it. For these moments there is the Filezilla – great FTP client supporting linux and windows. Go ahead.

IDE | PhpStorm

Of course you are going to need an IDE. And in my opinion the best one is PhpStorm. For a reasonable price you will get kick ass tool. Integrates GIT, FTP client and terminal, so you really don’t need to switch to other apps during the development. Really worth a try.

Framework | Nette Framework

There’s a lot of PHP frameworks. You probably know Symfony or Zend, but the slickest one is not so widely known framework called Nette. If nothing you should at least check out Tracy (debugging tool), Tester (do you write tests, right?) or Latte (amazing template engine).

Tracy in action
Tracy in action

Dependencies | Composer

You don’t know Composer? And how do you maintain your projects libraries? Go for it right now. This is the future of PHP development.

Hosting | Digital Ocean

If you are maintaining a lot of web apps or websites you should consider to buy a VPS. Just because it’s cheaper and you have the whole environment under your control. There’s no problem with old version PHP, the PHP is set up the way u want to and because you can set up the GIT deployment.

I recommend to try Digital Ocean – it’s working on SSD and servers are really fast and surprisingly cheap. Starts at $5/month, simply scalable.

Server & Database | Nginx + Postgresql

You are probably familiar with Apache (web server) and MySQL. Check out MariaDB (MySQL fork, for now it’s compatible) – it might be better match for your projects. Anyway there’s a lot of great stuff in web servers too. Earlier this year I was trying Lighty (lighttpd) but afterall, the nginx looks like true love for me and I am trying to use this one for all projects from now on. Why? It saves me a lot of server resources and it’s highly configurable.

And at last but not least the PostgreSQL. MySQL is great but not compared to PostgreSQL. If you need to work with JSONs or geodata and you still want to use object-relational database, this is the one.

 Resume

Lot of interesting tools were mentioned above but there’s still lot of them out there. Share your favorite tools in comments. Really looking forward for your opinions.

 

Android Tutorial – Square Layout

For the latest app I’ve developed, I needed to use squared layout. It would be easy with fixed size – I would put the height (width) dimension into the dimens.xml file and it would work. However that wasn’t what I needed.

The goal is to have layout which will be squared with width (or height) set up to match_parent.

And because this is not part of the Android SDK, we are going to create it. Firstly create new Java class in your project. I store these files in src/main/java/com.example.something/tools but you are free to choose your own destination 😉 Name it SquareLayout.java.  This file will be extending the LinearLayout, so I will just paste the complete code, it’s pretty simple to understand.

public class SquareLayout extends LinearLayout {

    public SquareLayout(Context context) {
        super(context);
    }

    public SquareLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int width = MeasureSpec.getSize(widthMeasureSpec);
        int height = MeasureSpec.getSize(heightMeasureSpec);

        // this is the scale between width & height, for square should be == 1
        int scale = 1;

        if (width > (int)(scale * height + 0.5)) {
            width = (int)(scale * height + 0.5);
        } else {
            height = (int)(width / scale + 0.5);
        }

        super.onMeasure(
                MeasureSpec.makeMeasureSpec(width, MeasureSpec.EXACTLY),
                MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY)
        );
    }
}

Well done. Now we can use this new layout in your layout files. And it works like this (don’t forget to change com.example.something to your namespace):

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_margin="60dp"
    android:layout_width="match_parent" android:layout_height="match_parent">

    <com.example.something.SquareLayout
        android:background="@color/pink"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        </com.example.something.SquareLayout>

</RelativeLayout>

And it should look like this screenshot:

android-square-layout-tutorial-example

Poker Incomes Reports – android app for casual poker players

Are you a non-pro poker player and do you want to track your winnings (or loses) in poker? This is the app for you. Poker Incomes Reports let’s you track your poker incomes and divide them into the three main categories – tournaments, cash games and online poker.

 

Upgrade from MySQL to MariaDB at Debian

Since I’ve decided to improve performance of my webservers (and migrate from Apache2 to Lighttpd) I wondered if I could do something with an MySQL. And after a research I made a decision that I am going to migrate my servers to MariaDB.
If you never heard about MariaDB you should probably know that MariaDB is a fork of MySQL because MySQL has been bought by an evil company and it’s not so much open sourced and the origin creators of MySQL have decided to do better db server and that’s MariaDB and so on and so on. Boring stuff mostly.Why to migrate?

  • open sourced
  • better engines (in my opinion)
  • performance might be slightly better
  • and it’s fully compatible with MySQL. For a now.

How to migrate?

Surprisingly simple. Python software properties are just for adding new repository by add-apt-repository, you don’t need to install this, if you know how to add repository.

# apt-get install python-software-properties

And add keys:

# apt-key adv --recv-keys --keyserver keyserver.ubuntu.com 0xcbcb082a1bb943db

Add repository:

# add-apt-repository 'deb http://mirror.stshosting.co.uk/mariadb/repo/10.0/debian wheezy main'

Update packages:

apt-get update

And install MariaDB:

apt-get install mariadb-server

mysql_replace_mariadb.fw

That’s it. During installation will be stopped the MySQL server, installed MariaDB, you will be asked for root password and then will started mariaDB server. Service is still mysqld, actually for you  – nothing changed. Everything works the same way as with the MySQL.

And if have not used the latest mysql version, you should run

mysql_upgrade

after installation.

ButterKnife inject views in Fragment

I’ve lost a lot of time to figure it out. I had in onCreateView() in my Fragment:

 ButterKnife.inject(this, rootView);

and it wasn’t working (nullpointerexception), so I was testing what’s wrong and could not fixed it, because it looked allright. And the solution is simple as always. This is the solution for Android Studio (for Eclipse check this)

Open File -> Settings (ctrl + alt + s). Navigate to Compiler -> Annotation Processors and at the top of the window check the Enable annotation processing. That’s it.

butterknife-fragment-inject-android-studio