Resolver.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. <?php
  2. /**
  3. * Pimf
  4. *
  5. * @copyright Copyright (c) Gjero Krsteski (http://krsteski.de)
  6. * @license http://krsteski.de/new-bsd-license New BSD License
  7. */
  8. namespace Pimf;
  9. use Pimf\Resolver\Exception as Bomb;
  10. use Pimf\Util\String as Str;
  11. /**
  12. * Resolves the user requests to controller and action.
  13. *
  14. * @package Pimf
  15. * @author Gjero Krsteski <[email protected]>
  16. */
  17. class Resolver
  18. {
  19. /**
  20. * @var string
  21. */
  22. protected $controllerPath;
  23. /**
  24. * @var string
  25. */
  26. protected $controllerClass;
  27. /**
  28. * @var string
  29. */
  30. protected $repositoryPath;
  31. /**
  32. * @var Request
  33. */
  34. protected $request;
  35. /**
  36. * @param Request $request
  37. * @param string $repositoryPath
  38. * @param string $prefix
  39. *
  40. * @throws Resolver\Exception
  41. */
  42. public function __construct(Request $request, $repositoryPath = '/Controller', $prefix = 'Pimf\\')
  43. {
  44. $conf = Registry::get('conf');
  45. $controllerName = $request->fromGet()->get('controller');
  46. if ($conf['app']['routeable'] === true) {
  47. $target = Registry::get('router')->find();
  48. if ($target instanceof \Pimf\Route\Target) {
  49. $controllerName = $target->getController();
  50. }
  51. }
  52. if (Sapi::isCli() && $conf['environment'] == 'production') {
  53. $controllerName = $request->fromCli()->get('controller');
  54. }
  55. if (!$controllerName) {
  56. $controllerName = $conf['app']['default_controller'];
  57. }
  58. $this->repositoryPath = $repositoryPath;
  59. $this->request = $request;
  60. $this->controllerClass = $prefix . 'Controller\\';
  61. $basepath = $this->repositoryPath . '/';
  62. $controller = ucfirst($controllerName);
  63. if (Str::isEvilPath($basepath . $controller)) {
  64. throw new Bomb('directory traversal attack is not funny!');
  65. }
  66. $this->controllerPath = $basepath . $controller . '.php';
  67. if (!file_exists($this->controllerPath)) {
  68. throw new Bomb('no controller found at the repository path; ' . $this->controllerPath);
  69. }
  70. }
  71. /**
  72. * @return \Pimf\Controller\Base
  73. * @throws \Exception If no controller specified or no controller found at the repository.
  74. */
  75. public function process()
  76. {
  77. $path = str_replace($this->repositoryPath, '', $this->controllerPath);
  78. $name = str_replace('/', $this->controllerClass, $path);
  79. $controller = str_replace('.php', '', $name);
  80. if (!class_exists($controller)) {
  81. throw new Bomb('can not load class "' . $controller . '" from the repository');
  82. }
  83. return new $controller($this->request, new Response(Registry::get('env')->REQUEST_METHOD));
  84. }
  85. }