/** * Set cache. * * @param string $request The GET request * @param string $data The data to cache * * @return bool */ public final function set($request, $data) { if (!DISPATCHER_CACHE_ENABLED) { $this->logger->info('Caching administratively disabled'); return false; } if (empty($data)) { if ($this->fail_silently) { $this->logger->warning('Empty data, nothign to cache'); return false; } else { $this->logger->error('Empty data, nothign to cache'); throw new IOException("Nothing to cache"); } } $cacheTag = md5($request) . ".cache"; $cacheFile = $this->cache_path . ($this->cache_path[strlen($this->cache_path) - 1] == "/" ? "" : "/") . $cacheTag; $f_data = serialize($data); $cached = file_put_contents($cacheFile, $f_data); if ($cached === false) { $this->logger->error('Error writing to cache', array('CACHEFILE' => $cacheFile)); if ($this->fail_silently) { return false; } else { throw new IOException("Error writing to cache"); } } return true; }
/** * Fire an event * * @param string $event Event name * @param string $type the type of event * @param Object $data Data to provide to callback * * @return Object|null */ public final function fire($event, $type, $data) { $this->logger->info('Firing event', array('EVENT' => $event)); $value = $data; if (isset($this->hooks[$event])) { foreach ($this->hooks[$event] as $callback) { $return_value = null; if (is_array($callback)) { if (is_callable(array($callback[0], $callback[1]))) { try { $return_value = call_user_func(array($callback[0], $callback[1]), $value); } catch (Exception $e) { $this->logger->error('Hook error', array('EVENT' => $event, 'CALLBACK' => $callback[0], 'METHOD' => $callback[1], 'CODE' => $e->getCode(), 'MESSAGE' => $e->getMessage())); continue; } } else { $this->logger->warning('Skipping not-callable hook', array('EVENT' => $event, 'CALLBACK' => $callback[0], 'METHOD' => $callback[1])); continue; } } else { if (is_callable($callback)) { try { $return_value = call_user_func($callback, $value); } catch (Exception $e) { $this->logger->error('Hook error', array('EVENT' => $event, 'CALLBACK' => $callback, 'CODE' => $e->getCode(), 'MESSAGE' => $e->getMessage())); continue; } } else { $this->logger->warning('Skipping not-callable hook', array('EVENT' => $event, 'CALLBACK' => $callback)); continue; } } switch ($type) { case 'REQUEST': $value = $return_value instanceof \Comodojo\Dispatcher\ObjectRequest\ObjectRequest ? $return_value : $value; break; case 'TABLE': $value = $return_value instanceof \Comodojo\Dispatcher\ObjectRoutingTable\ObjectRoutingTable ? $return_value : $value; break; case 'ROUTE': $value = $return_value instanceof \Comodojo\Dispatcher\ObjectRoute\ObjectRoute ? $return_value : $value; break; case 'RESULT': $value = $return_value instanceof \Comodojo\Dispatcher\ObjectResult\ObjectResultInterface ? $return_value : $value; break; case 'VOID': default: $value = $value; break; } } return $value; } return $data; }