MAPG-10 refactor view-controller to be able to handle JSON response
This commit is contained in:
		
							parent
							
								
									55b693df68
								
							
						
					
					
						commit
						037d19b0b3
					
				@ -1,24 +0,0 @@
 | 
				
			|||||||
<?php namespace MapGuesser\Controller;
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
abstract class BaseController
 | 
					 | 
				
			||||||
{
 | 
					 | 
				
			||||||
    protected string $view;
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    protected array $variables = [];
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    public function render() : string
 | 
					 | 
				
			||||||
    {
 | 
					 | 
				
			||||||
        $this->operate();
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        extract($this->variables);
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        ob_start();
 | 
					 | 
				
			||||||
        require ROOT . '/views/' . $this->view . '.php';
 | 
					 | 
				
			||||||
        $content = ob_get_contents();
 | 
					 | 
				
			||||||
        ob_end_clean();
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        return $content;
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    abstract protected function operate() : void;
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
							
								
								
									
										8
									
								
								src/Controller/ControllerInterface.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										8
									
								
								src/Controller/ControllerInterface.php
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,8 @@
 | 
				
			|||||||
 | 
					<?php namespace MapGuesser\Controller;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					use MapGuesser\View\ViewBase;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					interface ControllerInterface
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
					    public function run(): ViewBase;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
							
								
								
									
										29
									
								
								src/View/HtmlView.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										29
									
								
								src/View/HtmlView.php
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,29 @@
 | 
				
			|||||||
 | 
					<?php namespace MapGuesser\View;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					class HtmlView extends ViewBase
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
					    private string $template;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    public function __construct(string $template, array &$data = [])
 | 
				
			||||||
 | 
					    {
 | 
				
			||||||
 | 
					        $this->template = $template;
 | 
				
			||||||
 | 
					        $this->data = &$data;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    public function &render(): string
 | 
				
			||||||
 | 
					    {
 | 
				
			||||||
 | 
					        extract($this->data);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        ob_start();
 | 
				
			||||||
 | 
					        require ROOT . '/views/' . $this->template . '.php';
 | 
				
			||||||
 | 
					        $content = ob_get_contents();
 | 
				
			||||||
 | 
					        ob_end_clean();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        return $content;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    public function getContentType(): string
 | 
				
			||||||
 | 
					    {
 | 
				
			||||||
 | 
					        return 'text/html';
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
							
								
								
									
										21
									
								
								src/View/JsonView.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										21
									
								
								src/View/JsonView.php
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,21 @@
 | 
				
			|||||||
 | 
					<?php namespace MapGuesser\View;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					class JsonView extends ViewBase
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
					    public function __construct(array &$data = [])
 | 
				
			||||||
 | 
					    {
 | 
				
			||||||
 | 
					        $this->data = &$data;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    public function &render(): string
 | 
				
			||||||
 | 
					    {
 | 
				
			||||||
 | 
					        $content = json_encode($this->data);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        return $content;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    public function getContentType(): string
 | 
				
			||||||
 | 
					    {
 | 
				
			||||||
 | 
					        return 'application/json';
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
							
								
								
									
										15
									
								
								src/View/ViewBase.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										15
									
								
								src/View/ViewBase.php
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,15 @@
 | 
				
			|||||||
 | 
					<?php namespace MapGuesser\View;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					abstract class ViewBase
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
					    protected array $data;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    public function &getData(): array
 | 
				
			||||||
 | 
					    {
 | 
				
			||||||
 | 
					        return $this->data;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    abstract public function &render(): string;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    abstract public function getContentType(): string;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user