Development

/branches/1.0/lib/addon/propel/sfPropelBehavior.class.php

You must first sign up to be able to contribute.

root/branches/1.0/lib/addon/propel/sfPropelBehavior.class.php

Revision 2453, 2.2 kB (checked in by fabien, 8 years ago)

added behaviors to Propel builder classes (this allows adding new behaviors to Propel generated classes at runtime)

  • Property svn:mime-type set to text/x-php
  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
Line 
1 <?php
2
3 /*
4  * This file is part of the symfony package.
5  * (c) 2004-2006 Fabien Potencier <fabien.potencier@symfony-project.com>
6  *
7  * For the full copyright and license information, please view the LICENSE
8  * file that was distributed with this source code.
9  */
10
11 /**
12  * @package    symfony
13  * @subpackage addon
14  * @author     Fabien Potencier <fabien.potencier@symfony-project.com>
15  * @version    SVN: $Id$
16  */
17 class sfPropelBehavior
18 {
19   static protected $behaviors = array();
20
21   static public function registerMethods($name, $callables)
22   {
23     if (!isset(self::$behaviors[$name]))
24     {
25       self::$behaviors[$name] = array('methods' => array(), 'hooks' => array());
26     }
27     foreach ($callables as $callable)
28     {
29       self::$behaviors[$name]['methods'][] = $callable;
30     }
31   }
32
33   static public function registerHooks($name, $hooks)
34   {
35     if (!isset(self::$behaviors[$name]))
36     {
37       self::$behaviors[$name] = array('methods' => array(), 'hooks' => array());
38     }
39     foreach ($hooks as $hook => $callable)
40     {
41       if (!isset(self::$behaviors[$name]['hooks']))
42       {
43         self::$behaviors[$name]['hooks'][$hook] = array();
44       }
45
46       self::$behaviors[$name]['hooks'][$hook][] = $callable;
47     }
48   }
49
50   static public function add($class, $behaviors)
51   {
52     foreach ($behaviors as $name => $parameters)
53     {
54       if (is_int($name))
55       {
56         // no parameters
57         $name = $parameters;
58       }
59       else
60       {
61         // register parameters
62         foreach ($parameters as $key => $value)
63         {
64           sfConfig::set('propel_behavior_'.$name.'_'.$class.'_'.$key, $value);
65         }
66       }
67
68       if (!isset(self::$behaviors[$name]))
69       {
70         throw new sfConfigurationException(sprintf('Propel behavior "%s" is not registered', $name));
71       }
72
73       // register hooks
74       foreach (self::$behaviors[$name]['hooks'] as $hook => $callables)
75       {
76         foreach ($callables as $callable)
77         {
78           sfMixer::register('Base'.$class.$hook, $callable);
79         }
80       }
81
82       // register new methods
83       foreach (self::$behaviors[$name]['methods'] as $callable)
84       {
85         sfMixer::register('Base'.$class, $callable);
86       }
87     }
88   }
89 }
90
Note: See TracBrowser for help on using the browser.