My favorites | Sign in
Project Home Downloads Wiki Issues Source
Project Information
Members
Links

sfIocPlugin - IoC container for Symfony framework

Description

sfIocPlugin implements IoC container in PHP and provide easy integration with Symfony Framework.

Plugin is inspired by Spring framework IoC container (http://www.springframework.org/), but IS NOT AN ONE-TO-ONE COPY.

IoC container is useful for business logic layer implementations and adds more flexibility to your applications.

Features

This plugin is a very basic implementation of IoC, but it is enough for solving typical architecture problems.

  • annotations support (easy bean configuration by annotations for classes and methods - no need for configuring all parameters in config files)
  • support for dependency injection (currently only for construct())
  • implements singleton pattern (there is no need for using boring getInstance() methods)
  • simple usage
  • configurable with YAML files

Requirements

  • Symfony 1.0.x or 1.1.x
  • sfAnnotationPlugin

Example

  /*
   * @Singleton
   /
  class UserLogic
  {
     /* 
      * @Inject
      */

     public function __construct(DataFinder $finder)
     {
        $this->finder = $finder;
     }

     public function authorize($login, $password)
     {
        $is_auth = $this->finder
           ->from('UserAccount')
           ->where('login',$login)
           ->where('password'=>$password)
           ->count() > 0;

        if($is_auth)
        {
           // authorize
        }

        return $is_auth;
     }
  }

Bean configuration:

  
all: # for all environments

  UserLogic: { class: DefaultUserLogic }
  DataFinder: { class: DBFinder }

test: # for test environment

  DataFinder: { class: testFinder }  # redeclare DataFinder in test env for unit testing without database

Calling bean methods:

   $api = sfContext::getInstance()->get('DefaultBeanFactory')->getBean('UserLogic');
   if($api->authorize('john_doe', 'secure'))
   {
     // secure stuff here
   }
Powered by Google Project Hosting