3580392da1e4c489e5f3f89a89c2e4203dc0b1aa
[yaffs-website] / web / modules / contrib / devel / webprofiler / src / Views / TraceableViewExecutable.php
1 <?php
2
3 namespace Drupal\webprofiler\Views;
4
5 use Drupal\views\ViewExecutable;
6 use Drupal\views\Views;
7 use Symfony\Component\HttpFoundation\Response;
8
9 /**
10  * Class TraceableViewExecutable
11  */
12 class TraceableViewExecutable extends ViewExecutable {
13
14   /**
15    * @var float
16    */
17   protected $render_time;
18
19   /**
20    * Gets the build time.
21    *
22    * @return float
23    */
24   public function getBuildTime() {
25     return $this->build_time;
26   }
27
28   /**
29    * Gets the execute time.
30    *
31    * @return float
32    */
33   public function getExecuteTime() {
34     return $this->execute_time;
35   }
36
37   /**
38    * Gets the render time.
39    *
40    * @return float
41    */
42   public function getRenderTime() {
43     return $this->render_time;
44   }
45
46   /**
47    * {@inheritdoc}
48    */
49   public function render($display_id = NULL) {
50     $start = microtime(TRUE);
51
52     $this->execute($display_id);
53
54     // Check to see if the build failed.
55     if (!empty($this->build_info['fail'])) {
56       return;
57     }
58     if (!empty($this->build_info['denied'])) {
59       return;
60     }
61
62     /** @var \Drupal\views\Plugin\views\exposed_form\ExposedFormPluginInterface $exposed_form */
63     $exposed_form = $this->display_handler->getPlugin('exposed_form');
64     $exposed_form->preRender($this->result);
65
66     $module_handler = \Drupal::moduleHandler();
67
68     // @TODO In the longrun, it would be great to execute a view without
69     //   the theme system at all. See https://www.drupal.org/node/2322623.
70     $active_theme = \Drupal::theme()->getActiveTheme();
71     $themes = array_keys($active_theme->getBaseThemes());
72     $themes[] = $active_theme->getName();
73
74     // Check for already-cached output.
75     /** @var \Drupal\views\Plugin\views\cache\CachePluginBase $cache */
76     if (!empty($this->live_preview)) {
77       $cache = Views::pluginManager('cache')->createInstance('none');
78     }
79     else {
80       $cache = $this->display_handler->getPlugin('cache');
81     }
82
83     // Run preRender for the pager as it might change the result.
84     if (!empty($this->pager)) {
85       $this->pager->preRender($this->result);
86     }
87
88     // Initialize the style plugin.
89     $this->initStyle();
90
91     if (!isset($this->response)) {
92       // Set the response so other parts can alter it.
93       $this->response = new Response('', 200);
94     }
95
96     // Give field handlers the opportunity to perform additional queries
97     // using the entire resultset prior to rendering.
98     if ($this->style_plugin->usesFields()) {
99       foreach ($this->field as $id => $handler) {
100         if (!empty($this->field[$id])) {
101           $this->field[$id]->preRender($this->result);
102         }
103       }
104     }
105
106     $this->style_plugin->preRender($this->result);
107
108     // Let each area handler have access to the result set.
109     $areas = ['header', 'footer'];
110     // Only call preRender() on the empty handlers if the result is empty.
111     if (empty($this->result)) {
112       $areas[] = 'empty';
113     }
114     foreach ($areas as $area) {
115       foreach ($this->{$area} as $handler) {
116         $handler->preRender($this->result);
117       }
118     }
119
120     // Let modules modify the view just prior to rendering it.
121     $module_handler->invokeAll('views_pre_render', [$this]);
122
123     // Let the themes play too, because pre render is a very themey thing.
124     foreach ($themes as $theme_name) {
125       $function = $theme_name . '_views_pre_render';
126       if (function_exists($function)) {
127         $function($this);
128       }
129     }
130
131     $this->display_handler->output = $this->display_handler->render();
132
133     $exposed_form->postRender($this->display_handler->output);
134
135     $cache->postRender($this->display_handler->output);
136
137     // Let modules modify the view output after it is rendered.
138     $module_handler->invokeAll('views_post_render', [
139       $this,
140       &$this->display_handler->output,
141       $cache
142     ]);
143
144     // Let the themes play too, because post render is a very themey thing.
145     foreach ($themes as $theme_name) {
146       $function = $theme_name . '_views_post_render';
147       if (function_exists($function)) {
148         $function($this, $this->display_handler->output, $cache);
149       }
150     }
151
152     $this->render_time = microtime(TRUE) - $start;
153
154     return $this->display_handler->output;
155   }
156 }