Pair program with me! profile for carousel at Stack Overflow, Q&A for professional and enthusiast programmers

4/27/2014

Laravel with OnApp cloud

Developing in (and for) the cloud has become defacto standard skill for any modern web developer. Without knowing how to, for example, setup Apache server on AWS Linux instance - important tools will be missing in your development toolset.
Many providers are moving to cloud. Fortrabbit (my provider of choice) is offering SSH connection to all application resources, so it is important to become familiar with cloud concepts.

These days I am working on one awesome project - it is basically Laravel SaaS application that connects to cloud servers, with the help of OnApp. OnApp is cloud management system for service providers.

There is no Laravel package for OnApp, but there is great  OnApp PHP wrapper, for API calls to virtual server. That wrapper is just an abstraction over php_curl, which internally handles all HTTP calls to servers, and returns response in one of two forms - XML of JSON.

Now, that PHP wrapper is very nicely designed, and allows easily communication with servers through native API. It has some very neat solutions, that makes pleasure working with API.

I will point some key features, that are crucial for understanding how it works.

First, to make use of wrapper, you will have to include it in your app. First line of code should be call to OnAppInit.php file, which autoloads all dependencies. Something like:

//based on my path
require "/lib/Onapp/OnAppInit.php";


With this line, all classes are autoloaded, and we can move to actual connection to server. This is where the beauty comes. Connection is made with the help of Factory class instance, which implements factory design patterns, that can instantiate all internal OnApp classes. Next, all OnApp classes inherits from base OnApp class, and allows unified interface for all inherited classes. It is done with the help of factory.

To connect to server, just call Factory class like this:


$factory = new OnApp_Factory($server,$user,$password);



Where $server represents full url to server (not just server.com), but requires protocol (http://server.com). If you made mistake, leaving out the protocol type, you can spend days trying to figure out where is the problem :)

After that you are connected, and ready to use API. But with basic connection, you can't go far. Factory class has factory method, that is responsible for instatiation of all other classes.
To make it more clear, here if that factory method (from the main Factory class):

public function factory( $name, $debug = false ) {
    $class_name = 'OnApp_' . $name;

    $result = new $class_name();
    $result->logger->setDebug( $debug );

    $result->setOption( ONAPP_OPTION_DEBUG_MODE, $debug );
    $result->logger->setTimezone();
    $result->version = $this->getAPIVersion();
    $result->options = $this->options;
    $result->_ch     = $this->_ch;
    $result->initFields( $this->getAPIVersion() );
    return $result;
}


Now things are more clear. Factory argument name will become OnApp clas name. Factory method also serves as configuration method, that will return new instance for chaining.

So to return User class, just call this:

$user = $factory->factory("User");

//or some other, useful classes
$user = $factory->factory("Log");

//or
$user = $factory->factory("BillingPlan");


Now, with those instances, you can use OnApp methods, that allows easily working with API. All instances have getList method. User class is used commonly (there have to be users :), so to list all users, you will do:

//list all server users
$user = $factory->factory("User")->getList($user_id);

// or to list all server logs
$user = $factory->factory("Log")->getList($user_id);


Remember that you are connected as user, with the help of base Factory class. Depending on you permisions (user of admin) you can list different users.

In a nutshell, this API allows creating powerfull SaaS applications, from your base code. In my case, all incoming data (in the form of JSON) are part of application, and I can manage users and server state easily from one central point. There is much more then this - you can pull statistics, work with hypervisors, work with virtual machines... It is really rich set of tools for cloud management.

Thanks for reading.

4/10/2014

How to use Xdebug profiler and Kcachegrind tools in PHP project?

I use more and more awesome Xdebug profiler in combination with Vdebug plugin and Kcachegrind.

Xdebug is standard PHP debugger. Vdebug is Vim plugin that let's you easily debug and trace PHP (and not just PHP) code from Vim. Kcachegrind is amazing piece of software which let's you visually represent whole application calling stack, and do a memory profile and discover bottlenecks.

You need some time and slight tweaks to enable all this tools to work.
But when you do that, you have awesome toolset that let's you dissect you code in a number of different ways. I mostly use it for learning new API-s, writing my extensions and looking at architecture and design of existing code (these days mostly Laravel).

Fist step is to install and enable xdebug. Here is how to do that on ubuntu:

    suto apt-get install php5-xdebug

And check in your php.ini that xdebug is enabled.

Here is my Xdebug setup from /etc/php5/apache2/php.ini file:

    zend_extension=/usr/lib/php5/20121212/xdebug.so
    xdebug.remote_enable=on
    xdebug.remote_handler=dbgp
    xdebug.remote_autostart=1
    xdebug.remote_host=localhost
    xdebug.remote_port=9000
    xdebug.profiler_output_dir="/var/log/xprofile/"
    xdebug.profiler_append=On
    xdebug.profiler_enable_trigger=On
    xdebug.profiler_output_name="%R-%u.trace"
    xdebug.trace_options=1
    xdebug.collect_params=4
    xdebug.collect_return=1
    xdebug.collect_vars=0  
    xdebug.profiler_enable=1
     ;xdebug.auto_trace=Off

As you can see it is divided into xdebug settings that let's you debug, and setting that allows profiling. Here you can find some advanced settings, which let's you save file names based on function call. By default profiler saves all files with some random prefix, and it is hard to distinguish between them

To install Vdebug on Vim, just install this plugin
https://github.com/joonty/vdebug and follow installation process. After that you wil be able to dubug and set breakpoints with builtin vim mappings.

In order to save and use output from profiles, you need to create default location for files. This is my location:

    xdebug.profiler_output_dir="/var/log/xprofile/"

Now, every time you start a project in the browser, a new profile file will be created.

Next tool is Kcachegrind. This is project home:

    http://kcachegrind.sourceforge.net/html/Home.html

But it can be easily installed through CLI of software center. Kcachegrind allows you to have visually insight into project calling stack. For me it was  far better learning experience then reading tons and tons of books. With this, I am actually "in the source code", without external explanation about that code. I can see how authors were designing whole application and have some advanced insight into whole process of app creation.

There is one gotcha with Xprofile. If you don't delete generated files, they can easily accumulate and consume lot of memory. You won't be always  using xdebug but profiler will always generate those files. So you need to manually delete them. It can be also done with some kind of deamon process and backround script.

This is how I solve this problem, with one command. I created alias like this:

     alias xd="sudo find /var/log/xprofile/* -mmin +0 -exec rm {} \;"

And placed it into my .zshrc file. With this alias command, all files inside xprofile directory older then 0 minutes will be deleted - which means all files will be deleted.

Alternatively, you can create new bash script/file, with this command:

    #!/usr/bin/sh
    sudo find /var/log/xprofile/* -mmin +0 -exec rm {} \;

Place it somewhere on your path, provide permissions and you will have globally executable command that will clean xprofile folder.

Thanks for reading.


3/14/2014

Laravel refactoring hell

Recently I had an opportunity as a freelancer, to work on existing codebase project. It was supposed to be a simple Laravel CRUD project, that does nothing more then collecting user data and storing it in a database. There were some people working on this before me, and I along with other members of the team supposed to finish that project.

Since I am primarly Jeffrey Way's Laravel student, I used to watch him how he writes clean and testable code. Also Jeffrey taught me to think very abstractly, and to think about future of the code - not just because of me, but because of other developers that could read my code.

I learned throughout  my career to teach programming just from high quality sources and to follow top level developers/programmers.

So in that sense (very naively) I was expecting to see everywhere that basic pattern, which is very human in its nature. Write and teach for people, not just for yourself. Code is a poetry - that is my moto. I am staring at this screen most of my day - so let me create art.

BUT

That code was everything else except human readable and maintanable.
Let me be more expressive - it was worst nightmare that can happen to a developer. It was big ball of mud. But very low quality mud - not the mud that you can wash easily - but stinky mud. Almost crap.

Ok, enough of pathetic. Lets talks about the facts. First let me point that project was old more that 5 months. Very trivial project that could be implemented for 2-3 weeks, was lasted for more then 5 months.
With potential to never ends in this form. While looking in the code, I became aware about the state of the mind of the prior developer. And his attitude toward this project. Everything was telling me that he hated this project and that he didn't have enough experience for that. When I was told that it was his first "bigger" Laravel project, everything becames clear to me.  It was natural for that project to became abandoned.
Also requirements were very poorly explained to me. There was no clear separation between developers in the team. Just make it work attitude. Project leaders were missing programming and leadership knowledge. It was obvious that this project needs refactoring and it is impossible to make it work. Well, it can just work,  but how long ? Every composer update can break whole project.

Can it be worse ?

I could stop writing this post because it is obvious what to expect next - pure horror written by unexperienced developer.

But there are some interesting things in that code. When I think now, it was rare opportunity for me to work on such a project. So I decided to learn as much as I can. 

Here are some juicy facts:

  • Prior developer was a kind of old-school PHP developer. He was trying to insert plain PHP as much as he can. 
  • He was forcing his own (bad) development style into Laravel architecture
  • He had his own rules about environment detection, completely ignoring Laravel environment handling ( or not knowing ).
  • Code is full of constants, even Laravel has some elegant solution for that. 
  • It is almost not commented.
  • There is no standard indentation, naming and code style consistency.
  • Powerfull blade templates are rarely used.
  • There are  view partials everywhere - without obvious reason.
  • There are views everywhere, with inline CSS, PHP, Javascript.
  • There is huge number of controllers, migrations, models... Everything is huge in this code. And everything is so ugly. He didn't know how to abstract things.
  • Pivot tables and complex relations are used with no reason, just to make things more complicated  
  • There is a lot of empty and not used functions/methods.
  • There is a front end code mixed with server side scripts (assets folder in app/). This was my favourite - when I mention that, I was cursed :)
  • There is a lot of everything everywhere, where it shouldn't be.
  • Dependencies, dependencies and dependencies everywhere.
  • Not tested at all - but should I expect it to be tested ? It is impossible to test this project.
How much should developer charge to refactor this ? I think most developers would write applicaton from scratch - it is easier.

And there are some valuable lessons learned from this. First I have to think about the people, not just about my ass. Someone can read that code for a month of six month. Next, what about responsibility ? Does this guy feel responsible for his work ? Does he think about the users of his project ? I don't think so. Does he think about his career ? No. Sadly, anyone can be self-declared Laravel developer. It is a big problem

That leads me to conclusion that the community should somehow consider Laravel certification. All big framework are certified - so why not Laravel ?

It is hard to find good clients. As a freelancer, I am forced to compete with kids that use dreamweawer and with clients that mostly dont know what they want. And to look at horror code, like this one.

Thanks for reading.

2/23/2014

My toolset

Developers have some special relationships with the tools  they use for everyday work.  I am not exception.

From the beginning of my carrier, I really spent lot of time trying different editors, terminals, operating systems... I was very passionate about that . Sometimes I didn't do anything, but just tuning new terminal or editor, which I quickly replaced with new one. I was aware that it could lead me to insoncistency,  so I was trying to learn as much as I can about new tools. By learning , I was sticking my  nose everywhere - from the Linux kernel to the most highest abstractions.

I didn't mentioned that Windows very quickly become boring, there was nothing interesting there for me. So I become dedicated Linux/Ubuntu user.  And Linux is perfect platform for any kind of programming (well not for C# but you know what I mean ). No developer can convince me that his favourite OS is Microsoft Windows, if  he is not their employee.

I was very fascinated (and still I am) with different (mostly open source) project. It is incredible how such masterpieces like Apache or Vim are totaly free, and you have support for any kind of problems - and it is still free.

These days I am not changing my tools so often - due to fact that I found my (almost) perfect  setup and that I don't have so much time.

So here is a quick overview what I use in my daily practice:

Main OS:
Linux/Ubuntu
I know, I know real hackers are using Gentoo and Slack but Ubuntu, which is based on Debian unstable is really up to date with bleeding edge packages - which are necessery due to fast changing web environment. Ubuntu 14.04. is going to be LTS (long term supported) so after upgrading, I wont be moving further for years.

Main editor:
Vim
What to say about Vim ? If you havent tried it, then no words can express what can Vim mean to productivity. Vim is iconic software and represents more then just text based editor. If you can't find something in the form of Vim extension, then you didn't look carefully. From Vim (with the help of terminal) I can code, debug, test, git, fetch...

Terminal emulators:
I am doing most of my work moving between browser and terminal.
I was using Terminator for long. Terminator has one great feature - you can split windows vertically and horizontally quickly. It is fully supported with all kind of character-sets and just works. One thing that I missed in Terminator was lack of line spacing. Only one Linux terminal that has that feature is Urxvt. Line spacing for me was so important, so I decided to migrate to Urxvt. It more geeky terminal, that needs some advanced configuration, but when you tune it by your needs - there is no turning back. But, nothing is perfect. On Ubuntu you can't set Urxvt transparent background  because Compiz has some weird issues with it. If you help me solve this, I'll buy you a beer , no matter in which part of the world you are :) Along with Urxvt comes Tmux, terminal multiplexer for managing multiple sessions.

Browsers:
Since I am primary a web developer, browser for me are just clients, with additional features. Chrome/Chromium team has produced awesome software. Chrome/Chromium developer tools is very powerful and if used correctly it can increase productivity, not just on client side. Chromium/Chrome is very opened toward developers and by just entering chrome://chrome-urls in the address bar, you can enjoy all different setting and statistics...

And that is it. There is so much power in everything above, that trying something else at this point would be just a waste of time.

At the end, I have to mention IDE-s. Only one that I was using for some time was KomodoEdit. It has good Linux support and some more good things  but that was all. I just don't know how to work in such environment. My fingers are in love with Vim keyboard bindings :)

Thanks for reading.

2/22/2014

Symfony in Laravel

Last couple of days i spent reading this tutorial. It is Fabien Potencier (creator of Symfony) online guide on how to create your own PHP framework on top of Symfony components. 

The reason I was reading this is because Laravel is heavily dependent on Symfony. I was trying to find out why Taylor Otwell (Laravel creator) choose Symfony to rely on.

I also watched some Fabien Potenciers video presentations and read some of his articles. I have to say, that this guy is not ordinary PHP geek, who is just a creator of one successful framework. There are some really great ideas in his talks and I highly recommend you to read something like this article. I was impressed wich some ideas like MVC is not a part of web, and Symfony doesn't have domain logic layer. If you thinks about this, there is a lot of truth inside. I found myself beeing amazed how much I learned from Symfony. 

So lets see what and why is Laravel using. The most important part of Symfony in Laravel is a HTTPKernelInterface. As a developer you know that PHP is interacting with server through SAPI (server application  programming interface). SAPI in PHP is basically implemented in the form of header and echo method. Through SAPI, PHP is sending its response to server. On Apache, default SAPI for PHP currently is Apache2handler. There is one more interface, for interacting with server - CLI SAPI or command-line interface. This is how PHP interact with server through command line. HTTPKernelInterface is build on top of idea that PHP needs better way to interact with server and clients. HTTPKernelInterface has just one method - handle. And that is all. Good programming practice suggest that interfaces should follow interface segregation principle. That principle states that no client should depend on the method that it doesn't use. So, that why there is only one method inside, that should provide all response logic.

Laravel is implementing this interface in the main Application class.There is a lot of things happening inside this implementation - from route collection lookup, to custom response. When you run app->run method, you are triggering stack that will end with returning response to client. Application has some bootstrap process before method run is triggered, but this method is the heart of Laravel HTTP handling logic.

It should be no surprise that both - Laravel request and response  are extensions of Symfony HTTPFoundation classes. So, Laravel is built on top of Symfony - I guess Taylor Otwell was first reading that tutorial :)

Web appllications are complex to develop, due to HTTP stateless nature.
On one end, we as developers have to take care about transport mechanism (protocol), and on another end we have to deal with domain logic (MVC). On native deskop applications, there is no HTTP. Everything is managed locally, and you can develop application in one consistent environment. I think that is the main reason for popularity of Single Page Web Applications. They are trying to simulate desktop environment, with moving most of logic to client. We will see how that trend is going to evolve.

Symfony ecosystem is huge. It is established PHP framework, so Laravel is gaining much of its strength from Symfony. So, if you want to learn more about Laravel, take some time and git clone Symfony. Besides weird yaml format (Laravel is using json) and some Symfony specific configurations, you can learn something new about architecture and design.

Thanks for reading. 



2/12/2014

SOA - Service Oriented Architecture in Laravel

My previous post title was n-tier meets MVC.
In this post I want to add one more thing to that conclusion - n-tier meets MVC with the help of SOA.

Yes, besides n-tier (client/server) and MVC our favourite framework is implementing one more well known architectural pattern. It is called SOA or Service Oriented Architecture. Wikipedia has got pretty good explanation of SOA.


Take a look at this sentence from Wiki:
Service provider: The service provider creates a web service and possibly publishes its interface and access information to the service registry.

Sounds familiar ? Well it took me a while untill I finally realized what is Laravel doing in one of its core parts. Like many Laravel developers I was struggling with facades, providers and containers terminology without knowing that it is just an implementation of well known pattern. Modified, but concept is still the same.

Less obvious part of this  is why Taylor Otwell actually decided to design framework with SOA style incorporeted ?
Knowing that he has strong .NET background can help. In Microsoft world, they traditionally care about services. I was teached to think Restfully, so this was hard concept to grasp.

So the story is:

In the middle of the Laravel  kingdom lives Application, that depends on its citizens and servants. Application can exists by its own, but there is no point in empty kingdom. Every kingom has a treasury.  So they build one. In this kingdom it is called Container . 
Then Application defined  a rules and exposed them publically through its main  accociate - ServiceProvider class. All new citizens have to behave and look the way it is defined by the king.
ServiceProvider announced that everyone who wants to be a part of Application kingdom must implements two methods:

  • register and
  • boot

Register to become part of kingdom and boot to invite their friends to join. Also, Application allows some level of freedom to their citizens and let them choose their names and how they will be called. Speeking in Laravel terms, they can use Aliases and Facades for their access.

But, not everything is perfect . Some citizens are on the higher position then others. So they are treated specially. e.g. Request class is called very early, and it is bound directly, without use of ServiceProvider class.
On the other hand some citizens choose fancy names like Config, but underneath they are originally called Repository.

What would be a kingdom without a police ? Events are everywhere.
They provide welcome to every new citizen (provider) and take care of illegal activities. If something bad happened they will fire its main weapon - Exception. In our kingdom it is allowed to have private police.
It is no problem. Even better - it is a free gift from one of the highly rated Applications servants. If you want to take care if right type of rules are applied in your Validation, just call Event police - and it will fire all exceptions.  

So, even it sometimes looks that things are out of order in typical Laravel application, it is just because developer didn't understood properly how framework is internally designed and with what intention in mind. In all other casess, we have great tool for building web applications.

Thanks for reading.

2/09/2014

n-tier meets MVC in Laravel


This is the post about two fundamental architectural patterns that can be found in most todays web framerworks. These patterns are:

  • n-tier represented as client/server model and
  • MVC (model,view,controller)

There is a lot resources online client/server, n-tier so I will take a look at Laravel specific implementations of these patterns. You can argue is MVC architectural or design pattern. I think MVC is closer to well established arch patterns due to its usage and overall complexity.

In Laravel we can exactly detect boundary between them as so as their main flow. Client model starts in the form of HTTP request, which is received from the browser (mostly) and provided to main Application class:

public function __construct(Request $request = null)
 {

  $this->registerBaseBindings($request ?: $this->createNewRequest());

  $this->registerBaseServiceProviders();

  $this->registerBaseMiddlewares();
 }


Request comes in a form of Laravel request which extends Symfony class. After application receives a request, it will registered it as a protected property (object). In this phase, flow is pretty straightforward. If there is no custom middleware functionality, request will end waiting for handler method. There are some minor processing like attaching session driver, but main job is done in a layer that comes after.

I mentioned middleware, which is actually all functionality that lives in a space between the reqeust hits the internal application routes. Laravel (from 4.1 Iversion) implements StackBuilder middleware, which can be used  like a flyweight alternative to caching and session management (and for some other things).

Here is a method that implements StackBuilder:

protected function getStackedClient()
 {
  $sessionReject = $this->bound('session.reject') ? $this['session.reject'] : null;

  $client = with(new \Stack\Builder)
      ->push('Illuminate\Cookie\Guard', $this['encrypter'])
      ->push('Illuminate\Cookie\Queue', $this['cookie'])
      ->push('Illuminate\Session\Middleware', $this['session'], $sessionReject);

  $this->mergeCustomMiddlewares($client);

  return $client->resolve($this);
 }


StackBuilder andLaravel Application class implements Symfony\HttpKernelInterface in the form of handle method. That is why it is called stack - it just forwards request further down the stack till it reaches main Application handler

When request hits Application handler, it is on the door of MVC part of Laravel, which leads further to domain logic and the heart of application.

Take a look at the main handler:

public function handle(SymfonyRequest $request, $type = HttpKernelInterface::MASTER_REQUEST, $catch = true)
 {
  try
  {
   $this->refreshRequest($request = Request::createFromBase($request));

   $this->boot();

   return $this->dispatch($request);
  }
  catch (\Exception $e)
  {
   if ($this->runningUnitTests()) throw $e;

   return $this['exception']->handleException($e);
  }
 }


As can be seen, it is dispatched to router, for further processing and which will result with HTTP response.   For now I wont go deeper into source code, since point of this post was just to show conceptual boundary between these two layers. This boundary is sometimes not so visible, due to frameworks specific implementations. One thing that is worth mentioning is that client/server model is dominant model in distributed systems like internet (where client is present), while MVC originated in the deskop environment. If not designed properly, applications made with the mix of these patterns can become very complex and lead to bugs and potential break. For me, it was very important to reckognize them in Laravel, so I could have better insight into awesome framework design.

Thanks for reading.

2/03/2014

Laravel sandwich

This morning, while reading  my favourite PHP frameworks code, it occured to me that Laravel application looks like one big, yummy sandwich. Well, that may sound weird but sometimes while coding, language concepts are presented in my mind like something that have nothing to do with programming. Or it does ?

So Laravel sandwich like every other is consists of three main parts:
  • top piece of bread (Application is called)
  • yummy layer (all good things like config, routes,controllers,models...)
  • bottom piece of bread (return what the client was asking)

When we transform these food terms into programming we got this:

//top piece
        $app = new Illuminate\Foundation\Application;

       // what was called
 public function __construct(Request $request = null)
 {
    
  $this->registerBaseBindings($request ?: $this->createNewRequest());
   
  $this->registerBaseServiceProviders();

  $this->registerBaseMiddlewares();
 }

As you can see above, app is called with provided request - which is little later registered into container. I was wondering why request is not registered as service provider - now I now . Request has to come first, before any bindings.
And that's all, belive it or not. Nothing fancy is happening here. Just some administrative tasks in the form of registering request into application. Yes, behind scene Symfony is doing a lot work while receving and preparing HTTP request, but it is not important for now.

Yummy part lives in the middle of our sandwich, and presents most delicious layer of Laravel delicacy. In fact we can say that this layer is most important in whole Laravel application. Here we can find all calls to routes, controllers, models, db - in other words it is the heart of domain layer, mean machine that has to be controlled somehow and from somewhere. If you don't understand what do I mean when I talk about "yummy layer" take a look at the content of these folders and files:
app/
        commands/
        config/
        controllers/
        database/
        lang/
        models/
        start/
        storage/
        tests/
        views/
        filters.php
        routes.php
You will be looking in the heart of nuclear reactor. That power needs some kind of control, or it will blow away everything. So let's push the red button. Or, let's eat our sandwich. We can do that with this method:

App::run();

        //in other words

 public function run(SymfonyRequest $request = null)
 {
  $request = $request ?: $this['request'];

        // calls dispatch route method
  $response = with($stack = $this->getStackedClient())->handle($request);

  $response->send();

  $stack->terminate($request, $response);
 }

Not so obvious ah ? Well I hope I will visit Laracon one day and thanks Taylor Otwell for this awesome piece of software that we enjoy somuch. Till next post, try to dig deeper into Laravel source code from App:run() point - there is a lot things inside.

I hope you like my post, and sorry for beeing too abstract sometimes.

2/01/2014

Laravel environments setup

Why does it matters ?

Ability to have multiple environments and to switch between them quickly is of big importance for any non-trivial web development task.
For example, if you test your application data layer, test code shouldn't be hitting real database tables in any case. Instead it should rely on mocked and prototyped tables that is easy to create and drop.
Laravel makes that process very easy by offering three main types of environments:

  1. development
  2. production
  3. testing
How to choose environment ?

Entry point for choosing is located in bootstrap/start.php file in the root folder of Laravel project, at ine 29 (Laravel version 4.1):

Original function looks like this:

$env = $app->detectEnvironment(array(
 'local' => array('your-machine-name'),
));


As can be seen, env is detected by providing array with env name and value in the form of key-value pair.

My custom version looks like this:
$env = $app->detectEnvironment(function()
{
    return getenv("ENV") ? : "local";
});


Instead of array, this function accepts function(closure) that returns type of env. That type will be auto deteced with the use of ternary operator.

How to setup environment ?

By just choosing, we didn't do much. We have to customize desired env and tell Laravel when to find them. All env settings are stored under app/config directory.  Default env is production, so if you don't create new, Laravel will treat all config settings as production. Easy task of setting custom env is done by just creating new folder under app/config directory. So if you want to have one env for development and one for production, just create new folder and name it development. Now all that we place inside will be red by Laravel only in case we explicitly choose in bootstrap/start.php file.  Note that development is just a convention, I instead name my development env as local - It is easier that way for me to differentiate. Name is not important, as long as it is in sync with name of the folder and vice verse. Most common reason to have different env are a database settings. By default, they are stored in app/config/database.php file. 

Here is my  database.php file, placed under app/local/ directory:
array(
  'mysql' => array(
   'driver'    => 'mysql',
   'host'      => 'localhost',
   'database'  => 'local-db',
   'username'  => 'root',
   'password'  => '123456',
   'charset'   => 'utf8',
   'collation' => 'utf8_unicode_ci',
   'prefix'    => ''
        )
)];


These setting works under local environment  common ( apache, localhost, mysql configuration ).

 And here are production settings: 
array(
  'mysql' => array(
   'driver'    => 'mysql',
   'host' => getenv("DB_HOST"),
   'database' => getenv("DB_NAME"),
   'username' => getenv("DB_USER"), 
   'password' => getenv("DB_PASSWORD"),
   'charset'   => 'utf8',
   'collation' => 'utf8_unicode_ci',
   'prefix'    => ''
      )
)];


Note that in order for these production values to have affect, you'l have to "catch" env variables that are specific to server environment.
Here I simply import them with native PHP getenv function.

Last and not so commonly used is testing environment. It is provides automatically by Laravel, so you don't have to worry about that. 

Here is how Laravel detects if we are using testing env:

public function createApplication()
 {
  $unitTesting = true;

  $testEnvironment = 'testing';

  return require __DIR__.'/../../bootstrap/start.php';
 }

This is excerpt from TestCase class which extends PHPUnit_Framework_TestCase and is locate under app/tests directory.
Again, in order this to work, we have to create new testing directory and place all out settings inside. One gotcha when migrating database table for testing purposes is to forgot to tell Laravel the type of env we want that table to be used for. If don't, Laravel will be using default environmet. 
To choose right type of env it is enough to add --env="testing" at the end of migration command like this:

php artisan migrate --env="testing"

It can be used for local/development purposes also.
php artisan migrate --env="local"

Thanks for reading.

12/16/2013

Dissecting Laravel Application class



This class along with core framework represents cornerstone of internal Laravel machinery. In the spirit of good software design it is responsible for one thing. This class, speaking from the highest level is conducting everything that exist in the framework core. It provides life to all service providers. But, underneath the simple surface we can find very sophisticated design with complex relationship to and from client and framework. Let’s take a peak inside.

This class is instantiated very early in application life cycle. It happens in start.php file under bootstrap/ folder . And it is initiated by client request. We already can see that it has direct relationship with HTTP protocol and transport layer. To proof take a look at Applicaton constructor - it is expecting Request object as it’s argument:

//
public function __construct(Request $request = null)
{
$this->registerBaseBindings ($request Request::createFromGlobals(

$this->registerBaseServiceProviders();

$this->registerBaseMiddlewares();
}
//

In case there is no explicit Request, constructor will fetch request data from globals. It is internally done by Symfony request class - which is used by Application. Alongside Symfony request class , our Application has relationships with 17 classes. We can see that Application is simply using those classes which are represented as service providers. They create a compositional relationship. On the other hand, our Application is extending Container class (in other words becomes a container ) and implements three interfaces. So already it can become a mass if the Laravel developers didn’t provide a way to organize those relations . It is done through Facade design pattern and Container bindings and resolving of service providers. These patterns deserves a dedicated posts, so let’s get back to more abstract view. One great geeky detail is that Application instance is registered to itself as a service provider in foundation/start.php file.

Application class contains 48 public and 13 protected methods. Those methods can be seen as implementation of Application responsibility to conduct all service providers in a consistent way. Most of ( but not all) Application responsibility is concetrated around service providers and HTTP request. It is not surprising since considering how important are those areas.

At the end of this brief overview try to imagine how would it be to create “headless” Laravel application or application without application ? Download core framework from the github and take a look at tests directory. Creator of Laravel did just that, by testing and simulating real environment - without app layer. Application class is present, but not outside of framework. It is instantiated from the tests. In my opinion developing in this way ( with tests ) is what is separating a real web-programmer from the rest of the world :)

Thanks for reading.

12/15/2013

Laravel architecture layers

Let's talk about Laravel structure. What is it and why having good structure is important - in terms of built in framework architecture.

In this post, I will categorize some Laravel layers as architectural. It may sometimes clash with classic definition of architecture, but it helps me to build good mental model in order to understand framework.

From the highest level, around and inside of Laravel we can reckognize:
  1. Client-server architecture
  2. Laravel specific micro-architecture ( my term )
  3. MVC (model, view, controller)

Client-server architecture lives on a level that is not specific to Laravel, but is implied by the framework. You can implement client-server architecture without Laravel, but opposite is not possible. Web as distributed system uses HTTP protocol as a transport layer to provide content to clients in the client-server model. It is true without any server side processing. Web 1.0 or static web is perfect example. Client-server model as such ( with transport protocol ) underlines whole Laravel implementations. It is implied that we will be writing applications that uses it. Further, we can say that client-server model with HTTP is a kind of edge layer to framework, which simply passes data to framework and give back response in the form of HTML document ( mostly ). Everything else is happening inside  framework - in  another Laravel specific layer. Point of entry to Laravel from client request is a public folder, which is read by server. After Laravel reckognize that request, it will forward data along with path and headers to something that I call Laravel micro-layer. HTTP request is indeed very simple, and there is nothing complicated with it. Best way to think about client-server model is as a transport mechanism that delivers data to some address.

Laravel specific micro-architecture is a middle layer that ties all pieces of framework together and connects client with Laravel. It is represented inside app folder structure. For me, best way to think about this layer is as  internal command-line for Laravel implementation. If you think, it acts like a CLI by calling core API and organizing service providers into meaningful structure. It is possible to interact with core without app layer, but that is not very useful if you are not writing tests. So app layer is a front-end layer of two part internal Laravel architecture. It becomes obvious whan you download core framework from github, without application. Developers spend most of their time in app layer, since it is made for interactiong with Laravel internals. App layer is at the deeper level represented in the form of Application class - a class that server as a glue for all service providers.
Think about service providers as a row power for your application, that comes in many forms. That power should be used in a way that is appropriate for you app. Service providers have their own responsibilities and are already ready to use. Thinking about providers leads us to next layer.

MVC (model, view, controller) is a well known architectural pattern for separating view from data (model). It is implemented in a Laravel through Model, View and Controler folders in application along with their associated classes. As you can guess, MVC classes are provided to application like a service-providers. MVC providers should be considered as primary points of organization and interaction inside application. Everything else is related to MVC. That's why there are dedicated MVC folders in application. If client-server model is responsible for physical separation ( or n-tiered ) MVC represent logical division inside application. Client-server model is exposed to clients while MVC is exposed to developers. All other providers are directly of indirectly related to MVC. For example session, cookies, filesystem, cache, database - these are all persistance mechanisms that finds their place in MVC designed Laravel application. In fact MVC is dominant pattern in the web frameworks, so it is not strange that Laravel is implementating it in its own way.

Everything else beneath MVC and overall architecture belongs to design decisions - how to connect or interact with providers, or how to extend their functionallity. Architecture establishes common structure in which design is possible. With Laravel this is represented through layers described above. I think that it is crucial for serious Laravel developer to become familiar with this layers, in order to master this awesome framework.

Thanks for reading.


12/14/2013

How I moved to PHP thanks to Laravel

Structure is one of the area that brought me a lot of pain while learning programming.

 If I tell you that my first language was Javascript, you'll understand why that's the case. I don't have anything against JS, I love and use that language daily. It had awesome brain-teasing mix of paradigms. Problem is ( as noticed by many developers ) that JS is lacking a good structure. It can especially confuse someone who is introduced to programming with Javascript as a first language ( like me ). With JS you are launhed straight into orbit of closures, prototypes, mutability and incredible dynamism.

That's why, in one moment of my career  I decided to move to some more traditional and better structured language. PHP was somehow natural choice, since I am a web developer. In the beginning ( while PHP was mostly procedural and represented by Wordpress and family ) I couldn't quite grasp it. I was a experienced JS commando that used to shoot with first class functions and throw objects around freely. So PHP wasn't a good choice at that time. And then came newer version of PHP that introduced support for object-oriented and functional  paradigm (a among other goodies ).

Along with the PHP transformation Laravel was born, and I was hooked. I had finally language and framework in which I can approach web development in the way I want ( and programming in general ).  So Laravel is responsible for my PHP adventure. And not just PHP. My knowledge about obligatory design patterns and whole object-oriented paradigm was missing some glue. Sure, thanks to previous experience with JS, in which I had to read about whole programming history and evolution of modern languages, I can flow between PHP code with no real struggle.

And of course, beautifully structured and designed Laravel is a joy to read without writing a single line of code - like a great novel. Class bases languages and frameworks like Laravel helps me to write better organized and more secure code - which is standard for server side applications. So my approach to Laravel is not just from the point of someone who wants to write web-applications and learn API. Through Laravel, I am making progress as a programmer, and I hope I'll be able to contribute to Laravel core soon :)

So this post somehow represents a long introduction to my future writings about Laravel internals, so stay tuned and thanks for reading.


10/22/2013

REST cheat sheet

I created a simple cheasheet table, like every day reminder for creating beautifull rest API-s. So, if you have experience with old PHP routing style in the form of:
"http://host/resource /?id=some_id"

it is time to refresh your skills by moving to more modern approach. Fortunately, REST (Representational state transfer) is supported in all new frameworks like ExpressJS or PHP Laravel.
So this table can help because it can be sometimes confusing to remember which verb belongs to which path.

Here it is:


Thanks for visiting my blog.
Miro

10/21/2013

How to properly understand Javascript language PART 1 ?

Hello;

My first programming language was Javascript. Learning JS without previos knowledge of programming theory ( paradigms, history and language background ) was a very painful process, full of pitfalls and roaming in the dark.  As I can see, a lot of people had a same experience. From my point of view it is because  in JS it is relatively  easy to write applications that looks like a real applications - with the help of large number of  libraries that provides usefull abstractions in the form of their "easy to learn" API.  Canonical example is jQeury - with a minimal code you can have reference to DOM object, without knowing what is happening in the back:

    //
    var elem = $("div")
    //

I am not saying that abstractions are evil, and that we should avoid them. It should be a nightmare to write code without them. After all with the help of abstractions, we are not writing assemby anymore, or in the case of jQuery, cool animations. I want to say that when learning language  starting with libraries like jQuery can cause  unnecessary confusion, which is hard to correct.

This is even more the case with JS, because of it‘s weird combinations of paradigms and idioms. For example, concept of closure  has become trademark of a language, that is very hard to pick without a knowledge about its context, root and origin. To proof, term closure is one of the most searched programming term online, with blogs and even whole sites dedicated to it.
People are really having problem to digest closure. It is not a surprise, because it is one of the hardest programming concept that exist.

After years of struggling with JS I created my mental model about what is Javascript. Of course, it is not just a theory. I am a programmer and the whole point of this post is to better understand Javascript so I can express my self with code.

So lets examine some of the main bulding blocks of JS.

I‘ll start with syntax, since it is very natural to start with when learning new language. Javascript belongs to so called "curly-braces" family of languages, which is mostly influenced by C.
Take a look at the following examples:
    //
    var i;
    for ( i = 0; i < 5, i++){
          console.log("hello");
    };
    int i;
    for ( i = 0; i < 5, i++){
          printf("%s\n","hello");
    };
    //

These two examples are identical, except one cosmetic difference. To write to output, Javascript uses console.log method, which is specific to browser environment, and C uses printf. Their syntatic similarity can also be seen  through code formating that I use in this blog. Both loops are formatted using same pre tag ( <pre class = "brush:js">), which is JS specific. But the output is still well formed, except some minor color differences.  Looking deeper in the languages syntax, there is very, very close relationship between them. I don't want to spend too much time dealing with that topic, since it is covered in more details on other places. To start with syntax comparison read a great Wiki articles: Comparison of programming languages syntax and List of programming languages by type.

The main takeaway from this is: Take some to learn basis of C programming language.  There are tons of great resources and tutorials online. You'll be surprised how much is Javascript influenced by C. In the beginning that influence can be seen as only syntatical, but with experience you'll see how much  have you missed. I became enlightened after learning C.

And that is just a beginning. In the future posts I'll cover other important Javascript  paradigms - first object and then functional.

Miro.


9/16/2013

Linux system calls - fork,exec and pipes.

These days I am passionate about network programming concepts, in order to fully understand Nodejs stack.
I created a small program to demonstrate canonical usage of basic linux system calls - fork, exec and pipes. These calls are used in most network applications and servers. It is heavily commented. Sorry for the code formatting, it is syntax highlighter issue.

#include
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 


// canonical example of fork,pipe and exec.
// it is heavily commented

// main program
int main (int argc, char ** args)
{
    // pipe
    int p[2];
    char buf[5]="\0";
    int pid;

    // do we have a fork ?
    if (!fork()){
        // write to one end of pipe
        write(p[1],"Child writes to pipe\n",22);

        // open a file
        int fd = open("one.js",O_RDWR);
        // read from that file
        read(fd,buf,70);

        //close file descriptor
        close(1);
        // replicate closed descriptro
        int i = dup(0);
        // print usefull message
        printf("Value of fd is: %d\n",i);

        // open new program, and provide some arguments
        execlp("./one","one",buf,NULL);

        // don't forget to exit child process and close open file descriptor
        exit(0);
        close(fd);
    }else{
        printf("Parent reading pipe");
        // read from a pipe
        read(p[0],buf,5);
        // print a pipe content
        printf("%s\n",buf);
        // wait for a child to exit
        wait(NULL);
    }
   return 0;
}
//

Thanx for visiting my blog
Happy Coding :)

9/05/2013

Network programming with sockets

I have to admit, I've moved away from Javascript , and I am spending most of my time in C/C++ together with Unix/Linux programming.
One of awesome things in compiled land :) are native kernel support for socket interface. In case you don't know what are sockets check out this link. Sockets are the basis of Internet, and It is very usefull to become familiar with them. Socket header files are written in C, but there are implementations for all modern languages. Sockets represents low level layer of network stack.
This is my simple implementation of server socket, written in C:

//
#include 
#include 
#include 
#include 
#include 
#include 
#include 

const char message[] = "Yuhuuuuu. Hello From The Server\n";
int main(int argc, char *argv[]) {

    int simpleSocket = 0;
    int simplePort = 0;
    int returnStatus = 0;
    struct sockaddr_in simpleServer;

    if (2 != argc) {

        fprintf(stderr, "Usage: %s \n", argv[0]);
        exit(1);

    }

    printf("%s\%s\n","Listening of port: ",argv[1]);

    simpleSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);

    if (simpleSocket == -1) {

        fprintf(stderr, "Could not create a socket!\n");
        exit(1);

    }
    else {
     fprintf(stderr, "Socket created!\n");
    }

    /* retrieve the port number for listening */
    simplePort = atoi(argv[1]);

    /* setup the address structure */
    /* use INADDR_ANY to bind to all local addresses  */
    bzero(&simpleServer, sizeof(simpleServer));
    simpleServer.sin_family = AF_INET;
    simpleServer.sin_addr.s_addr = htonl(INADDR_ANY);
    simpleServer.sin_port = htons(simplePort);

    /*  bind to the address and port with our socket  */
    returnStatus = bind(simpleSocket,(struct sockaddr *)&simpleServer,sizeof(simpleServer));

    if (returnStatus == 0) {
     fprintf(stderr, "Bind completed!\n");
    }
    else {
        fprintf(stderr, "Could not bind to address!\n");
 close(simpleSocket);
 exit(1);
    }

    /* lets listen on the socket for connections      */
    returnStatus = listen(simpleSocket, 5);

    if (returnStatus == -1) {
        fprintf(stderr, "Cannot listen on socket!\n");
 close(simpleSocket);
        exit(1);
    }

    while (1)

    {

        struct sockaddr_in clientName = { 0 };
 int simpleChildSocket = 0;
 int clientNameLength = sizeof(clientName);

 /* wait here */

        simpleChildSocket = accept(simpleSocket,(struct sockaddr *)&clientName, &clientNameLength);

 if (simpleChildSocket == -1) {

            fprintf(stderr, "Cannot accept connections!\n");
     close(simpleSocket);
     exit(1);

 }

        /* handle the new connection request  */
 /* write out our message to the client */
 write(simpleChildSocket, message, strlen(message));
        close(simpleChildSocket);

    }

    close(simpleSocket);
    return 0;
}
//
Happy Coding.

8/17/2013

Bootstrap data with Jade templating engine

Jade is templating engine closely related to expressjs. It is not a surprise, since jade is created by a same author. Jade is heavily influenced by syntax of Ruby and Haml.
There are lot of great features in Jade, that makes dealing with compex views on server side really  smooth. One of them is ability to transfer data directly to html ( jade file ), without additional request.
Let me explain that more. We know that HTTP request are expensive. So it is a good pattern to initially load as more content as possible,  so we don't have to make new requests to server. That is  a winning paradigm that  makes single page web applications so powerful on the client side.  On the client, simply load all the view once and make Ajax request for data if needed.  Since Jade is rendered on the server, this approach is especially useful when application needs data from database for the first load.

With Jade we can easily bootstrap data like this:

//
//here data is plain js object
var data = { "name": "myApp","version":0.0.0,"db":"mongodb"};

// now just render existing view, with data object attached   
app.get("/",function(req,res){
    res.render("home",{obj:data}) 
//

In the previous example data object  was local, contained in the same file as main app file. When we want  to bootstrap data from database, we take same approach when rendering jade template.
Again, data is attached to Jade template. The only difference is that we have to have access to database.

Here is example:
//
exports.queryDB = function (req,res) {
    db.collection("jazz",function (err,collection) {
        collection.find().toArray(function (err,collection) {
            res.render("jazz",{giants:collection});
        });
    });
};
//

Database example is a function that asynchronously query data, due to nature af nodejs native mongodb driver. But when we have data, we can manipulate with it with Jade syntax. For example, to iterate over data object and insert it in the html we can do this:

    // 
    ul
        each giant in giants
            li
                a(href="#{giant["name"]}") #{giant["name"]}
    //


Now we have nice list of jazz giants on our page, bootstrapped on first request.

Thanks for visiting my blog.

8/12/2013

Mongodb and underscorejs - the mean stack

I found a way to combine two very powerful toys that I am playing with :

Mongodb - noSQL database that is shaping web these days and Underscorejs  - which make me fell in love with functional programming.

If you don't know already, mongo is using document/oriented key/value way of storing data in JSON format ( BSON - binary JSON under the hood ).
Underscore has a tons of functions that works with literals like plain JS objects and arrays ( which are basic building blocks of mongo collection ). Also, underscore is environment agnostic. That means that it is not dependent on interfaces like DOM. It is important just to execute underscore functions under Javascripot interpreter. Mongo shell is actually javascript shell, so underscore fits great.

To import JS code in mongo, open your .mongorc.js file, that is located in /home/ directory ( Linux/MacOSx), and place any script inside. It can be whole library, like underscore. Now, from terminal, open mongo shell, and type load (".mongorc.js"). Now all code inside that file is avaliable to mongo documents.

Play yourself and have fun.

Thanks for visiting my blog.


8/11/2013

Design patterns refactored

I did some cleaning and refactoring of my implementations of classic design patterns in coffeescript.
Also I included some variations of existing patterns ( adapter, composite ). There is always more than one way in which we can do same thing ( especially with JS ).

Code is  here

Thanks for visiting my blog.

8/09/2013

Local Search Application

I wrote an application that will search through local data store. It can be easily modified to browse remote db also. By default is searches items in a wiki articles, and uses local array-object as storage.
It is written with the help of jQuery and Coffeescript.
I have to mention that it is not structured in object-oriented or modular way. This code is just a basic idea implemented quickly after inspiration. Inside of bigger application of maybe framework it definitly needs structure, optimization, testing and maybe refactoring.

Code can be found here:

Here is a screenshot:



Thanks for visiting my blog.