Development

/plugins/sfGearmanPlugin/trunk/lib/sfGearman.class.php

You must first sign up to be able to contribute.

root/plugins/sfGearmanPlugin/trunk/lib/sfGearman.class.php

Revision 33306, 4.0 kB (checked in by bicou, 1 year ago)

Allow to namespaces function, for mutualized gearmand
https://github.com/bicouy0/sfGearmanPlugin/pull/4

  • Property svn:mime-type set to text/x-php
  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Rev URL
Line 
1 <?php
2
3 /**
4  * sfGearman util class
5  *
6  * @abstract
7  * @package   sfGearmanPlugin
8  * @author    Benjamin VIELLARD <bicou@bicou.com>
9  * @license   The MIT License
10  * @version   SVN: $Id$
11  */
12 abstract class sfGearman
13 {
14   /**
15    * asynchronous tasks
16    *
17    * @var integer
18    */
19   const BACKGROUND = 1;
20
21   /**
22    * low priority tasks
23    *
24    * @var integer
25    */
26   const LOW = 2;
27
28   /**
29    * high priority tasks
30    *
31    * @var integer
32    */
33   const HIGH = 4;
34
35   /**
36    * configuration array read from gearman.yml
37    *
38    * @var mixed  Defaults to null.
39    */
40   public static $config = null;
41
42
43   /**
44    * worker configuration array by key name
45    *
46    * @param string $name gearman.yml key
47    *
48    * @return array
49    */
50   public static function getWorker($name)
51   {
52     if (isset(self::$config['worker'][$name]))
53     {
54       return self::$config['worker'][$name];
55     }
56
57     throw new sfConfigurationException(sprintf('sfGearmanPlugin worker config "%s" not found', $name));
58   }
59
60   /**
61    * Namespace to suffix function on a mutialized gearman
62    *
63    * @return string
64    */
65   public static function getNamespace()
66   {
67       return isset(self::$config['namespace']) ? self::$config['namespace'] : '';
68   }
69
70   /**
71    * doctrine worker configuration array by key name
72    *
73    * @param string $name gearman.yml key
74    *
75    * @return array
76    */
77   public static function getDoctrine($name)
78   {
79     if (isset(self::$config['doctrine'][$name]))
80     {
81       return self::$config['doctrine'][$name];
82     }
83
84     throw new sfConfigurationException(sprintf('sfGearmanPlugin doctrine worker config "%s" not found', $name));
85   }
86
87   /**
88    * Configure a gearman connection with parameters from gearman.yml
89    *
90    * @param sfGearmanWorker|sfGearmanClient $connection
91    * @param string              $server Optional, defaults to null.
92    *
93    * @return void
94    */
95   public static function setupConnection($connection, $server = null)
96   {
97     self::addServer($connection, self::getServer($server));
98   }
99
100   /**
101    * server configuration array by key name
102    *
103    * @param string $name gearman.yml key, optional, defaults to null
104    *
105    * @return array
106    */
107   public static function getServer($name = null)
108   {
109     // transform key to 'default' for array
110     $key = $name !== null ? $name : 'default';
111
112     if (isset(self::$config['server'][$key]))
113     {
114       return self::$config['server'][$key];
115     }
116
117     // not found but still null, return null (module defaults)
118     if ($name === null) return null;
119
120     throw new sfConfigurationException(sprintf('sfGearmanPlugin server config "%s" not found', $name));
121   }
122
123   /**
124    * Add servers to GearmanClient or GearmanWorker
125    *
126    * @param sfGearmanWorker|sfGearmanClient $connection
127    * @param mixed               $params
128    *
129    * @return void
130    */
131   public static function addServer($connection, $params)
132   {
133     if ($params === null)
134     {
135       // module defaults
136       $connection->addServer();
137     }
138     elseif (is_string($params))
139     {
140       // "host:port"
141       $connection->addServers($params);
142     }
143     elseif (is_array($params))
144     {
145       if (isset($params['host']))
146       {
147         if (isset($params['port']))
148         {
149           // {host: ..., port: ...}
150           $connection->addServer($params['host'], $params['port']);
151         }
152         else
153         {
154           // {host: ...}
155           $connection->addServer($params['host']);
156         }
157       }
158       else
159       {
160         // array of servers => iterate
161         foreach ($params as $param)
162         {
163           self::addServer($connection, $param);
164         }
165       }
166     }
167   }
168
169   /**
170    * serialize if needed
171    *
172    * @param mixed $data
173    * @static
174    * @access public
175    * @return void
176    */
177   public static function serialize($data)
178   {
179     return is_scalar($data) ? $data : serialize($data);
180   }
181
182   /**
183    * unserialize if needed
184    *
185    * @param mixed $data
186    * @static
187    * @access public
188    * @return void
189    */
190   public static function unserialize($data)
191   {
192     $r = @unserialize($data);
193     if ($r !== false or $data === 'b:0;')
194     {
195       return $r;
196     }
197     return $data;
198   }
199 }
200
201
Note: See TracBrowser for help on using the browser.