예제 #1
0
 public function onApplicationEnd(Message $message)
 {
     $response = $message->getParam('response');
     if ($response instanceof HtmlResponse) {
         $b = $response->getBody();
         $b = explode('</body>', $b);
         $b[0] .= str_replace(array("\n", "\t", "\r"), "", $this->view->render()) . '</body>';
         $response->setBody(implode('', $b));
     }
 }
예제 #2
0
 public function after(ResponseInterface $response)
 {
     if (config('micro_debug.handlers.fire_php')) {
         $db = \app('db');
         if ($db) {
             $profiler = $db->getProfiler();
             if ($profiler->getEnabled()) {
                 $totalTime = $profiler->getTotalElapsedSecs();
                 $queryCount = $profiler->getTotalNumQueries();
                 $longestTime = 0;
                 $longestQuery = \null;
                 $total = sprintf('%.6f', microtime(\true) - $_SERVER['REQUEST_TIME_FLOAT']);
                 $label = 'Executed ' . $queryCount . ' queries in ' . sprintf('%.6f', $totalTime) . ' seconds. (' . ($total ? \round($totalTime / $total * 100, 2) : 0) . '%)';
                 $table = [];
                 $table[] = ['Time', 'Event', 'Parameters'];
                 if ($profiler->getQueryProfiles()) {
                     foreach ($profiler->getQueryProfiles() as $k => $query) {
                         if ($query->getElapsedSecs() > $longestTime) {
                             $longestTime = $query->getElapsedSecs();
                             $longestQuery = $k;
                         }
                     }
                     foreach ($profiler->getQueryProfiles() as $k => $query) {
                         $table[] = [\sprintf('%.6f', $query->getElapsedSecs()) . ($k == $longestQuery ? ' !!!' : ''), $query->getQuery(), ($params = $query->getQueryParams()) ? $params : \null];
                     }
                 }
                 FirePHP\FirePHP::getInstance()->table('DB - ' . $label, $table);
             }
         }
     }
     if (\config('micro_debug.handlers.dev_tools')) {
         if ($response instanceof HtmlResponse) {
             $body = $response->getBody();
             if ($body->isSeekable()) {
                 $body->rewind();
             }
             $b = $body->getContents();
             $b = explode('</body>', $b);
             $b[0] .= str_replace(array("\n", "\t", "\r"), "", $this->view->render()) . '</body>';
             $response->withBody(new TempStream(implode('', $b)));
         }
     }
     if (($fileForCache = \config('micro_debug.handlers.performance')) !== \null) {
         $forStore = [];
         foreach (\MicroLoader::getFiles() as $class => $file) {
             if (\substr($class, 0, 6) === 'Micro\\') {
                 $forStore[$class] = $file;
             }
         }
         \file_put_contents($fileForCache, "<?php\nreturn " . \var_export($forStore, \true) . ";", \LOCK_EX);
     }
     return $response;
 }
예제 #3
0
 function pagination(Paginator $paginator, $partial = 'paginator', array $params = \null, View $view = \null)
 {
     $pages = ['pages' => $paginator->getPages()];
     if ($params !== \null) {
         $pages = array_merge($pages, (array) $params);
     }
     if ($view === \null) {
         $view = new View();
     }
     return $view->partial($partial, $pages);
 }
예제 #4
0
 /**
  * @param string $package
  * @param Http\Request $request
  * @param Http\Response $response
  * @param bool $subRequest
  * @throws CoreException
  * @return \Micro\Http\Response
  */
 public function resolve($package, Http\Request $request, Http\Response $response, $subRequest = \false)
 {
     if (!is_string($package) || strpos($package, '@') === \false) {
         throw new CoreException('[' . __METHOD__ . '] Package must be in [Package\\Handler@action] format', 500);
     }
     list($package, $action) = explode('@', $package);
     if (!class_exists($package, \true)) {
         throw new CoreException('[' . __METHOD__ . '] Package class "' . $package . '" not found', 404);
     }
     $parts = explode('\\', $package);
     $packageParam = Utils::decamelize($parts[0]);
     $controllerParam = Utils::decamelize($parts[count($parts) - 1]);
     $actionParam = Utils::decamelize($action);
     $request->setParam('package', $packageParam);
     $request->setParam('controller', $controllerParam);
     $request->setParam('action', $actionParam);
     $packageInstance = new $package($request, $response);
     if ($packageInstance instanceof Controller) {
         $actionMethod = lcfirst(Utils::camelize($action)) . 'Action';
     } else {
         $actionMethod = lcfirst(Utils::camelize($action));
     }
     if (!method_exists($packageInstance, $actionMethod)) {
         throw new CoreException('[' . __METHOD__ . '] Method "' . $actionMethod . '" not found in "' . $package . '"', 404);
     }
     if ($packageInstance instanceof ContainerAwareInterface) {
         $packageInstance->setContainer($this);
     }
     $scope = '';
     if ($packageInstance instanceof Controller) {
         $packageInstance->init();
         $scope = $packageInstance->getScope();
     }
     if (($packageResponse = $packageInstance->{$actionMethod}()) instanceof Http\Response) {
         return $packageResponse;
     }
     if (is_object($packageResponse) && !$packageResponse instanceof View) {
         throw new CoreException('[' . __METHOD__ . '] Package response is object and must be instance of View', 500);
     }
     if ($packageResponse === \null || is_array($packageResponse)) {
         if ($packageInstance instanceof Controller) {
             $view = $packageInstance->getView();
         } else {
             $view = new View();
         }
         if (is_array($packageResponse)) {
             $view->addData($packageResponse);
         }
         $packageResponse = $view;
     }
     if ($packageResponse instanceof View) {
         if ($packageResponse->getTemplate() === \null) {
             $packageResponse->setTemplate(($scope ? $scope . '/' : '') . $controllerParam . '/' . $actionParam);
         }
         $packageResponse->injectPaths((array) package_path($parts[0], 'Resources/views'));
         if (($eventResponse = $this->get('event')->trigger('render.start', ['view' => $packageResponse])) instanceof Http\Response) {
             return $eventResponse;
         }
         if ($subRequest) {
             $packageResponse->setRenderParent(\false);
         }
         $response->setBody((string) $packageResponse->render());
     } else {
         $response->setBody((string) $packageResponse);
     }
     return $response;
 }
예제 #5
0
 /**
  * @param mixed $module
  * @param ServerRequestInterface $request
  * @param ResponseInterface $response
  * @param bool $subRequest
  * @throws CoreException
  * @return ResponseInterface
  */
 public function resolve($module, ServerRequestInterface $request, ResponseInterface $response, $subRequest = \false)
 {
     if ($module instanceof ResponseInterface) {
         return $module;
     }
     if (($matches = $this->matchResolve($module)) === \null) {
         // write string to the response
         if (\is_string($module) || \is_object($module) && \method_exists($module, '__toString')) {
             $response->getBody()->write((string) $module);
             return $response;
         }
         throw new CoreException(\sprintf('Handler [%s] must be in [Handler@action] format', \is_object($module) ? \get_class($module) : $module));
     }
     $module = $matches[0];
     $action = $matches[1];
     if ($this->container->has($module)) {
         $moduleInstance = $this->container->get($module);
         if (!\is_object($moduleInstance) || $moduleInstance instanceof \Closure) {
             throw new CoreException('Handler "' . $module . '" is container service but it is not object', 500);
         }
         $module = \get_class($moduleInstance);
     } else {
         if (!\class_exists($module, \true)) {
             throw new CoreException('Handler class "' . $module . '" not found', 404);
         }
         $moduleInstance = new $module($request, $response, $this->container);
         if ($moduleInstance instanceof ContainerAwareInterface) {
             $moduleInstance->setContainer($this->container);
         }
     }
     $parts = explode('\\', $module);
     $moduleParam = Utils::decamelize($parts[0]);
     $controllerParam = Utils::decamelize($parts[count($parts) - 1]);
     $actionParam = Utils::decamelize($action);
     $request->withAttribute('module', $moduleParam);
     $request->withAttribute('controller', $controllerParam);
     $request->withAttribute('action', $actionParam);
     if ($moduleInstance instanceof Controller) {
         $action = $action . 'Action';
     }
     if (!\method_exists($moduleInstance, $action) && !\method_exists($moduleInstance, '__call')) {
         throw new CoreException('Method "' . $action . '" not found in "' . $module . '"', 404);
     }
     if ($moduleInstance instanceof Controller) {
         $moduleInstance->init();
     }
     $scope = '';
     if (\method_exists($moduleInstance, 'getScope')) {
         $scope = $moduleInstance->getScope();
     }
     if (($moduleResponse = $moduleInstance->{$action}()) instanceof ResponseInterface) {
         return $moduleResponse;
     }
     if (\is_object($moduleResponse) && !$moduleResponse instanceof View) {
         throw new CoreException('Handler response is object and must be instance of View', 500);
     }
     // resolve View object
     if ($moduleResponse === \null || is_array($moduleResponse)) {
         if ($moduleInstance instanceof Controller) {
             $view = $moduleInstance->getView();
         } else {
             $view = new View();
         }
         if (is_array($moduleResponse)) {
             $view->assign($moduleResponse);
         }
         $moduleResponse = $view;
     }
     if ($moduleResponse instanceof View) {
         if ($moduleResponse->getTemplate() === \null) {
             $moduleResponse->setTemplate(($scope ? $scope . '/' : '') . $controllerParam . '/' . $actionParam);
         }
         try {
             $moduleResponse->addPath(module_path($parts[0], '/Resources/views'), $moduleParam);
             $moduleResponse->addPath(config('view.paths', []));
         } catch (\Exception $e) {
         }
         if ($this->useEvent === \true && ($eventResponse = $this->container->get('event')->trigger('render.start', ['view' => $moduleResponse])) instanceof ResponseInterface) {
             return $eventResponse;
         }
         if ($subRequest) {
             $moduleResponse->setRenderParent(\false);
         }
         $response->getBody()->write((string) $moduleResponse->render());
     } else {
         $response->getBody()->write((string) $moduleResponse);
     }
     return $response;
 }