SproutCMS

This is the code documentation for the SproutCMS project

source of /sprout/Helpers/SproutVariable.php

  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.  
  14. namespace Sprout\Helpers;
  15.  
  16. use BadMethodCallException;
  17. use Closure;
  18. use Exception;
  19. use karmabunny\kb\PropertiesTrait;
  20. use Kohana;
  21. use Sprout\Exceptions\FileMissingException;
  22. use Twig\Markup;
  23.  
  24. /**
  25.  *
  26.  */
  27. class SproutVariable
  28. {
  29. use PropertiesTrait;
  30.  
  31. static protected $extra = [];
  32.  
  33. public $url;
  34. public $request;
  35. public $skin;
  36. public $needs;
  37. public $enc;
  38. public $session;
  39. public $navigation;
  40. public $notification;
  41. public $widgets;
  42. public $social;
  43. public $replace;
  44. public $file;
  45. public $lnk;
  46. public $admin;
  47.  
  48. public function __construct()
  49. {
  50. $this->url = new Url();
  51. $this->request = new Request();
  52. $this->skin = new Skin();
  53. $this->needs = new Needs();
  54. $this->enc = new Enc();
  55. $this->session = new Session();
  56. $this->navigation = new Navigation();
  57. $this->notification = new Notification();
  58. $this->widgets = new Widgets();
  59. $this->social = [
  60. 'meta' => new SocialMeta(),
  61. 'networking' => new SocialNetworking(),
  62. ];
  63. $this->replace = new ContentReplace();
  64. $this->file = new File();
  65. $this->lnk = new Lnk();
  66. $this->admin = new AdminAuth();
  67. }
  68.  
  69.  
  70. public function getParams()
  71. {
  72. return $_GET;
  73. }
  74.  
  75.  
  76. public function getParam($name)
  77. {
  78. return $_GET[$name] ?? '';
  79. }
  80.  
  81.  
  82. public function getQueryString()
  83. {
  84. return Router::$query_string;
  85. }
  86.  
  87.  
  88. public function getPath()
  89. {
  90. return Router::$current_uri;
  91. }
  92.  
  93.  
  94. public function include($name, $data = [])
  95. {
  96. return new Markup(View::include($name, $data), 'UTF-8');
  97. }
  98.  
  99.  
  100. public function require($path)
  101. {
  102. $matches = [];
  103.  
  104. if (!preg_match('!^(.+)(\.[^.]+)$!', $path, $matches)) {
  105. throw new FileMissingException("File missing: {$path}");
  106. }
  107.  
  108. [$_, $name, $extension] = $matches;
  109. $path = Skin::findTemplate($name, $extension);
  110. return new Markup(file_get_contents(DOCROOT . $path), 'UTF-8');
  111. }
  112.  
  113.  
  114. public function config($name, $slash = false, $required = true)
  115. {
  116. return Kohana::config($name, $slash, $required);
  117. }
  118.  
  119.  
  120. public function __call($name, $arguments)
  121. {
  122. $item = self::$extra[$name] ?? null;
  123.  
  124. if (!$item) {
  125. throw new BadMethodCallException("Variable {$name} does not exist");
  126. }
  127.  
  128. if (is_object($item) and $item instanceof Closure) {
  129. return $item(...$arguments);
  130. }
  131.  
  132. if (!is_object($item) and is_callable($item)) {
  133. return $item(...$arguments);
  134. }
  135.  
  136. return $item;
  137. }
  138.  
  139.  
  140. public static function register($name, $item)
  141. {
  142. $properties = self::getProperties();
  143. if (in_array($name, $properties)) {
  144. throw new Exception("Cannot register reserved variable '{$name}'");
  145. }
  146.  
  147. self::$extra[$name] = $item;
  148. }
  149. }
  150.