API
6 years ago
Access
6 years ago
Application
6 years ago
Archive
6 years ago
ArchiveProcessor
6 years ago
Archiver
6 years ago
AssetManager
6 years ago
Auth
6 years ago
Category
6 years ago
CliMulti
6 years ago
Columns
6 years ago
Composer
6 years ago
Concurrency
6 years ago
Config
6 years ago
Container
6 years ago
CronArchive
6 years ago
DataAccess
6 years ago
DataFiles
6 years ago
DataTable
6 years ago
Db
6 years ago
DeviceDetector
6 years ago
Email
6 years ago
Exception
6 years ago
Http
6 years ago
Intl
6 years ago
Mail
6 years ago
Measurable
6 years ago
Menu
6 years ago
Metrics
6 years ago
Notification
6 years ago
Period
6 years ago
Plugin
6 years ago
ProfessionalServices
6 years ago
Report
6 years ago
ReportRenderer
6 years ago
Scheduler
6 years ago
Segment
6 years ago
Session
6 years ago
Settings
6 years ago
Tracker
6 years ago
Translation
6 years ago
UpdateCheck
6 years ago
Updater
6 years ago
Updates
6 years ago
Validators
6 years ago
View
6 years ago
ViewDataTable
6 years ago
Visualization
6 years ago
Widget
6 years ago
.htaccess
6 years ago
Access.php
6 years ago
Archive.php
6 years ago
ArchiveProcessor.php
6 years ago
AssetManager.php
6 years ago
Auth.php
6 years ago
BaseFactory.php
6 years ago
Cache.php
6 years ago
CacheId.php
6 years ago
CliMulti.php
6 years ago
Common.php
6 years ago
Config.php
6 years ago
Console.php
6 years ago
Context.php
6 years ago
Cookie.php
6 years ago
CronArchive.php
6 years ago
DataArray.php
6 years ago
DataTable.php
6 years ago
Date.php
6 years ago
Db.php
6 years ago
DbHelper.php
6 years ago
Development.php
6 years ago
DeviceDetectorFactory.php
6 years ago
ErrorHandler.php
6 years ago
EventDispatcher.php
6 years ago
ExceptionHandler.php
6 years ago
FileIntegrity.php
6 years ago
Filechecks.php
6 years ago
Filesystem.php
6 years ago
FrontController.php
6 years ago
Http.php
6 years ago
IP.php
6 years ago
Log.php
6 years ago
LogDeleter.php
6 years ago
Mail.php
6 years ago
Metrics.php
6 years ago
MetricsFormatter.php
6 years ago
Nonce.php
6 years ago
Notification.php
6 years ago
NumberFormatter.php
6 years ago
Option.php
6 years ago
Period.php
6 years ago
Piwik.php
6 years ago
Plugin.php
6 years ago
Profiler.php
6 years ago
ProxyHeaders.php
6 years ago
ProxyHttp.php
6 years ago
QuickForm2.php
6 years ago
RankingQuery.php
6 years ago
Registry.php
6 years ago
ReportRenderer.php
6 years ago
ScheduledTask.php
6 years ago
Segment.php
6 years ago
Sequence.php
6 years ago
Session.php
6 years ago
SettingsPiwik.php
6 years ago
SettingsServer.php
6 years ago
Singleton.php
6 years ago
Site.php
6 years ago
TCPDF.php
6 years ago
TaskScheduler.php
6 years ago
Theme.php
6 years ago
Timer.php
6 years ago
Tracker.php
6 years ago
Translate.php
6 years ago
Twig.php
6 years ago
Unzip.php
6 years ago
UpdateCheck.php
6 years ago
Updater.php
6 years ago
Updates.php
6 years ago
Url.php
6 years ago
UrlHelper.php
6 years ago
Version.php
6 years ago
View.php
6 years ago
bootstrap.php
6 years ago
dispatch.php
6 years ago
testMinimumPhpVersion.php
6 years ago
Development.php
202 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Piwik - free/libre analytics platform |
| 4 | * |
| 5 | * @link https://matomo.org |
| 6 | * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later |
| 7 | * |
| 8 | */ |
| 9 | |
| 10 | namespace Piwik; |
| 11 | |
| 12 | use Exception; |
| 13 | use Piwik\Container\StaticContainer; |
| 14 | use Psr\Log\LoggerInterface; |
| 15 | |
| 16 | /** |
| 17 | * Development related checks and tools. You can enable/disable development using `./console development:enable` and |
| 18 | * `./console development:disable`. The intention of the development mode and this class is to support the developer |
| 19 | * as much as possible by doing some additional checks if the development mode is enabled. For instance if a developer |
| 20 | * has to register any class/method we can make sure whether they actually exist and if not display a useful error |
| 21 | * message. This helps the user to find for instance simple typos and makes sure it will actually work even if he |
| 22 | * forgets to test it. |
| 23 | */ |
| 24 | class Development |
| 25 | { |
| 26 | private static $isEnabled = null; |
| 27 | |
| 28 | /** |
| 29 | * Returns `true` if development mode is enabled and `false` otherwise. |
| 30 | * |
| 31 | * @return bool |
| 32 | */ |
| 33 | public static function isEnabled() |
| 34 | { |
| 35 | if (is_null(self::$isEnabled)) { |
| 36 | self::$isEnabled = (bool) Config::getInstance()->Development['enabled']; |
| 37 | } |
| 38 | |
| 39 | return self::$isEnabled; |
| 40 | } |
| 41 | |
| 42 | /** |
| 43 | * Verifies whether a className of object implements the given method. It does not check whether the given method |
| 44 | * is actually callable (public). |
| 45 | * |
| 46 | * @param string|object $classOrObject |
| 47 | * @param string $method |
| 48 | * |
| 49 | * @return bool true if the method exists, false otherwise. |
| 50 | */ |
| 51 | public static function methodExists($classOrObject, $method) |
| 52 | { |
| 53 | if (is_string($classOrObject)) { |
| 54 | return class_exists($classOrObject) && method_exists($classOrObject, $method); |
| 55 | } |
| 56 | |
| 57 | return method_exists($classOrObject, $method); |
| 58 | } |
| 59 | |
| 60 | /** |
| 61 | * Formats a method call depending on the given class/object and method name. It does not perform any checks whether |
| 62 | * does actually exists. |
| 63 | * |
| 64 | * @param string|object $classOrObject |
| 65 | * @param string $method |
| 66 | * |
| 67 | * @return string Formatted method call. Example: "MyNamespace\MyClassname::methodName()" |
| 68 | */ |
| 69 | public static function formatMethodCall($classOrObject, $method) |
| 70 | { |
| 71 | if (is_object($classOrObject)) { |
| 72 | $classOrObject = get_class($classOrObject); |
| 73 | } |
| 74 | |
| 75 | return $classOrObject . '::' . $method . '()'; |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * Checks whether the given method is actually callable on the given class/object if the development mode is |
| 80 | * enabled. En error will be triggered if the method does not exist or is not callable (public) containing a useful |
| 81 | * error message for the developer. |
| 82 | * |
| 83 | * @param string|object $classOrObject |
| 84 | * @param string $method |
| 85 | * @param string $prefixMessageIfError You can prepend any string to the error message in case the method is not |
| 86 | * callable. |
| 87 | */ |
| 88 | public static function checkMethodIsCallable($classOrObject, $method, $prefixMessageIfError) |
| 89 | { |
| 90 | if (!self::isEnabled()) { |
| 91 | return; |
| 92 | } |
| 93 | |
| 94 | self::checkMethodExists($classOrObject, $method, $prefixMessageIfError); |
| 95 | |
| 96 | if (!self::isCallableMethod($classOrObject, $method)) { |
| 97 | self::error($prefixMessageIfError . ' "' . self::formatMethodCall($classOrObject, $method) . '" is not callable. Please make sure to method is public'); |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | /** |
| 102 | * Checks whether the given method is actually callable on the given class/object if the development mode is |
| 103 | * enabled. En error will be triggered if the method does not exist or is not callable (public) containing a useful |
| 104 | * error message for the developer. |
| 105 | * |
| 106 | * @param string|object $classOrObject |
| 107 | * @param string $method |
| 108 | * @param string $prefixMessageIfError You can prepend any string to the error message in case the method is not |
| 109 | * callable. |
| 110 | */ |
| 111 | public static function checkMethodExists($classOrObject, $method, $prefixMessageIfError) |
| 112 | { |
| 113 | if (!self::isEnabled()) { |
| 114 | return; |
| 115 | } |
| 116 | |
| 117 | if (!self::methodExists($classOrObject, $method)) { |
| 118 | self::error($prefixMessageIfError . ' "' . self::formatMethodCall($classOrObject, $method) . '" does not exist. Please make sure to define such a method.'); |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | /** |
| 123 | * Verify whether the given method actually exists and is callable (public). |
| 124 | * |
| 125 | * @param string|object $classOrObject |
| 126 | * @param string $method |
| 127 | * @return bool |
| 128 | */ |
| 129 | public static function isCallableMethod($classOrObject, $method) |
| 130 | { |
| 131 | if (!self::methodExists($classOrObject, $method)) { |
| 132 | return false; |
| 133 | } |
| 134 | |
| 135 | $reflection = new \ReflectionMethod($classOrObject, $method); |
| 136 | return $reflection->isPublic(); |
| 137 | } |
| 138 | |
| 139 | /** |
| 140 | * Triggers an error if the development mode is enabled. Depending on the current environment / mode it will either |
| 141 | * log the given message or throw an exception to make sure it will be displayed in the Piwik UI. |
| 142 | * |
| 143 | * @param string $message |
| 144 | * @throws Exception |
| 145 | */ |
| 146 | public static function error($message) |
| 147 | { |
| 148 | if (!self::isEnabled()) { |
| 149 | return; |
| 150 | } |
| 151 | |
| 152 | $message .= ' (This error is only shown in development mode)'; |
| 153 | |
| 154 | if (SettingsServer::isTrackerApiRequest() |
| 155 | || Common::isPhpCliMode() |
| 156 | ) { |
| 157 | StaticContainer::get(LoggerInterface::class)->error($message, [ |
| 158 | 'ignoreInScreenWriter' => true, |
| 159 | ]); |
| 160 | } else { |
| 161 | throw new Exception($message); |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | public static function getMethodSourceCode($className, $methodName) |
| 166 | { |
| 167 | $method = new \ReflectionMethod($className, $methodName); |
| 168 | |
| 169 | $file = new \SplFileObject($method->getFileName()); |
| 170 | $offset = $method->getStartLine() - 1; |
| 171 | $count = $method->getEndLine() - $method->getStartLine() + 1; |
| 172 | |
| 173 | $fileIterator = new \LimitIterator($file, $offset, $count); |
| 174 | |
| 175 | $methodCode = "\n " . $method->getDocComment() . "\n"; |
| 176 | foreach ($fileIterator as $line) { |
| 177 | $methodCode .= $line; |
| 178 | } |
| 179 | $methodCode .= "\n"; |
| 180 | |
| 181 | return $methodCode; |
| 182 | } |
| 183 | |
| 184 | public static function getUseStatements($className) |
| 185 | { |
| 186 | $class = new \ReflectionClass($className); |
| 187 | |
| 188 | $file = new \SplFileObject($class->getFileName()); |
| 189 | |
| 190 | $fileIterator = new \LimitIterator($file, 0, $class->getStartLine()); |
| 191 | |
| 192 | $uses = array(); |
| 193 | foreach ($fileIterator as $line) { |
| 194 | if (preg_match('/(\s*)use (.+)/', $line, $match)) { |
| 195 | $uses[] = trim($match[2]); |
| 196 | } |
| 197 | } |
| 198 | |
| 199 | return $uses; |
| 200 | } |
| 201 | } |
| 202 |