SproutCMS

This is the code documentation for the SproutCMS project

source of /sprout/Controllers/BaseController.php

Copyright (C) 2017 Karmabunny Pty Ltd.

This file is a part of SproutCMS.

SproutCMS is free software: you can redistribute it and/or modify it under the terms
of the GNU General Public License as published by the Free Software Foundation, either
version 2 of the License, or (at your option) any later version.

For more information, visit <http://getsproutcms.com>.

This class was originally from Kohana 2.3.4
Copyright 2007-2008 Kohana Team
  1. <?php
  2. /**
  3.  * Copyright (C) 2017 Karmabunny Pty Ltd.
  4.  *
  5.  * This file is a part of SproutCMS.
  6.  *
  7.  * SproutCMS is free software: you can redistribute it and/or modify it under the terms
  8.  * of the GNU General Public License as published by the Free Software Foundation, either
  9.  * version 2 of the License, or (at your option) any later version.
  10.  *
  11.  * For more information, visit <http://getsproutcms.com>.
  12.  *
  13.  * This class was originally from Kohana 2.3.4
  14.  * Copyright 2007-2008 Kohana Team
  15.  */
  16.  
  17. namespace Sprout\Controllers;
  18.  
  19. use Event;
  20. use Exception;
  21. use Kohana;
  22. use Sprout\Helpers\Sprout;
  23. use Sprout\Helpers\Text;
  24.  
  25. /**
  26.  * This is a true base controller.
  27.  *
  28.  * It does nothing but base things.
  29.  *
  30.  * @package Sprout\Controllers
  31.  */
  32. abstract class BaseController
  33. {
  34.  
  35. // Allow all controllers to run in production by default
  36. const ALLOW_PRODUCTION = TRUE;
  37.  
  38. /**
  39.   * @return void
  40.   */
  41. public function __construct()
  42. {
  43. if (Kohana::$instance == NULL)
  44. {
  45. // Set the instance to the first controller loaded
  46. Kohana::$instance = $this;
  47. }
  48. }
  49.  
  50.  
  51. /**
  52.   * The router/kohana will invoke this method to invoke an action.
  53.   *
  54.   * If you please, you may wrap this method to create before/after hooks.
  55.   *
  56.   * @param mixed $method
  57.   * @param mixed $args
  58.   * @return void
  59.   */
  60. public function _run($method, $args)
  61. {
  62. $this->$method(...$args);
  63. }
  64.  
  65.  
  66. /**
  67.   * Handles methods that do not exist.
  68.   *
  69.   * @param string method name
  70.   * @param array arguments
  71.   * @return void
  72.   */
  73. public function __call($method, $args)
  74. {
  75. // If this method is called directly as a result of a bad URL or route, a 404 error is reported
  76. $bt = debug_backtrace();
  77. if ($bt[1]['function'] === 'invokeArgs' and $bt[1]['class'] === 'ReflectionMethod') {
  78. Event::run('system.404');
  79. return;
  80. }
  81.  
  82. // In every other case, the missing method should be reported
  83. throw new \Exception("Method '{$method}' not found");
  84. }
  85.  
  86.  
  87. /**
  88.   * Gets the relative path to the module the controller lives in, or sprout itself
  89.   * @return string 'sprout' or 'modules/AwesomeModule'
  90.   */
  91. public function getModulePath()
  92. {
  93. $path = Sprout::determineFilePath(get_called_class());
  94. $path = preg_replace('|^' . preg_quote(DOCROOT, '|') . '|', '', $path);
  95. $parts = explode('/', $path);
  96. if (count($parts) < 2) throw new Exception("Where am I?");
  97. if ($parts[0] == 'sprout') return 'sprout';
  98. if ($parts[0] != 'modules') throw new Exception("Where am I?");
  99. return implode('/', array_slice($parts, 0, 2));
  100. }
  101.  
  102.  
  103. /**
  104.   * Return the class name for this controller, expressed in CSS style, i.e. with dashes
  105.   *
  106.   * Example: When called from BlogPostController --> 'blog-post-controller'
  107.   *
  108.   * @return string Name of this PHP class, in a format suitable for use in CSS
  109.   */
  110. public function getCssClassName()
  111. {
  112. $class_name = Sprout::removeNs(get_class($this));
  113. $class_name = Text::camel2lc($class_name);
  114. $class_name = str_replace('_', '-', $class_name);
  115. return $class_name;
  116. }
  117. }