Esempio n. 1
0
 private function checkIsValidWidget($name, $method)
 {
     if (!Development::isEnabled()) {
         return;
     }
     if (empty($name)) {
         Development::error('No name is defined for added widget having method "' . $method . '" in ' . get_class($this));
     }
     if (Development::isCallableMethod($this, $method)) {
         return;
     }
     $controllerClass = 'Piwik\\Plugins\\' . $this->getModule() . '\\Controller';
     if (!Development::methodExists($this, $method) && !Development::methodExists($controllerClass, $method)) {
         Development::error('The added method "' . $method . '" neither exists in "' . get_class($this) . '" nor "' . $controllerClass . '". Make sure to define such a method.');
     }
     $definedInClass = get_class($this);
     if (Development::methodExists($controllerClass, $method)) {
         if (Development::isCallableMethod($controllerClass, $method)) {
             return;
         }
         $definedInClass = $controllerClass;
     }
     Development::error('The method "' . $method . '" is not callable on "' . $definedInClass . '". Make sure the method is public.');
 }
Esempio n. 2
0
 /**
  * Copies the given method and all needed use statements into an existing class. The target class name will be
  * built based on the given $replace argument.
  * @param string $sourceClassName
  * @param string $methodName
  * @param array $replace
  */
 protected function copyTemplateMethodToExisitingClass($sourceClassName, $methodName, $replace)
 {
     $targetClassName = $this->replaceContent($replace, $sourceClassName);
     if (Development::methodExists($targetClassName, $methodName)) {
         // we do not want to add the same method twice
         return;
     }
     Development::checkMethodExists($sourceClassName, $methodName, 'Cannot copy template method: ');
     $targetClass = new \ReflectionClass($targetClassName);
     $file = new \SplFileObject($targetClass->getFileName());
     $methodCode = Development::getMethodSourceCode($sourceClassName, $methodName);
     $methodCode = $this->replaceContent($replace, $methodCode);
     $methodLine = $targetClass->getEndLine() - 1;
     $sourceUses = Development::getUseStatements($sourceClassName);
     $targetUses = Development::getUseStatements($targetClassName);
     $usesToAdd = array_diff($sourceUses, $targetUses);
     if (empty($usesToAdd)) {
         $useCode = '';
     } else {
         $useCode = "\nuse " . implode("\nuse ", $usesToAdd) . "\n";
     }
     // search for namespace line before the class starts
     $useLine = 0;
     foreach (new \LimitIterator($file, 0, $targetClass->getStartLine()) as $index => $line) {
         if (0 === strpos(trim($line), 'namespace ')) {
             $useLine = $index + 1;
             break;
         }
     }
     $newClassCode = '';
     foreach (new \LimitIterator($file) as $index => $line) {
         if ($index == $methodLine) {
             $newClassCode .= $methodCode;
         }
         if (0 !== $useLine && $index == $useLine) {
             $newClassCode .= $useCode;
         }
         $newClassCode .= $line;
     }
     file_put_contents($targetClass->getFileName(), $newClassCode);
 }
Esempio n. 3
0
File: Menu.php Progetto: piwik/piwik
 private function checkisValidCallable($module, $action)
 {
     if (!Development::isEnabled()) {
         return;
     }
     $prefix = 'Menu item added in ' . get_class($this) . ' will fail when being selected. ';
     if (!is_string($action)) {
         Development::error($prefix . 'No valid action is specified. Make sure the defined action that should be executed is a string.');
     }
     $reportAction = lcfirst(substr($action, 4));
     if (ReportsProvider::factory($module, $reportAction)) {
         return;
     }
     $controllerClass = '\\Piwik\\Plugins\\' . $module . '\\Controller';
     if (!Development::methodExists($controllerClass, $action)) {
         Development::error($prefix . 'The defined action "' . $action . '" does not exist in ' . $controllerClass . '". Make sure to define such a method.');
     }
     if (!Development::isCallableMethod($controllerClass, $action)) {
         Development::error($prefix . 'The defined action "' . $action . '" is not callable on "' . $controllerClass . '". Make sure the method is public.');
     }
 }