Completed
Push — master ( 895e90...df3299 )
by Tolan
02:29
created
lib/AbstractViewComponent.php 1 patch
Spacing   +92 added lines, -92 removed lines patch added patch discarded remove patch
@@ -89,7 +89,7 @@  discard block
 block discarded – undo
89 89
         AbstractViewComponent $parent = null,
90 90
         ExecHelper $execHelper = null,
91 91
         LoggerInterface $logger = null
92
-    ){
92
+    ) {
93 93
         // Null means we are root
94 94
         $this->parent = $parent;
95 95
 
@@ -99,11 +99,11 @@  discard block
 block discarded – undo
99 99
         if (null === $execHelper) {
100 100
             $execHelper = new ExecHelper();
101 101
         }
102
-        $this->setExec( $execHelper );
102
+        $this->setExec($execHelper);
103 103
 
104 104
         $this->handleDependencyInjection();
105 105
         
106
-        $this->setLogger( $logger );
106
+        $this->setLogger($logger);
107 107
 
108 108
         // Set up the state container
109 109
         $this->initState();
@@ -113,11 +113,11 @@  discard block
 block discarded – undo
113 113
      * @param string $message
114 114
      * @param string $level A constant from LogLevel
115 115
      */
116
-    protected function log( $message, $level ){
117
-        if( isset( $this->logger ) ){
118
-            $class = get_class( $this );
116
+    protected function log($message, $level) {
117
+        if (isset($this->logger)) {
118
+            $class = get_class($this);
119 119
             $message = "[{$class}] {$message}";
120
-            $this->logger->log( $level, $message );
120
+            $this->logger->log($level, $message);
121 121
         }
122 122
     }
123 123
 
@@ -125,9 +125,9 @@  discard block
 block discarded – undo
125 125
      * User this to serialise ViewComponents as extra steps may be added later.
126 126
      * @return string
127 127
      */
128
-    public function dehydrate(){
129
-        $this->log( "Dehydrating", LogLevel::DEBUG );
130
-        return serialize( $this );
128
+    public function dehydrate() {
129
+        $this->log("Dehydrating", LogLevel::DEBUG);
130
+        return serialize($this);
131 131
     }
132 132
 
133 133
     /**
@@ -138,13 +138,13 @@  discard block
 block discarded – undo
138 138
      * @return AbstractViewComponent
139 139
      */
140 140
     
141
-    public static function rehydrate( $serialised, ExecHelper $execHelper, LoggerInterface $logger = null ){
141
+    public static function rehydrate($serialised, ExecHelper $execHelper, LoggerInterface $logger = null) {
142 142
         /** @var AbstractViewComponent $view */
143
-        $view = unserialize( $serialised );
144
-        $view->setExec( $execHelper );
143
+        $view = unserialize($serialised);
144
+        $view->setExec($execHelper);
145 145
         $view->handleDependencyInjection();
146
-        $view->setLogger( $logger );
147
-        $view->log( "Rehydrated", LogLevel::DEBUG );
146
+        $view->setLogger($logger);
147
+        $view->log("Rehydrated", LogLevel::DEBUG);
148 148
         return $view;
149 149
     }
150 150
 
@@ -168,7 +168,7 @@  discard block
 block discarded – undo
168 168
      * @throws \Exception
169 169
      * @return Response
170 170
      */
171
-    public function render( $execMethodName = null, array $execArgs = null )
171
+    public function render($execMethodName = null, array $execArgs = null)
172 172
     {
173 173
         $this->state->validate();
174 174
 
@@ -183,13 +183,13 @@  discard block
 block discarded – undo
183 183
         // If we're called with an 'exec' then run it instead of rendering the whole tree.
184 184
         // It may still render the whole tree or it may just render a portion or just return JSON
185 185
         if ($execMethodName) { // Used to test for null but it could easily be an empty string
186
-            $this->log( "Rendering with exec: {$execMethodName}, args:".var_export($execArgs, true ), LogLevel::DEBUG );
187
-            $out = $this->execMethod( $execMethodName, $execArgs );
186
+            $this->log("Rendering with exec: {$execMethodName}, args:" . var_export($execArgs, true), LogLevel::DEBUG);
187
+            $out = $this->execMethod($execMethodName, $execArgs);
188 188
         }else {
189
-            $this->log( "Rendering without exec", LogLevel::DEBUG );
190
-            $out = $this->template->render( $this->state, $this->props );
191
-            if (!( $out instanceof Response )) {
192
-                throw new \Exception( get_class( $this->template ) . " returned invalid response. Should have been an instance of ViewComponentResponse" );
189
+            $this->log("Rendering without exec", LogLevel::DEBUG);
190
+            $out = $this->template->render($this->state, $this->props);
191
+            if (!($out instanceof Response)) {
192
+                throw new \Exception(get_class($this->template) . " returned invalid response. Should have been an instance of ViewComponentResponse");
193 193
             }
194 194
         }
195 195
         return $out;
@@ -203,26 +203,26 @@  discard block
 block discarded – undo
203 203
      * @throws \Exception
204 204
      * @return Response
205 205
      */
206
-    protected function execMethod( $methodName, array $args = null )
206
+    protected function execMethod($methodName, array $args = null)
207 207
     {
208
-        if (!is_array( $methodName )) {
209
-            $methodName = explode( '.', $methodName );
208
+        if (!is_array($methodName)) {
209
+            $methodName = explode('.', $methodName);
210 210
         }
211
-        if (count( $methodName ) == 1) {
211
+        if (count($methodName) == 1) {
212 212
             $methodName = $methodName[ 0 ] . 'Handler';
213
-            $out = $this->$methodName( $args );
213
+            $out = $this->$methodName($args);
214 214
         }else {
215
-            $childName = array_shift( $methodName );
215
+            $childName = array_shift($methodName);
216 216
             $child = $this->childComponents[ $childName ];
217 217
             if ($child instanceof AbstractViewComponent) {
218
-                $out = $child->execMethod( $methodName, $args );
218
+                $out = $child->execMethod($methodName, $args);
219 219
             }else {
220
-                throw new \Exception( implode( ".", $methodName ) . " is not a valid method." );
220
+                throw new \Exception(implode(".", $methodName) . " is not a valid method.");
221 221
             }
222 222
         }
223
-        if (!( $out instanceof Response )) {
224
-            $nameStr = is_array( $methodName )?implode( ".", $methodName ):$methodName;
225
-            throw new \Exception( $nameStr . " returned invalid response. Should have been an instance of ViewComponentResponse" );
223
+        if (!($out instanceof Response)) {
224
+            $nameStr = is_array($methodName) ? implode(".", $methodName) : $methodName;
225
+            throw new \Exception($nameStr . " returned invalid response. Should have been an instance of ViewComponentResponse");
226 226
         }
227 227
         return $out;
228 228
     }
@@ -231,10 +231,10 @@  discard block
 block discarded – undo
231 231
      * @param $execMethod
232 232
      * @return string
233 233
      */
234
-    public function getExecPath( $execMethod )
234
+    public function getExecPath($execMethod)
235 235
     {
236 236
         $path = $this->getPath();
237
-        return ( $path === null?$execMethod:$path . '.' . $execMethod );
237
+        return ($path === null ? $execMethod : $path . '.' . $execMethod);
238 238
     }
239 239
 
240 240
     /**
@@ -249,7 +249,7 @@  discard block
 block discarded – undo
249 249
     /**
250 250
      * @param $string
251 251
      */
252
-    protected function setFlashMessage( $string )
252
+    protected function setFlashMessage($string)
253 253
     {
254 254
         $this->flashMessage = $string;
255 255
     }
@@ -257,7 +257,7 @@  discard block
 block discarded – undo
257 257
     /**
258 258
      * @param $string
259 259
      */
260
-    protected function setFlashError( $string )
260
+    protected function setFlashError($string)
261 261
     {
262 262
         $this->flashError = $string;
263 263
     }
@@ -299,7 +299,7 @@  discard block
 block discarded – undo
299 299
         if (null === $this->parent) {
300 300
             return null;
301 301
         }
302
-        if (null !== ( $pPath = $this->parent->getPath() )) {
302
+        if (null !== ($pPath = $this->parent->getPath())) {
303 303
             return $pPath . '.' . $this->handle;
304 304
         }else {
305 305
             return $this->handle;
@@ -315,20 +315,20 @@  discard block
 block discarded – undo
315 315
      * @return AbstractViewComponent
316 316
      * @throws \Exception
317 317
      */
318
-    protected function addOrUpdateChild( $handle, $type, array $props = [ ] )
318
+    protected function addOrUpdateChild($handle, $type, array $props = [ ])
319 319
     {
320
-        $this->log( "Adding/updating child '{$handle}' of type {$type}", LogLevel::DEBUG );
321
-        if (!isset( $this->childComponents[ $handle ] )) {
322
-            if( ! class_exists( $type ) ){
323
-                throw new \Exception( "Class '{$type}' for sub-component  does not exist." );
320
+        $this->log("Adding/updating child '{$handle}' of type {$type}", LogLevel::DEBUG);
321
+        if (!isset($this->childComponents[ $handle ])) {
322
+            if (!class_exists($type)) {
323
+                throw new \Exception("Class '{$type}' for sub-component  does not exist.");
324 324
             }
325
-            $child = new $type( $handle, $this, $this->exec, $this->logger );
325
+            $child = new $type($handle, $this, $this->exec, $this->logger);
326 326
             $this->childComponents[ $handle ] = $child;
327 327
         }else {
328 328
             // exec, di and logger are set recursively in rehydrate()
329 329
             $child = $this->childComponents[ $handle ];
330 330
         }
331
-        $child->updateProps( $props );
331
+        $child->updateProps($props);
332 332
         $child->updateState();
333 333
     }
334 334
 
@@ -339,12 +339,12 @@  discard block
 block discarded – undo
339 339
      * @return Response
340 340
      * @throws \Exception
341 341
      */
342
-    public function renderChild( $handle )
342
+    public function renderChild($handle)
343 343
     {
344 344
         if (!$this->childComponents[ $handle ]) {
345 345
             $message = "Attempted to render nonexistent child component with handle '{$handle}'";
346
-            $this->log( $message, LogLevel::CRITICAL );
347
-            throw new \Exception( $message );
346
+            $this->log($message, LogLevel::CRITICAL);
347
+            throw new \Exception($message);
348 348
         }
349 349
         return $this->childComponents[ $handle ]->render()->content;
350 350
     }
@@ -392,79 +392,79 @@  discard block
 block discarded – undo
392 392
      * @param array $inputs
393 393
      * @throws \Exception
394 394
      */
395
-    protected function testInputs( array $inputSpec, array &$inputs )
395
+    protected function testInputs(array $inputSpec, array &$inputs)
396 396
     {
397 397
         
398 398
         foreach ($inputSpec as $fieldName => $fieldSpec) {
399 399
             // Required field
400
-            if (( count( $fieldSpec ) < 2 )) {
401
-                if (!isset( $inputs[ $fieldName ] )) {
402
-                    $calledFunc = debug_backtrace()[1]['function'];
403
-                    $callerFunc = debug_backtrace()[2]['function'];
404
-                    $callerClass = debug_backtrace()[2]['class'];
400
+            if ((count($fieldSpec) < 2)) {
401
+                if (!isset($inputs[ $fieldName ])) {
402
+                    $calledFunc = debug_backtrace()[ 1 ][ 'function' ];
403
+                    $callerFunc = debug_backtrace()[ 2 ][ 'function' ];
404
+                    $callerClass = debug_backtrace()[ 2 ][ 'class' ];
405 405
                     $parentText = '';
406
-                    if( $this->parent !== null ){
407
-                        $parentText = " (parent component is ".get_class($this->parent).")";
406
+                    if ($this->parent !== null) {
407
+                        $parentText = " (parent component is " . get_class($this->parent) . ")";
408 408
                     }
409
-                    throw new \Exception( $fieldName . " is a required field for " . get_class( $this )."::{$calledFunc}() called from {$callerClass}::{$callerFunc}(){$parentText}" );
409
+                    throw new \Exception($fieldName . " is a required field for " . get_class($this) . "::{$calledFunc}() called from {$callerClass}::{$callerFunc}(){$parentText}");
410 410
                 }
411 411
             }
412 412
             // Set default is unset
413
-            if (!isset( $inputs[ $fieldName ] )) {
413
+            if (!isset($inputs[ $fieldName ])) {
414 414
                 $inputs[ $fieldName ] = $fieldSpec[ 1 ];
415 415
             }
416 416
             // Check type
417 417
             // Any type allowed, continue
418
-            if (!isset( $fieldSpec[ 0 ] ) || $fieldSpec[ 0 ] === null) {
418
+            if (!isset($fieldSpec[ 0 ]) || $fieldSpec[ 0 ] === null) {
419 419
                 continue;
420 420
             }
421 421
             $requiredType = $fieldSpec[ 0 ];
422 422
             $input = $inputs[ $fieldName ];
423 423
             // Specific type required
424 424
             // Null is allowed
425
-            if (!is_null( $input )) {
425
+            if (!is_null($input)) {
426 426
                 switch ($requiredType) {
427 427
                     case "boolean":
428 428
                     case "bool":
429
-                    $failed = !is_bool( $input );
429
+                    $failed = !is_bool($input);
430 430
                         break;
431 431
                     case "integer":
432 432
                     case "int":
433
-                    $failed = !is_int( $input+0 );
433
+                    $failed = !is_int($input + 0);
434 434
                         break;
435 435
                     case "double":
436
-                        $failed = !is_double( $input+0 );
436
+                        $failed = !is_double($input + 0);
437 437
                         break;
438 438
                     case "float":
439
-                        $failed = !is_float( $input+0 );
439
+                        $failed = !is_float($input + 0);
440 440
                         break;
441 441
                     case "string":
442
-                        $failed = !is_string( $input );
442
+                        $failed = !is_string($input);
443 443
                         break;
444 444
                     case "array":
445
-                        $failed = !is_array( $input );
445
+                        $failed = !is_array($input);
446 446
                         break;
447 447
                     case "object":
448
-                        $failed = !is_object( $input );
448
+                        $failed = !is_object($input);
449 449
                         break;
450 450
                     case "resource":
451
-                        $failed = !is_resource( $input );
451
+                        $failed = !is_resource($input);
452 452
                         break;
453 453
                     case "callable":
454
-                        $failed = !is_callable( $input );
454
+                        $failed = !is_callable($input);
455 455
                         break;
456 456
                     default:
457
-                        $failed = !( $input instanceof $requiredType );
457
+                        $failed = !($input instanceof $requiredType);
458 458
                 }
459 459
                 if ($failed) {
460
-                    $calledFunc = debug_backtrace()[1]['function'];
461
-                    $callerFunc = debug_backtrace()[2]['function'];
462
-                    $callerClass = debug_backtrace()[2]['class'];
460
+                    $calledFunc = debug_backtrace()[ 1 ][ 'function' ];
461
+                    $callerFunc = debug_backtrace()[ 2 ][ 'function' ];
462
+                    $callerClass = debug_backtrace()[ 2 ][ 'class' ];
463 463
                     $parentText = '';
464
-                    if( $this->parent !== null ){
465
-                        $parentText = " (parent component is ".get_class($this->parent).")";
464
+                    if ($this->parent !== null) {
465
+                        $parentText = " (parent component is " . get_class($this->parent) . ")";
466 466
                     }
467
-                    throw new \Exception( $fieldName . " should be of type " . $requiredType . "in " . get_class( $this )."::{$calledFunc}() called from {$callerClass}::{$callerFunc}(){$parentText}" );
467
+                    throw new \Exception($fieldName . " should be of type " . $requiredType . "in " . get_class($this) . "::{$calledFunc}() called from {$callerClass}::{$callerFunc}(){$parentText}");
468 468
                 }
469 469
             }
470 470
         }
@@ -475,9 +475,9 @@  discard block
 block discarded – undo
475 475
      *
476 476
      * @var array $props
477 477
      */
478
-    public function updateView( $props )
478
+    public function updateView($props)
479 479
     {
480
-        $this->updateProps( $props );
480
+        $this->updateProps($props);
481 481
         $this->updateState();
482 482
     }
483 483
 
@@ -486,13 +486,13 @@  discard block
 block discarded – undo
486 486
      *
487 487
      * @var array $props
488 488
      */
489
-    protected function updateProps( $props )
489
+    protected function updateProps($props)
490 490
     {
491
-        $this->log( "Storing new props: " . var_export( $props, true ), LogLevel::DEBUG );
491
+        $this->log("Storing new props: " . var_export($props, true), LogLevel::DEBUG);
492 492
         $this->props = $props;   
493 493
     }
494 494
 
495
-    protected function forceResponse( Response $response )
495
+    protected function forceResponse(Response $response)
496 496
     {
497 497
         $this->forceResponse = $response;
498 498
     }
@@ -500,12 +500,12 @@  discard block
 block discarded – undo
500 500
     /**
501 501
      * @param ExecHelper $execHelper
502 502
      */
503
-    private function setExec( ExecHelper $execHelper )
503
+    private function setExec(ExecHelper $execHelper)
504 504
     {
505 505
         $this->exec = clone $execHelper;
506
-        $this->exec->setComponent( $this );
507
-        foreach( $this->childComponents as $child ){
508
-            $child->setExec( $execHelper );
506
+        $this->exec->setComponent($this);
507
+        foreach ($this->childComponents as $child) {
508
+            $child->setExec($execHelper);
509 509
         }
510 510
     }
511 511
 
@@ -519,9 +519,9 @@  discard block
 block discarded – undo
519 519
         // it manually and you still get the advantage that the deps
520 520
         // are specified in the optional injectDependencies() method's
521 521
         // signature
522
-        $this->log( "Dependency injection...", LogLevel::DEBUG );
523
-        DependencyInjector::instance()->injectIntoMethod( $this );
524
-        foreach( $this->childComponents as $child ){
522
+        $this->log("Dependency injection...", LogLevel::DEBUG);
523
+        DependencyInjector::instance()->injectIntoMethod($this);
524
+        foreach ($this->childComponents as $child) {
525 525
             $child->handleDependencyInjection();
526 526
         }
527 527
     }
@@ -529,13 +529,13 @@  discard block
 block discarded – undo
529 529
     /**
530 530
      * @param LoggerInterface $logger
531 531
      */
532
-    private function setLogger( LoggerInterface $logger = null )
532
+    private function setLogger(LoggerInterface $logger = null)
533 533
     {
534
-        if( null !== $logger ){
534
+        if (null !== $logger) {
535 535
             $this->logger = $logger;
536 536
             /** @var AbstractViewComponent $child */
537
-            foreach( $this->childComponents as $child ){
538
-                $child->setLogger( $logger );
537
+            foreach ($this->childComponents as $child) {
538
+                $child->setLogger($logger);
539 539
             }
540 540
         }
541 541
     }
Please login to merge, or discard this patch.