public function transform($input, &$output = '')
 {
     // Get all defined handlers
     $handlers = \samsonphp\event\Event::listeners();
     // Iterate all event fire calls
     foreach ($this->fires as $id => $data) {
         // Collection of actual event handler call for replacement
         $code = array();
         // Set pointer to event subscriptions collection
         if (isset($this->subscriptions[$id])) {
             // Iterate event subscriptions
             foreach ($this->subscriptions[$id] as &$event) {
                 $this->log('Analyzing event subscription[##]', $id);
                 // If subscriber callback is object method
                 if (isset($event['object'])) {
                     $eventHandlers =& $handlers[$id];
                     if (isset($eventHandlers)) {
                         // Iterate all handlers
                         foreach ($eventHandlers as $handler) {
                             $call = '';
                             // Get pointer to object
                             if (is_scalar($handler[0][0])) {
                                 $object = $handler[0][0];
                             } else {
                                 $object =& $handler[0][0];
                             }
                             // TODO: Not existing dynamic handlers what was excluded from compressed code
                             if (is_object($object) && $object instanceof \samson\core\iModule && $object instanceof \samson\core\iModuleCompressable) {
                                 // Build object method call
                                 $call = 'm("' . $object->id() . '")->' . $event['method'] . '(';
                                 $this->log('   - Replacing event fire[##] with object function call [##]', $id, $call);
                             } elseif (strpos($event['object'], '(') !== false) {
                                 // Function
                                 // Build object method call
                                 $call = $event['object'] . '->' . $event['method'] . '(';
                             } elseif (is_string($object) && class_exists($object, false)) {
                                 // Static class
                                 //trace($event['object'].'-'.$object);
                                 // Build object method call
                                 $call = $event['object'] . '::' . $event['method'] . '(';
                             }
                             // TODO: Define what to do with other classes, only functions supported
                             // If we have found correct object
                             if (isset($call[0])) {
                                 // Event fire passes parameters
                                 if (is_array($data['params'])) {
                                     $call .= implode(', ', $data['params']);
                                 }
                                 // Gather object calls
                                 $code[] = $call . ');';
                             } else {
                                 $this->log(' - Cannot replace event fire[##] with [##] - [##]', $id, $event['object'], $event['method']);
                             }
                         }
                     }
                 } else {
                     // Global function
                     if (strpos($event['method'], '$') === false) {
                         $call = $event['method'] . '(' . implode(', ', $data['params']) . ');';
                         $code[] = $call;
                         $this->log(' - Replacing event fire[##] with function call [##]', $id, $call);
                     } else {
                         $this->log('Cannot replace event fire method with [##] - variables not supported', $event['method']);
                     }
                 }
             }
             // Remove duplicates
             $code = array_unique($code);
             // Replace Event::fire call with actual handlers
             $input = str_replace($data['source'], implode("\n", $code), $input);
             // Logging changes in code
             foreach ($code as $replace) {
                 $this->log('Replacing [##] with [##]', $data['source'], $replace);
             }
         } else {
             // There is no subscriptions to this event fire
             // Remove Event::fire call without subscriptions
             $input = str_replace($data['source'], '', $input);
             $this->log('Removing event firing [##] as it has no subscriptions', $data['source']);
         }
     }
     // Remove all subscriptions from code
     $input = $this->removeSubscriptionCalls($this->subscriptions, $input);
     // Copy output
     $output = $input;
     return true;
 }
Exemple #2
0
 /** Test if listeners getter not empty after all tests */
 public function testListenersList()
 {
     $listeners = \samsonphp\event\Event::listeners();
     // Perform test
     $this->assertGreaterThan(0, $listeners);
 }