Bolt\Legacy\Storage::getContent PHP Method

getContent() public method

Used directly by {% setcontent %} but also in other parts. This code has been split into multiple methods in the spirit of separation of concerns, but the situation is still far from ideal. Where applicable each 'concern' notes the coupling in the local documentation.
public getContent ( string $textquery, string $parameters = '', array &$pager = [], array $whereparameters = [] ) : array
$textquery string
$parameters string
$pager array
$whereparameters array
return array
    public function getContent($textquery, $parameters = '', &$pager = [], $whereparameters = [])
    {
        // Start the 'stopwatch' for the profiler.
        $this->app['stopwatch']->start('bolt.getcontent', 'doctrine');
        // $whereparameters is passed if called from a compiled template. If present, merge it with $parameters.
        if (!empty($whereparameters)) {
            $parameters = array_merge((array) $parameters, (array) $whereparameters);
        }
        $logNotFound = false;
        if (isset($parameters['log_not_found'])) {
            $logNotFound = $parameters['log_not_found'];
            unset($parameters['log_not_found']);
        }
        // Decode this textquery
        $decoded = $this->decodeContentQuery($textquery, $parameters);
        if ($decoded === false) {
            $this->app['logger.system']->error("Not a valid query: '{$textquery}'", ['event' => 'storage']);
            $this->app['stopwatch']->stop('bolt.getcontent');
            return false;
        }
        // Run table checks
        if (!$this->runContentTypeTableChecks($decoded['contenttypes'])) {
            $this->app['stopwatch']->stop('bolt.getcontent');
            return false;
        }
        // Run the actual queries
        list($results, $totalResults) = call_user_func($decoded['queries_callback'], $decoded, $parameters);
        // Perform post hydration ordering
        if ($decoded['order_callback'] !== false) {
            if (is_scalar($decoded['order_callback']) && $decoded['order_callback'] == 'RANDOM') {
                shuffle($results);
            } else {
                uasort($results, $decoded['order_callback']);
            }
        }
        // Perform pagination if necessary, but never paginate when 'returnsingle' is used.
        $offset = 0;
        $limit = false;
        if ($decoded['self_paginated'] === false && isset($decoded['parameters']['page']) && !$decoded['return_single']) {
            $offset = ($decoded['parameters']['page'] - 1) * $decoded['parameters']['limit'];
            $limit = $decoded['parameters']['limit'];
        }
        if ($limit !== false) {
            $results = array_slice($results, $offset, $limit);
        }
        // Return content
        if ($decoded['return_single']) {
            if (count($results) > 0) {
                $this->app['stopwatch']->stop('bolt.getcontent');
                return reset($results);
            }
            if ($logNotFound) {
                $msg = sprintf("Requested specific query '%s', not found.", $textquery);
                $this->app['logger.system']->error($msg, ['event' => 'storage']);
            }
            $this->app['stopwatch']->stop('bolt.getcontent');
            return false;
        }
        // Set up the $pager array with relevant values, but only if we requested paging.
        if (isset($decoded['parameters']['paging'])) {
            $pagerName = implode('_', $decoded['contenttypes']);
            /** @var \Bolt\Pager\PagerManager $manager */
            $manager = $this->app['pager'];
            $pager = $manager->createPager($pagerName)->setCount($totalResults)->setTotalpages(ceil($totalResults / $decoded['parameters']['limit']))->setCurrent($decoded['parameters']['page'])->setShowingFrom(($decoded['parameters']['page'] - 1) * $decoded['parameters']['limit'] + 1)->setShowingTo(($decoded['parameters']['page'] - 1) * $decoded['parameters']['limit'] + count($results));
            $this->app['twig']->addGlobal('pager', $pager);
        }
        $this->app['stopwatch']->stop('bolt.getcontent');
        return $results;
    }

Usage Example

Ejemplo n.º 1
0
 /**
  * Render the not found page if on frontend and http exception
  *
  * @param GetResponseForExceptionEvent $event
  */
 public function onKernelException(GetResponseForExceptionEvent $event)
 {
     $exception = $event->getException();
     if (!$exception instanceof HttpExceptionInterface || Zone::isBackend($event->getRequest())) {
         return;
     }
     if ($exception->getStatusCode() !== Response::HTTP_NOT_FOUND) {
         return;
     }
     // If $notFoundPage is referencing a template, render it and be done.
     if ($this->render->hasTemplate($this->notFoundPage)) {
         try {
             $this->renderNotFound($event, $this->notFoundPage, []);
         } catch (TwigErrorLoader $e) {
             // Template not found, fall though to see if we can render a
             // record, failing that let the exception handler take over
         }
     }
     // Next try for referencing DB content.
     $content = $this->storage->getContent($this->notFoundPage, ['returnsingle' => true]);
     if (!$content instanceof Content || empty($content->id)) {
         return;
     }
     $template = $this->templateChooser->record($content);
     $this->renderNotFound($event, $template, $content->getTemplateContext());
 }
All Usage Examples Of Bolt\Legacy\Storage::getContent