PageRenderTime 50ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

/inc/api/command.php

http://github.com/symfony-cmf/phpcrbrowser
PHP | 152 lines | 51 code | 15 blank | 86 comment | 8 complexity | b7d91ae557f7609f83495800a4b279eb MD5 | raw file
Possible License(s): Apache-2.0, BSD-3-Clause
  1. <?php
  2. /* Licensed under the Apache License, Version 2.0
  3. * See the LICENSE and NOTICE file for further information
  4. */
  5. /**
  6. * Base command to be extended by all commands.
  7. *
  8. * A command is responsible for getting the data and executing all the
  9. * actions for the current request.
  10. *
  11. * @author Silvan Zurbruegg
  12. */
  13. abstract class api_command {
  14. /**
  15. * api_request: Request object containing information about the current
  16. * request.
  17. */
  18. protected $request = null;
  19. /**
  20. * api_response: Response object used to send output to the client.
  21. */
  22. protected $response = null;
  23. /**
  24. * array: Route definition of the current request. This is the return
  25. * value of api_routing::getRoute().
  26. */
  27. protected $route = array();
  28. /**
  29. * array of api_model: Data objects to be passed to the XSLT
  30. * stylesheets.
  31. */
  32. protected $data = array();
  33. /**
  34. * string: Default Method. Gets called by api_command::process() when
  35. * the route does not contain a method.
  36. */
  37. protected $defaultMethod = 'defaultRequest';
  38. /**
  39. * Constructor. Initializes the object's attributes but does not have
  40. * any side effects.
  41. * @param $route array: The attributes as returned by
  42. * api_routing::getRoute().
  43. */
  44. public function __construct(&$route) {
  45. $this->request = api_request::getInstance();
  46. $this->response = api_response::getInstance();
  47. $this->route = &$route;
  48. }
  49. /**
  50. * Get XSL parameters from command. Used to overwrite view configuration
  51. * from the route.
  52. * @return array: Associative array with params.
  53. */
  54. public function getXslParams() {
  55. return array();
  56. }
  57. /**
  58. * Process request. This is the entry point of a command which calls
  59. * the method as passed in from the routing engine. If no method has
  60. * been defined, then the method specified by api_command::$defaultMethod
  61. * is called.
  62. * @return void
  63. */
  64. public function process() {
  65. $route = $this->route;
  66. if (isset($route['method']) && $route['method'] != 'process') {
  67. // __call() may return false, then we go execute the default request
  68. if ((!method_exists($this, $route['method']) || is_callable(array($this, $route['method'])))
  69. && $this->{$route['method']}()
  70. ) {
  71. return;
  72. }
  73. }
  74. $this->{$this->defaultMethod}();
  75. return;
  76. }
  77. /**
  78. * Default method called by api_command::process (as specified with
  79. * api_command::$defaultMethod).
  80. *
  81. * If you want a catch-all method that is executed on every request,
  82. * overwrite api_command::process(). If you just want a fall-back for
  83. * the case when a method specified in the route doesn't exist in this
  84. * class, then overwrite api_command::defaultRequest().
  85. * @return void
  86. */
  87. public function defaultRequest() {
  88. }
  89. /**
  90. * Safety purposes __call so that the default command is called when
  91. * somebody is careless in the route configuration. So if the route
  92. * configuration specifies a method which doesn't exist in the command,
  93. * then this no-operation method is called.
  94. * @param $name string: Method name. Passed in automatically by PHP.
  95. * @param $argv array: Method parameters. Passed in automatically by PHP.
  96. * @return bool: false
  97. */
  98. public function __call($name, $argv) {
  99. return false;
  100. }
  101. /**
  102. * Checks permission. To prevent a user from accessing a command, the
  103. * command has to redirect the user somewhere else in this method.
  104. *
  105. * When this method returns false the controller throws a
  106. * api_exception_CommandNotAllowed exception.
  107. *
  108. * @return bool
  109. */
  110. public function isAllowed() {
  111. return true;
  112. }
  113. /**
  114. * Merge the response of all data objects in api_command::$data into
  115. * one XML DOM and return the DOM. This calls api_model::$getDOM()
  116. * on every element of the data array.
  117. * @return DOMDocument: Document with root tag "command". The root tag
  118. * has an attribute "name" which is set to the base
  119. * name of the command class
  120. * (see api_helpers_class::getBaseName()).
  121. */
  122. public function getData() {
  123. $dom = new DOMDocument();
  124. $dom->loadXML("<command/>");
  125. $commandname = api_helpers_class::getBaseName($this);
  126. $cmdNode = $dom->documentElement;
  127. $cmdNode->setAttribute("name", $commandname);
  128. foreach ($this->data as $d) {
  129. $dataDom = $d->getDOM();
  130. if (!is_null($dataDom) && $dataDom->documentElement) {
  131. $node = $dom->importNode($dataDom->documentElement, true);
  132. $dom->documentElement->appendChild($node);
  133. }
  134. }
  135. return $dom;
  136. }
  137. }