spacer

Engineering and
Construction

Tips and tricks, insights and thoughts about hosting and
software development by our technical teams.

spacer spacer spacer spacer

Recent Posts
  • Migration to Symfony2 continued
  • POC: Flexible PHP Output Caching
  • Risk-based testing
  • Painless (well, less painful) migration to Symfony2
  • Drawing with CSS3
Recent Comments
  • Leaseweb Labs Blog: Migration auf Symfony2 fortgesetzt | PHP Boutique on Migration to Symfony2 continued
  • Ronny Roethof on High availability load balancing using HAProxy on Ubuntu (part 1)
  • Imre Toth on POC: Flexible PHP Output Caching
  • Rogier Mulders on POC: Flexible PHP Output Caching
  • Leaseweb Labs Blog: POC: Flexible PHP Ausgabezwischenspeicherung | PHP Boutique on POC: Flexible PHP Output Caching
Archives
  • February 2012
  • January 2012
  • December 2011
  • November 2011
  • September 2011
  • August 2011
  • July 2011
Tags
autoit automation cache cassandra conference css css3 data database debug doctrine fitnesse gluster gui haproxy high availability hosting iphone iui javascript keepalived loadbalancing mobile mysql nosql oscon performance pfcongres php QA rational robot risk based testing scalability selenium software development sql storage subversion symfony symfony2 tdd test ubuntu video zend
Search blog
« POC: Flexible PHP Output Caching

Migration to Symfony2 continued

February 7, 2012 | Author Maurits van der Schee (Software Developer)

On December 21, 2011 Stefan Koopmanschap wrote an excellent article on this blog titled “Painless (well, less painful) migration to Symfony2.” In his article he explained the advantages of doing a gradual migration. The technical solution that he proposed to make this possible was to “…wrap your old application into your Symfony2 application.” He even provided us the tool (The IngewikkeldWrapperBundle code) to do so.

We were very much inspired by his passionate elucidation and we were fully convinced of the urge to start migrating to Symfony2 as soon as possible. However, he also provided us with a “A word of caution” about 2 things: performance and authentication/authorization. This might get some people worried, but not us: it challenged us to find a solution for those two open issues.

1. Performance

As Stefan Koopmanschap explains, in his solution you “…use two frameworks for all your legacy pages” and “…two frameworks add more overhead than one.” Since our Symfony1 application (the LeaseWeb Self Service Center) is not the fastest you’ve ever seen, some customers are even complaining about it’s speed, this got us a little worried. Losing performance was not really an option, so we had to find a solution.

Symfony 1 & 2 both use a Front Controller architecture (one file handling all requests) we were just looking for seperating traffic between the two front controllers. Stefan proposed to do so using Symfony 2 routing and make it use a fall-back route to handle your legacy URLs. We hereby propose to do it using a .htaccess rewrite. This has virtually no overhead, because every Symfony request gets rewritten by mod_rewrite anyway.

2. Authentication/authorization

He also wrote: “Another issue is sessions.” Further clarifying the problem by stating: “If your application works with authentication and authorization, you’ll now have to work with two different systems that have a different approach to authentication and authorization”. Since our application requires both authentication and authorization we had to come up with a solution here. We decided to move the authentication (login page) to Symfony2 and make Symfony1 “trust” this authentication done by “Symfony2”.

To realize this solution we had to enable Symfony1 to “see” and “understand” the Symfony2 session. First we made sure that both applications use the same name by setting the Symfony2 “framework_session_name” setting in “app/config/config.yml” to “symfony”. Then we reverse engineered the Symfony2 session storage and found that it serializes some PHP object into it. To be able to unserialize those objects we had to register an autoload function in Symfony1 using “spl_autoload_register”

Finally, instructions

To solve the performance problem we installed Symfony2 in the “sf2” directory inside the Symfony1 project (next to “apps”) and we started by changing the lines in our “web/.htaccess” file from:

# redirect to front controller
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php [QSA,L]

And added these lines above it:

# redirect to new symfony2 front controller
RewriteCond %{REQUEST_FILENAME} !-f
# but only if URL matches one from this list:
RewriteCond %{REQUEST_URI} ^/users/login
# end of list
RewriteRule ^(.*)$ sf2/web/$1 [QSA,L]

To support the Symfony2 authentication and authorization in Symfony1 we created a “Symfony2AuthenticationFilter” class. This filter can be loaded by putting it under “lib/filter” folder in your Symfony1 project and add the following lines in “apps/ssc/config/filters.yml”:
symfony2AuthenticationFilter:

class: Symfony2AuthenticationFilter
For configuration of the filter we added a few new application settings to “/apps/ssc/config/app.yml”:

app_symfony2_path: 'sf2/vendor/symfony/src'
app_symfony2_attribute: '_security_secured_area'

This path setting shows that Symfony2 is located in the “sf2” sub-directory of the Symfony1 project. The attribute reflects the name of the Symfony2 firewall. The code of the Symfony2AuthenticationFilter is this:


<?php
function symfony2_autoload ($pClassName)
{
  $sf2Path = sfConfig::get('sf_root_dir').'/'.sfConfig::get('app_symfony2_path');
  $file = str_replace('\\', DIRECTORY_SEPARATOR ,$pClassName ) . ".php";
  include($sf2Path . DIRECTORY_SEPARATOR . $file);
}
spl_autoload_register("symfony2_autoload");

use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
use Symfony\Component\Security\Core\User\User;
use Symfony\Component\Security\Core\Role\Role;

class Symfony2AuthenticationFilter extends sfFilter
{
  public function execute($filterChain)
  {
  	$symfony2Attribute = sfConfig::get('app_symfony2_attribute');
  	if (isset($_SESSION['_symfony2']['attributes'][$symfony2Attribute]))
    {
    	$sessionData = unserialize($_SESSION['_symfony2']['attributes'][$symfony2Attribute]);
      $sf2UserName = $sessionData->getUser()->getUserName();
      $sf1UserName = $this->getContext()->getUser()->getAttribute('userName');
      if (!$this->getContext()->getUser()->isAuthenticated() || $sf1UserNumber!=$sf2UserNumber)
      {
      	$customer = Doctrine::getTable("Customer")->findOneByCustomerNumber($sf2UserNumber);
        if ($customer)
        {
          $this->getContext()->getUser()->setAuthenticated(true);
          $this->getContext()->getUser()->setAttribute('userName', $sf2UserNumber);
        }
      }
    }
    elseif ($this->getContext()->getUser()->isAuthenticated())
    {
    	$this->getContext()->getUser()->clear();
    	$path = $this->getContext()->getRequest()->getPathInfo();
      $this->getContext()->getController()->redirect($path);
    } 

    // Execute next filter in the chain
    $filterChain->execute();
  }

}

?>
  • Share
  • Tweet
  • &title=">
Tags: performance, php, security, symfony, symfony2

One Response to “Migration to Symfony2 continued”

  • Leaseweb Labs Blog: Migration auf Symfony2 fortgesetzt | PHP Boutique:
    February 10, 2012 at 5:37 am

    [...] von Posts, Stefan Koopmanschap über Umhüllung Ihren Code, um ihn gesprochen zu arbeiten . In diese zweite post , Maurtis van der Schee packt zwei Fragen Stefan genannt – Performance-Probleme und [...]

Leave a Reply

Click here to cancel reply.

« POC: Flexible PHP Output Caching
© 2011 LeaseWeb B.V.

gipoco.com is neither affiliated with the authors of this page nor responsible for its contents. This is a safe-cache copy of the original web site.