Item.php

00001 <?php
00009 class Dsao_GarbageCollector_Item
00010 {
00014   const PROBABILITY_MAX = 1000;
00015 
00019   protected $_method;
00020 
00024   protected $_object = null;
00025 
00029   protected $_parameters = array();
00030 
00034   protected $_probability = 10;
00035 
00047   public function __construct($method = 'collectGarbage', $object = null, $probability = 10)
00048   {
00049     if (is_array($method))
00050     {
00051       $item = $method;
00052 
00053       // Falls keine Methode übergeben wurde, Exception werfen
00054       if (!isset($item['method']))
00055       {
00056         throw new Dsao_Exception('dsao_garbagecollector_item_invalid');
00057       }
00058 
00059       $method = (string) $item['method'];
00060 
00061       $object = (isset($item['object']) ? $item['object'] : $object);
00062 
00063       $probability = (isset($item['probability']) ? $item['probability'] :
00064         $probability);
00065     }
00066 
00067     $this->_method = $method;
00068 
00069     $this->_object = $object;
00070 
00071     // Falls Wahrscheinlichkeit übergeben wurde und gültig
00072     if (isset($probability) && is_numeric($probability))
00073     {
00074       $this->_probability = $probability;
00075     }
00076   }
00077 
00083   public function run()
00084   {
00085     $random = mt_rand(1, self::PROBABILITY_MAX);
00086 
00087     // Sofern Zufallszahl größer als Wahrscheinlichkeit, abbrechen
00088     if ($random > $this->_probability)
00089     {
00090       return;
00091     }
00092 
00093     // Falls Objekt übergeben wurde
00094     if (null != $this->_object)
00095     {
00096       // Falls kein Objekt, aus Registry laden / initialisieren lassen
00097       if (!is_object($this->_object))
00098       {
00099         $this->_object = Dsao_Registry::get($this->_object);
00100       }
00101     }
00102 
00103     // Falls kein Objekt vorhanden und Methode ungültig, oder Methode des
00104     // Objekts nicht vorhanden, Exception werfen
00105     if ((null === $this->_object && !function_exists($this->_method))
00106         || !method_exists($this->_object, $this->_method))
00107     {
00108         throw new Dsao_Exception('dsao_garbagecollector_item_invalid');
00109     }
00110 
00111     // Falls kein Objekt vorhanden, statische Methode / Funktion ausführen
00112     if (null === $this->_object)
00113     {
00114       call_user_func_array($this->_method, (array) $this->_parameters);
00115     }
00116     // Ansonsten Methode des Objekts ausführen
00117     else
00118     {
00119       call_user_func_array
00120         (array($this->_object, $this->_method), (array) $this->_parameters);
00121     }
00122 
00123     return $this;
00124   }
00125 }

Erzeugt am Fri Sep 18 19:04:11 2009 für DSA online - Morgendaemmerung von  doxygen 1.5.7.1