Subversion-Projekte lars-tiefland.php_share

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
1 lars 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
 * Manages behaviors for propel. Implements hooks for propel objects.
13
 *
14
 * @package    symfony
15
 * @subpackage propel
16
 * @author     Fabien Potencier <fabien.potencier@symfony-project.com>
17
 * @version    SVN: $Id: sfPropelBehavior.class.php 12809 2008-11-09 09:14:58Z fabien $
18
 */
19
class sfPropelBehavior
20
{
21
  static protected
22
    $loaded    = array(),
23
    $behaviors = array();
24
 
25
  static public function registerMethods($name, $callables)
26
  {
27
    if (!isset(self::$behaviors[$name]))
28
    {
29
      self::$behaviors[$name] = array('methods' => array(), 'hooks' => array());
30
    }
31
    foreach ($callables as $callable)
32
    {
33
      self::$behaviors[$name]['methods'][] = $callable;
34
    }
35
  }
36
 
37
  static public function registerHooks($name, $hooks)
38
  {
39
    if (!isset(self::$behaviors[$name]))
40
    {
41
      self::$behaviors[$name] = array('methods' => array(), 'hooks' => array());
42
    }
43
    foreach ($hooks as $hook => $callable)
44
    {
45
      if (!isset(self::$behaviors[$name]['hooks']))
46
      {
47
        self::$behaviors[$name]['hooks'][$hook] = array();
48
      }
49
 
50
      self::$behaviors[$name]['hooks'][$hook][] = $callable;
51
    }
52
  }
53
 
54
  static public function add($class, $behaviors)
55
  {
56
    foreach ($behaviors as $name => $parameters)
57
    {
58
      if (is_int($name))
59
      {
60
        // no parameters
61
        $name = $parameters;
62
      }
63
      else
64
      {
65
        // register parameters
66
        foreach ($parameters as $key => $value)
67
        {
68
          sfConfig::set('propel_behavior_'.$name.'_'.$class.'_'.$key, $value);
69
        }
70
      }
71
 
72
      if (!isset(self::$behaviors[$name]))
73
      {
74
        throw new sfConfigurationException(sprintf('Propel behavior "%s" is not registered', $name));
75
      }
76
 
77
      // register hooks
78
      foreach (self::$behaviors[$name]['hooks'] as $hook => $callables)
79
      {
80
        foreach ($callables as $callable)
81
        {
82
          $key = 'Base'.$class.$hook.'//'.self::callableToString($callable);
83
          if (!isset(self::$loaded[$key]))
84
          {
85
            sfMixer::register('Base'.$class.$hook, $callable);
86
            self::$loaded[$key] = true;
87
          }
88
        }
89
      }
90
 
91
      // register new methods
92
      foreach (self::$behaviors[$name]['methods'] as $callable)
93
      {
94
        $key = 'Base'.$class.'//'.self::callableToString($callable);
95
        if (!isset(self::$loaded[$key]))
96
        {
97
          sfMixer::register('Base'.$class, $callable);
98
          self::$loaded[$key] = true;
99
        }
100
      }
101
    }
102
  }
103
 
104
  static protected function callableToString($callable)
105
  {
106
    return is_array($callable) ? (is_object($callable[0]) ? get_class($callable[0]) : $callable[0]).'::'.$callable[1] : var_export($callable, true);
107
  }
108
}