/** * To create an application structure from templates * * @param string $config Array of config params. * ~~~ * $config = [ * 'basePath' => '/path/to/copy/template/files', * 'appTemplates' => [ * 'templatesPath' => '/path/to/templates/directory', * 'params' => [ an optional array of any extra params ], * ], * 'appInfo' => [ an optional array of any extra params ] * ] * ~~~ * @return null */ public static function createApplication($config) { //--- Check config: if (!is_array($config)) { self::log(_('Config mast be an array!'), 'error'); return -1; } //--- Include application templates: if ($config['appTemplates'] && is_string($config['appTemplates'])) { $config['appTemplates'] = (require $config['appTemplates']); } if (!is_array($config['appTemplates'])) { self::log(_('appTemplates mast be an array!'), 'error'); return -1; } //--- Extract and prepare expected params: $params = array_merge(Basic::pathValue($config['appInfo'], null, '[', ']'), Basic::pathValue($config['appTemplates']['params'], null, '[', ']')); //print_r($params); //--- Get Application templates structure: $applicationBase = $config['basePath']; $templateBase = $config['appTemplates']['templatesPath']; $templateFiles = Files::glob($templateBase, '{,.}*', GLOB_BRACE, true); foreach ($templateFiles as $srcFile) { $file = preg_replace('/^' . preg_quote($templateBase . '/', '/') . '/', '', $srcFile); $dstFile = $applicationBase . '/' . $file; $result = null; try { //--- Skip an exists file(dir): if (file_exists($dstFile)) { throw new \Exception(_('SKIPPED') . ' ' . _('destination is exists'), -2); } //--- Copy file: if (is_file($srcFile)) { $dstDir = dirname($dstFile); if (!file_exists($dstDir) && !mkdir($dstDir, 0775, true)) { throw new \Exception(_('cant create a directory')); } //--- Load source content: $content = file_get_contents($srcFile); //--- Replace shortcodes [...key...] to values: $content = strtr($content, $params); //--- Save content to destination: if (file_put_contents($dstFile, $content) === false) { throw new \Exception(_('cant copy a file')); } $message = _('OK') . ' ' . _('file copied'); } //--- Create a directory: if (is_dir($srcFile)) { if (mkdir($dstFile, 0775, true)) { $message = _('OK') . ' ' . _('directory created'); } else { throw new \Exception(_('cant create a directory')); } } } catch (\Exception $e) { $message = $e->getMessage(); $result = 'error'; if ($e->getCode() == -2) { $result = 'warn'; } } self::log(_('source') . ': [' . $file . "]\t" . $message . "\t[" . $dstFile . ']', $result); } }
/** * To find all methods of class into a source code of a given class * * @param string $data Text data contains a class source code. * @return <i>array</i> Array structure: * ~~~ * array( * "method1" => array( * "type" => static | abstract | "", * "scope" => private | protected | private , * "params" => "parameters of methods" * ) * "method2" => array( ... ), * ... * ) * ~~~ */ public static function getClassMethods($data) { if (preg_match_all('/^\\s*([\\w\\s]*)\\s*function\\s+([\\w_]+)\\s*\\(([^\\{]*)\\)[^\\{\\)]*\\{/ms', $data, $m)) { foreach ($m[1] as $i => $value) { $methodProp = $m[1][$i]; $methodScop = Basic::inPatterns($methodProp, array("public", "protected", "private"), true); if (!$methodScop) { $methodScop = "public"; } $methodType = Basic::inPatterns($methodProp, array("abstract"), true); if (!$methodType) { $methodType = Basic::inPatterns($methodProp, array("static"), true); } $methodName = $m[2][$i]; $methodPara = $m[3][$i]; $methods[$methodName] = array("scope" => $methodScop, "type" => $methodType, "params" => $methodPara); } } return $methods; }
/** * Extends PHP glob with recursion and base path (files only) * * @param string|array $paths Paths to find files (base paths). * Default = system current directory. * @param string|array $filePatterns RegExp patterns to filter files (to find files). * Default = "" - all files. * @param string|array $excludePatterns RegExp patterns to filter files (to ignore files). * Default = NULL - no files to exclusion. * @param boolean $recursive =TRUE - find files in sub directories. * Default = TRUE. * @return array A hash array of found files. Hash structure: * @code * array( * "/absolute/file/name1" => "/base/path" * "/absolute/file/name2" => "/base/path" * ... * ) * @endcode * * Exapmle: * @code print_r( Files::globFiles('/base/path/', '\.php$') ); //-------- // Return: Array ( '/base/path/file1.php' => '/base/path', '/base/path/file2.php' => '/base/path', '/base/path/folder1/file1_in_folder1.php' => '/base/path', '/base/path/folder1/file2_in_folder1.php' => '/base/path', '/base/path/folder1/subfolder1/file1_in_subfolder1.php' => '/base/path', ) * @endcode */ public static function globFiles($paths = array(''), $filePatterns = array(''), $excludePatterns = NULL, $recursive = true) { //--- Convert $paths to array: if (!is_array($paths)) { $paths = array($paths); } //--- Convert $filePatterns to array: if (!is_array($filePatterns)) { $filePatterns = array($filePatterns); } //--- Convert $excludePatterns to array: if ($excludePatterns && !is_array($excludePatterns)) { $excludePatterns = array($excludePatterns); } //--- Get all source paths: $sourcePaths = array(); foreach ($paths as $sourcePath) { $sourcePath = realpath($sourcePath); $sourcePaths[$sourcePath] = $sourcePath; if ($sourcePath && is_array($out = self::glob($sourcePath, "*", GLOB_ONLYDIR, $recursive))) { foreach ($out as $path) { $sourcePaths[$path] = $sourcePath; } } } //--- Get all source files and filter it: foreach ($sourcePaths as $path => $base) { if (is_array($files = self::glob($path))) { foreach ($files as $file) { if (!is_file($file)) { continue; } $name = basename($file); if (Basic::inPatterns($name, $filePatterns) && !Basic::inPatterns($name, $excludePatterns)) { $outSources[$file] = $base; } } } } return $outSources; }
/** * Init the class */ private function init($config) { //--- Init the class: Basic::initClass($this, $config); }
/** * Generate common file body to execute all tests * * @param array $destinations List of destination file names. * @param string $destinationRoot Root path for destinations. * @return string A source code (text) of run-file. */ public function generateRunFile($destinations, $destinationRoot) { if (!is_array($destinations)) { return false; } $destinationRoot = realpath($destinationRoot); $body = Basic::replace($this->runFileHeaderTemplate, array("DATE" => date("Y.m.d H:i"), "GENERATOR_NAME" => get_class(), "RUN_DIR" => $destinationRoot)); foreach ($destinations as $file => $_) { $testStruct = SimpleCodeParser::parseCode(file_get_contents($file)); if (is_array($testStruct)) { foreach ($testStruct as $nameSpace) { if (is_array($nameSpace["classes"])) { foreach ($nameSpace["classes"] as $className => $value) { $testName = $className; $path = Files::getRelativePath(dirname($file), $destinationRoot); $fileName = "." . $path["relative"] . "/" . basename($file); $logName = "." . $path["relative"] . "/" . $testName; $body .= Basic::replace($this->runFileLineTemplate, array("TEST_TITLE" => $testName, "TEST_NAME" => $testName, "TEST_FILE" => $fileName, "TEST_LOG" => $logName, "PHPUNIT" => $this->phpUnitFile)); } } } } } return $body; }
/** * Init the class */ protected function init($config) { //--- Init the class: Basic::initClass($this, $config); //--- Create loggers: if (!is_object($this->debugLogger) || isset($config['debug'])) { $this->debugLogger = new Debug($this->debug); } if (!is_object($this->errorLogger) || isset($config['error'])) { $this->errorLogger = new Logger($this->error); } }
/** * Init the class */ protected function init($config) { //--- Init the class: Basic::initClass($this, $config); //--- Reset: $this->agentStatus = null; $this->agentVersion = null; $this->agentReadCommunity = null; $this->setError(); snmp_set_oid_output_format($this->oidOutputFormat); }