PageRenderTime 25ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 0ms

/application/Espo/Core/ControllerManager.php

https://gitlab.com/johanlindberg/irvato-crm
PHP | 130 lines | 75 code | 28 blank | 27 comment | 14 complexity | d062709c700e39ef00f76e7bf16e4fd8 MD5 | raw file
  1. <?php
  2. /************************************************************************
  3. * This file is part of EspoCRM.
  4. *
  5. * EspoCRM - Open Source CRM application.
  6. * Copyright (C) 2014-2015 Yuri Kuznetsov, Taras Machyshyn, Oleksiy Avramenko
  7. * Website: http://www.espocrm.com
  8. *
  9. * EspoCRM is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation, either version 3 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * EspoCRM is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with EspoCRM. If not, see http://www.gnu.org/licenses/.
  21. *
  22. * The interactive user interfaces in modified source and object code versions
  23. * of this program must display Appropriate Legal Notices, as required under
  24. * Section 5 of the GNU General Public License version 3.
  25. *
  26. * In accordance with Section 7(b) of the GNU General Public License version 3,
  27. * these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
  28. ************************************************************************/
  29. namespace Espo\Core;
  30. use \Espo\Core\Utils\Util;
  31. use \Espo\Core\Exceptions\NotFound;
  32. class ControllerManager
  33. {
  34. private $config;
  35. private $metadata;
  36. private $container;
  37. public function __construct(\Espo\Core\Container $container)
  38. {
  39. $this->container = $container;
  40. $this->config = $this->container->get('config');
  41. $this->metadata = $this->container->get('metadata');
  42. }
  43. protected function getConfig()
  44. {
  45. return $this->config;
  46. }
  47. protected function getMetadata()
  48. {
  49. return $this->metadata;
  50. }
  51. public function process($controllerName, $actionName, $params, $data, $request)
  52. {
  53. $customeClassName = '\\Espo\\Custom\\Controllers\\' . Util::normilizeClassName($controllerName);
  54. if (class_exists($customeClassName)) {
  55. $controllerClassName = $customeClassName;
  56. } else {
  57. $moduleName = $this->metadata->getScopeModuleName($controllerName);
  58. if ($moduleName) {
  59. $controllerClassName = '\\Espo\\Modules\\' . $moduleName . '\\Controllers\\' . Util::normilizeClassName($controllerName);
  60. } else {
  61. $controllerClassName = '\\Espo\\Controllers\\' . Util::normilizeClassName($controllerName);
  62. }
  63. }
  64. if ($data && stristr($request->getContentType(), 'application/json')) {
  65. $data = json_decode($data);
  66. }
  67. if ($data instanceof \stdClass) {
  68. $data = get_object_vars($data);
  69. }
  70. if (!class_exists($controllerClassName)) {
  71. throw new NotFound("Controller '$controllerName' is not found");
  72. }
  73. $controller = new $controllerClassName($this->container, $request->getMethod());
  74. if ($actionName == 'index') {
  75. $actionName = $controllerClassName::$defaultAction;
  76. }
  77. $actionNameUcfirst = ucfirst($actionName);
  78. $beforeMethodName = 'before' . $actionNameUcfirst;
  79. $actionMethodName = 'action' . $actionNameUcfirst;
  80. $afterMethodName = 'after' . $actionNameUcfirst;
  81. $fullActionMethodName = strtolower($request->getMethod()) . ucfirst($actionMethodName);
  82. if (method_exists($controller, $fullActionMethodName)) {
  83. $primaryActionMethodName = $fullActionMethodName;
  84. } else {
  85. $primaryActionMethodName = $actionMethodName;
  86. }
  87. if (!method_exists($controller, $primaryActionMethodName)) {
  88. throw new NotFound("Action '$actionName' (".$request->getMethod().") does not exist in controller '$controllerName'");
  89. }
  90. if (method_exists($controller, $beforeMethodName)) {
  91. $controller->$beforeMethodName($params, $data, $request);
  92. }
  93. $result = $controller->$primaryActionMethodName($params, $data, $request);
  94. if (method_exists($controller, $afterMethodName)) {
  95. $controller->$afterMethodName($params, $data, $request);
  96. }
  97. if (is_array($result) || is_bool($result) || $result instanceof \StdClass) {
  98. return \Espo\Core\Utils\Json::encode($result);
  99. }
  100. return $result;
  101. }
  102. }