| 1: | <?php |
| 2: | namespace TIC\TwigBundle\Traits; |
| 3: | |
| 4: | use Symfony\Component\HttpFoundation\Response; |
| 5: | use Symfony\Component\Form\FormInterface; |
| 6: | |
| 7: | |
| 8: | |
| 9: | |
| 10: | |
| 11: | trait ControllerViewable |
| 12: | { |
| 13: | |
| 14: | use \TIC\CoreBundle\Traits\ContextProperties; |
| 15: | |
| 16: | protected $defaultViewPath = null; |
| 17: | protected $defaultViewType = 'html'; |
| 18: | protected $defaultViewPage = 'index'; |
| 19: | |
| 20: | |
| 21: | |
| 22: | |
| 23: | |
| 24: | |
| 25: | |
| 26: | |
| 27: | |
| 28: | |
| 29: | |
| 30: | |
| 31: | |
| 32: | protected function getView(string $page = null, string $type = null): string |
| 33: | { |
| 34: | if (empty($page)) $page = $this->defaultViewPage; |
| 35: | if (empty($type)) $type = $this->defaultViewType; |
| 36: | |
| 37: | if (null !== $this->defaultViewPath) $path = $this->defaultViewPath; |
| 38: | else { |
| 39: | $path = $this->ctxSnake; |
| 40: | if ($this->ctxPath) $path = \strtolower($this->ctxPath) . '/' . $path; |
| 41: | if ($this->ctxBundle) $path = $this->ctxBundle . '/' . $path; |
| 42: | } |
| 43: | |
| 44: | return \sprintf('%s/%s.%s.twig', $path, $page, $type); |
| 45: | } |
| 46: | |
| 47: | |
| 48: | |
| 49: | |
| 50: | |
| 51: | |
| 52: | |
| 53: | |
| 54: | |
| 55: | protected function renderAction(?string $action = '', array $parameters = array(), Response $response = null): Response |
| 56: | { |
| 57: | $type = null; |
| 58: | if (\strpos($action, '.')) list($action, $type) = \explode('.', $action, 2); |
| 59: | |
| 60: | |
| 61: | if (null === $response) $response = new Response(); |
| 62: | foreach ($parameters as $k => $v) { |
| 63: | if (!$v instanceof FormInterface) continue; |
| 64: | $parameters[$k] = $v->createView(); |
| 65: | if (200 === $response->getStatusCode() && $v->isSubmitted() && !$v->isValid()) $response->setStatusCode(422); |
| 66: | } |
| 67: | |
| 68: | return $this->render($this->getView($action, $type), $parameters, $response); |
| 69: | } |
| 70: | |
| 71: | |
| 72: | |
| 73: | |
| 74: | |
| 75: | |
| 76: | |
| 77: | |
| 78: | protected function renderContent(string $action, array $parameters = array()): string |
| 79: | { |
| 80: | return $this->renderAction($action, $parameters)->getContent(); |
| 81: | } |
| 82: | |
| 83: | } |
| 84: | |