| 1 |  |  | <?php declare(strict_types=1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  * Bright Nucleus View Component. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * @package   BrightNucleus\View | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * @author    Alain Schlesser <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  * @license   MIT | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * @link      http://www.brightnucleus.com/ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  * @copyright 2016-2017 Alain Schlesser, Bright Nucleus | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | namespace BrightNucleus\View\View; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | use BrightNucleus\Config\Exception\FailedToProcessConfigException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | use BrightNucleus\View\Exception\FailedToInstantiateView; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | use BrightNucleus\View\View; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | use BrightNucleus\View\Engine\Engine; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | use BrightNucleus\View\ViewBuilder; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | use BrightNucleus\Views; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | use Closure; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |  * Abstract class AbstractView. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |  * @since   0.1.0 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |  * @package BrightNucleus\View\View | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |  * @author  Alain Schlesser <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  | abstract class AbstractView implements View | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |      * URI of the view. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |      * The underscores are used to prevent accidental use of these properties from within the rendering closure. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |      * @since 0.1.0 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |      * @var string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |     protected $_uri_; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |      * Engine to use for the view. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |      * The underscores are used to prevent accidental use of these properties from within the rendering closure. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |      * @since 0.1.0 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |      * @var Engine | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |     protected $_engine_; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |      * ViewBuilder instance. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |      * The underscores are used to prevent accidental use of these properties from within the rendering closure. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |      * @since 0.2.0 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |      * @var ViewBuilder | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |     protected $_builder_; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 | 29 |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |      * The context with which the view will be rendered. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 | 29 |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 | 29 |  |      * The underscores are used to prevent accidental use of these properties from within the rendering closure. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 | 29 |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |      * @since 0.4.0 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |      * @var array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |     protected $_context_ = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |      * Instantiate an AbstractView object. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |      * @since 0.1.0 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 | 29 |  |      * @param string $uri    URI for the view. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |      * @param Engine $engine Engine to use for the view. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 | 29 |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 | 29 |  |     public function __construct(string $uri, Engine $engine) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 | 29 |  |         $this->_uri_    = $uri; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 | 29 |  |         $this->_engine_ = $engine; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 | 29 |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |      * Render the view. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 | 29 |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 | 29 |  |      * @since 0.1.0 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |      * @param array $context Optional. The context in which to render the view. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |      * @param bool  $echo    Optional. Whether to echo the output immediately. Defaults to false. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |      * @return string Rendered HTML. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |      * @throws FailedToProcessConfigException If the Config could not be processed. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |     public function render(array $context = [], bool $echo = false): string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |         $this->initializeViewBuilder(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |         $this->assimilateContext($context); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |         $closure = Closure::bind( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |             $this->_engine_->getRenderCallback($this->_uri_, $context), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |             $this, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |             static::class | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 | 1 |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 | 1 |  |         $output = $closure(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 | 1 |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |         if ($echo) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |             echo $output; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 | 1 |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 | 1 |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |         return $output; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 | 1 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |      * Render a partial view (or section) for a given URI. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |      * @since 0.2.0 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |      * @param string      $view    View identifier to create a view for. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |      * @param array       $context Optional. The context in which to render the view. | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |      * @param string|null $type    Type of view to create. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |      * @return string Rendered HTML content. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 | 29 |  |      * @throws FailedToProcessConfigException If the Config could not be processed. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |      * @throws FailedToInstantiateView If the View could not be instantiated. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 | 29 |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |     public function section(string $view, array $context = null, $type = null): string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 | 29 |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |         if (null === $context) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |             $context = $this->_context_; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  |         $this->initializeViewBuilder(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |         $viewObject = $this->_builder_->create($view, $type); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 | 29 |  |         return $viewObject->render($context); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 | 29 |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  |      * Get the entire array of contextual data. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 | 29 |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  |      * @since 0.4.0 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 |  |  |      * @return array Array of contextual data. | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 153 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 154 |  |  |     public function getContext(): array | 
            
                                                                        
                            
            
                                    
            
            
                | 155 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 156 |  |  |         return $this->_context_; | 
            
                                                                        
                            
            
                                    
            
            
                | 157 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 158 | 29 |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 159 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 160 | 29 |  |      * Associate a view builder with this view. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 161 | 29 |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 162 | 29 |  |      * @since 0.2.0 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 163 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 164 | 29 |  |      * @param ViewBuilder $builder | 
            
                                                                                                            
                            
            
                                    
            
            
                | 165 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 166 |  |  |      * @return View | 
            
                                                                                                            
                            
            
                                    
            
            
                | 167 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 168 |  |  |     public function setBuilder(ViewBuilder $builder): View | 
            
                                                                                                            
                            
            
                                    
            
            
                | 169 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 170 |  |  |         $this->_builder_ = $builder; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 171 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 172 |  |  |         return $this; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 173 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 174 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 175 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 176 |  |  |      * Initialize the view builder associated with the view. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 177 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 178 |  |  |      * @since 0.2.0 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 179 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 180 |  |  |      * @throws FailedToProcessConfigException If the Config could not be processed. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 181 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 182 |  |  |     protected function initializeViewBuilder() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 183 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 184 |  |  |         if (null === $this->_builder_) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 185 |  |  |             $this->_builder_ = Views::getViewBuilder(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 186 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 187 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 188 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 189 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 190 |  |  |      * Assimilate the context to make it available as properties. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 191 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 192 |  |  |      * @since 0.2.0 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 193 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 194 |  |  |      * @param array $context Context to assimilate. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 195 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 196 |  |  |     protected function assimilateContext(array $context = []) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 197 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 198 |  |  |         $this->_context_ = $context; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 199 |  |  |         foreach ($context as $key => $value) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 200 |  |  |             $this->$key = $value; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 201 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 202 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 203 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 204 |  |  |  | 
            
                        
This check looks for
@paramannotations where the type inferred by our type inference engine differs from the declared type.It makes a suggestion as to what type it considers more descriptive. In addition it looks for parameters that have the generic type
arrayand suggests a stricter type likearray<String>.Most often this is a case of a parameter that can be null in addition to its declared types.