/** * @param string $fullPath * @param string $relPath * @return string */ public function compile($fullPath, $relPath) { $this->_initCompiler(); $fullPath = FS::real($fullPath); $result = $this->_compile($fullPath, $relPath); return $result; }
protected function setUp() { $_SERVER['HTTP_HOST'] = 'example.com'; $_SERVER['SERVER_PORT'] = '80'; FS::rmdir(PROJECT_ROOT . '/cache'); FS::rmdir(PROJECT_ROOT . '/tests/cache'); }
/** * Check action by path. * * @param string $path * @return bool */ public function hasAction($path) { $action = explode('/', $path); $action = end($action); $details = explode('/', $path, 3); if (count($details) <= 2) { return false; } list($type, $vendor, $ns) = $details; if (Plugin::loaded($vendor) === true) { $vendor = FS::clean($vendor, '/'); $nsDetails = explode('/', $ns); $type = $this->_currentType($type); unset($nsDetails[count($nsDetails) - 1]); $namespace = implode('/', $nsDetails); $namespace = FS::clean(implode('\\', [$vendor, $type, $namespace . $type]), '\\'); return method_exists($namespace, $action); } $details = explode('/', $path, 2); list($type, $vendor) = $details; $type = $this->_currentType($type); $nsDetails = explode('/', FS::clean($vendor, '/')); unset($nsDetails[count($nsDetails) - 1]); $defVendor = Configure::read('App.namespace'); $namespace = implode('/', $nsDetails); $namespace = FS::clean(implode('\\', [$defVendor, $type, $namespace . $type]), '\\'); return method_exists($namespace, $action); }
public function setUp() { $root = FS::clean(__DIR__ . '/test', '/'); FS::rmdir($root); mkdir($root, 0777, true); $this->_root = $root; }
/** * Create asset instance. * * @param string $alias * @param mixed $source * @param string|array $dependencies * @param string|array $options * @throws Exception * @return Asset */ public function create($alias, $source, $dependencies = [], $options = []) { $assetType = isset($options['type']) ? $options['type'] : ''; if (isset($this->_customTypes[$assetType])) { $assetType = $this->_customTypes[$assetType]; } elseif (is_callable($source)) { $assetType = 'Callback'; } elseif (is_string($source)) { $ext = strtolower(FS::ext($source)); if ($ext === 'js') { $assetType = 'JsFile'; } elseif ($ext === 'css') { $assetType = 'CssFile'; } elseif ($ext === 'less') { $assetType = 'LessFile'; } elseif ($ext === 'jsx') { $assetType = 'JsxFile'; } } elseif (is_array($source)) { $assetType = 'Collection'; } $className = __NAMESPACE__ . '\\Asset\\' . $assetType; if (class_exists($className)) { $options = is_array($options) ? new Data($options) : $options; return new $className($this->getManager(), $alias, $source, $dependencies, $options); } throw new Exception('Undefined asset type: ' . print_r($source, true)); }
/** * {@inheritdoc} */ public function setImportPath($fullPath, $relPath = null) { $this->_initCompiler(); if (!FS::isDir($fullPath)) { throw new Exception('Undefined import path: ' . $fullPath); } $fullPath = FS::getRelative($fullPath, $this->_options->get('root_path')); $this->_compiler->addImportDir($fullPath); }
protected function setUp() { parent::setUp(); if (!class_exists('\\JBZoo\\Utils\\FS')) { throw new Exception('jbzoo/utils required for CovCatcher unit-tests'); } FS::rmdir(PROJECT_BUILD . '/coverage_cov'); FS::rmdir(PROJECT_BUILD . '/coverage_html'); FS::rmdir(PROJECT_BUILD . '/coverage_xml'); }
/** * @return string */ protected function _getResultFile() { // Normalize relative path $relPath = Slug::filter($this->_hash, '_'); $relPath = Str::low($relPath); // Gett full clean path $fullPath = FS::real($this->_options->get('cache_path')) . '/' . $relPath . '.css'; $fullPath = FS::clean($fullPath); return $fullPath; }
/** * {@inheritdoc} */ public function setImportPath($fullPath, $relPath = null) { $this->_initCompiler(); $relPath = $relPath ?: $this->_options->get('root_url'); if (!FS::isDir($fullPath)) { throw new Exception('Undefined import path: ' . $fullPath); } $importPaths = \Less_Parser::$options['import_dirs']; $importPaths[$fullPath] = $relPath; $this->_compiler->SetImportDirs($importPaths); }
/** * Get current plugin name. * * @param string $plugin * @return string */ public static function getName($plugin = 'Union/Core') { $plugin = FS::clean($plugin, '/'); $name = $plugin; if (strpos($plugin, '/')) { list(, $name) = explode('/', $plugin); if (Str::low($name) == 'core') { $name = 'union'; } } return Inflector::camelize($name); }
/** * Register commands * * @param $commandsDir * @return bool */ protected function _registerCommands($commandsDir) { $files = FS::ls($commandsDir); if (empty($files)) { return false; } foreach ($files as $file) { require_once $file; $reflection = new \ReflectionClass(__NAMESPACE__ . '\\Command\\' . FS::filename($file)); if ($reflection->isSubclassOf('Symfony\\Component\\Console\\Command\\Command') && !$reflection->isAbstract()) { $this->add($reflection->newInstance()); } } return true; }
/** * @param int $count * @return array */ public function runTest($count = 1) { gc_collect_cycles(); // Forces collection of any existing garbage cycles $this->_profiler->start(); for ($i = 0; $i < $count; $i++) { // Store the result so it appears in memory profiling $this->_executeTest(); } $this->_profiler->stop(); $time = $this->_profiler->getTime(); $timeOne = $this->_profiler->getTime() / $count; $memory = $this->_profiler->getMemory(); return array('time' => $time, 'time_one' => $timeOne, 'memory' => $memory, 'count' => $count, 'formated' => sprintf("Time: %s/%s; Memory: %s; Count: %s", Timer::formatMS($time), Timer::formatMS($timeOne), FS::format($memory), $count)); }
/** * Create img tag. * * @param string $src * @param string|array $class * @param string $id * @param array $attrs * @return string */ public function render($src, $class = '', $id = '', array $attrs = array()) { $attrs['class'] = false; $attrs = array_merge(array('fullUrl' => true), $attrs); $attrs['id'] = $id; $attrs = $this->_normalizeClassAttr($attrs, $this->_jbSrt('image')); if ($class !== '') { $attrs = $this->_normalizeClassAttr($attrs, $class); } $attrs['class'] = Str::clean($attrs['class']); $isFull = $attrs['fullUrl']; unset($attrs['fullUrl']); $src = FS::clean($src, '/'); $attrs['src'] = $isFull ? Url::root() . '/' . $src : $src; return '<img ' . $this->buildAttrs($attrs) . ' />'; }
protected function setUp() { parent::setUp(); if (!class_exists('\\JBZoo\\Utils\\Sys')) { throw new Exception('jbzoo/utils required for HttpServer unit-tests'); } if (!class_exists('\\JBZoo\\HttpClient\\HttpClient')) { throw new Exception('jbzoo/http-client required for HttpServer unit-tests'); } if (Sys::isPHP53() || Sys::isHHVM()) { skip('PHP 5.3.x/7.0/hhvm doen\'t support built-in web-server'); } FS::rmdir(PROJECT_BUILD . '/coverage_cov'); FS::rmdir(PROJECT_BUILD . '/coverage_html'); FS::rmdir(PROJECT_BUILD . '/coverage_xml'); }
public function testFunctionOverhead() { runBench(array('Clean' => function () { return pathinfo(__FILE__, PATHINFO_BASENAME); }, 'Wrapper' => function () { return FS::base(__FILE__); }), array('name' => 'Pathinfo overhead', 'count' => 10000)); runBench(array('Vars::get' => function () { return Vars::get($GLOBALS['somevar']); }, 'isset' => function () { return isset($GLOBALS['somevar']); }), array('name' => 'Isset overhead', 'count' => 10000)); $randArr = array_fill(0, 100, null); for ($i = 0; $i < 100; $i += 1) { $randArr[$i] = mt_rand(0, 9); } runBench(array('array_keys(+flip)' => function () use($randArr) { return Arr::unique($randArr, false); }, 'array_unique' => function () use($randArr) { return Arr::unique($randArr, true); }), array('name' => 'Isset overhead', 'count' => 1000)); }
/** * Auto include plugin assets. * * @return void */ protected function _autoAssets() { $path = 'assets'; $request = $this->request; $prefix = $request->param('prefix') ? $request->param('prefix') . '/' : null; if ($plugin = (string) $request->param('plugin')) { $path = $plugin . ':assets'; } $this->Html->less(FS::clean($path . '/less/' . $prefix . '/styles.less'), ['block' => 'css_bottom']); $this->Html->css(FS::clean($path . '/css/' . $prefix . '/styles.css'), ['fullBase' => true, 'block' => 'css_bottom']); $this->Html->script(FS::clean($path . '/js/' . $prefix . '/scripts.js'), ['fullBase' => true, 'block' => 'script_bottom']); }
/** * Returns the resources (time, memory) of the request as a string. * * @param bool $getPeakMemory * @param bool $isRealMemory * @return string */ public static function resourceUsage($getPeakMemory = true, $isRealMemory = false) { if ($getPeakMemory) { $message = 'Time: %s, Peak memory: %s'; $memory = memory_get_peak_usage($isRealMemory); } else { $message = 'Time: %s, Memory: %s'; $memory = memory_get_usage($isRealMemory); } $memory = FS::format($memory, 2); $time = Timer::format(Timer::timeSinceStart()); return sprintf($message, $time, $memory); }
/** * Executes the current command. * * @param InputInterface $input * @param OutputInterface $output * @return void */ protected function execute(InputInterface $input, OutputInterface $output) { $this->_executePrepare($input, $output); $this->_init(); $this->_setupConfigure(); $this->_jbyml->init(); $totalItems = $this->_jbyml->getTotal(); $filePath = $this->_getFilePath() . '/' . $this->_getProfFileName() . '.xml'; $fullPath = FS::clean(JBZOO_CLI_JOOMLA_ROOT . '/' . $filePath); $stepMode = $this->_getOpt('stepmode'); $stepSize = $this->_config->find('params.step_size', 25); $this->_showProfiler('YML Export - prepared'); $this->_('YML File: ' . $fullPath, 'Info'); $this->_('Total items: ' . $totalItems, 'Info'); $this->_('Step size: ' . $stepSize, 'Info'); $this->_('Step mode: ' . ($stepMode ? 'on' : 'off'), 'Info'); $this->_progressWrap($this->_commandName, $totalItems, $stepSize, function () use($totalItems, $stepSize) { $this->_jbyml->renderStart(); $this->_progressBar('yml-export', $totalItems, $stepSize, function ($currentStep, $stepSize) { $offset = $stepSize * $currentStep; $this->_jbyml->exportItems($offset, $stepSize); }); return true; }, function ($step) use($stepSize) { $offset = $stepSize * $step; if ($step == 0) { $this->_jbyml->renderStart(); } $this->_jbyml->exportItems($offset, $stepSize); }, function ($isFinished) { if ($isFinished) { $this->_jbyml->renderFinish(); $this->_showProfiler('Import - finished'); } }); }
/** * Normilize paths and compare them * * @param string $expected * @param string $actual * @param string $message * @throws Exception */ function isSamePath($expected, $actual, $message = null) { if (!class_exists('\\JBZoo\\Utils\\Filter')) { throw new Exception('jbzoo/utils required for isSamePath() function'); } $cleanFunc = function ($paths) { $return = array(); $paths = (array) $paths; foreach ($paths as $key => $path) { $return[$key] = FS::clean($path, '/'); } return $return; }; $expected = Filter::_($expected, $cleanFunc); $actual = Filter::_($actual, $cleanFunc); isSame($expected, $actual, $message); }
/** * Format the results, rounding numbers, showing difference percentages * and removing a flat time based on the benchmark overhead * * @param array $results array($name => array('time' => 1.0)) * @return array array(array('Test' => $name, 'Time' => '1000 ms', 'Perc' => '100 %')) */ public function formatResults(array $results) { uasort($results['list'], function ($testOne, $testTwo) { if ($testOne['time'] === $testTwo['time']) { return 0; } else { return $testOne['time'] < $testTwo['time'] ? -1 : 1; } }); $minTime = INF; $minMemory = INF; foreach ($results['list'] as $name => $result) { // time $time = $result['time']; //$time -= $this->_overhead['time']; // Substract base_time $results['list'][$name]['time'] = $time; $minTime = min($minTime, $time); // memory $memory = $results['list'][$name]['memory']; $memory -= $this->_overhead['memory']; $results['list'][$name]['memory'] = $memory; $minMemory = min($minMemory, $memory); } $output = array(); $isOne = count($results['list']) === 1; foreach ($results['list'] as $name => $result) { if ($isOne) { $output[] = array(self::COL_NAME => $name, self::COL_TIME => $this->_timeFormat($result['time'], 0), self::COL_TIME_ONE => $this->_timeFormat($result['time'] / $this->_count), self::COL_MEMORY => FS::format($result['memory'], 2)); } else { $output[] = array(self::COL_NAME => $name, self::COL_TIME => $this->_timeFormat($result['time'], 0), self::COL_TIME_ONE => $this->_timeFormat($result['time'] / $this->_count), self::COL_TIME_REL => Vars::relativePercent($minTime, $result['time']), self::COL_MEMORY => FS::format($result['memory'], 2), self::COL_MEMORY_REL => Vars::relativePercent($minMemory, $result['memory'])); } } return $output; }
/** * Determine textbox size * * @param string $fontSize * @param int $angle * @param string $fontFile * @param string $text * @return array * * @throws Exception */ protected static function _getTextboxSize($fontSize, $angle, $fontFile, $text) { // Determine textbox size $fontPath = FS::clean($fontFile); if (!FS::isFile($fontPath)) { throw new Exception('Unable to load font: ' . $fontFile); } $box = imagettfbbox($fontSize, $angle, $fontFile, $text); $boxWidth = abs($box[6] - $box[2]); $boxHeight = abs($box[7] - $box[1]); return array($boxWidth, $boxHeight); }
/** * Load an image * * @param string $filename Path to image file * @return $this * * @throws Exception */ public function loadFile($filename) { $cleanFilename = FS::clean($filename); if (!FS::isFile($cleanFilename)) { throw new Exception('Image file not forund: ' . $filename); } $this->cleanup(); $this->_filename = $cleanFilename; $this->_loadMeta(); return $this; }
public function testGetRoot() { $dir = __DIR__; $path = new Path(); $path->setRoot($dir); $manager = new Manager($path); isSame(FS::clean($dir), FS::clean($manager->getPath()->getRoot())); }
<?php /** * JBZoo CCK Cli * * This file is part of the JBZoo CCK package. * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. * * @package CCK Cli * @license MIT * @copyright Copyright (C) JBZoo.com, All rights reserved. * @link https://github.com/JBZoo/CCK-Cli * @author Denis Smetannikov <*****@*****.**> */ use JBZoo\Utils\FS; // Define important constatns define('JBZOO_CLI', true); // simple security define('JBZOO_CLI_ROOT', FS::real(__DIR__ . '/../')); !defined('DIRECTORY_SEPERATOR') && define('DIRECTORY_SEPERATOR', '/'); // Try to find Joomla root path $jrootPath = FS::real(__DIR__ . '/../../../'); $jconfigPath = FS::real($jrootPath . '/configuration.php'); $path = $jconfigPath ? $jrootPath : getenv('JOOMLA_DEV_PATH'); // placeholder for developer if (!$path) { throw new Exception('Joomla Root Path is not found!'); } define('JBZOO_CLI_JOOMLA_ROOT', FS::real($path));
/** * Find theme path. * * @param string $name * @return null|string */ protected static function _find($name) { $paths = App::path('Plugin'); foreach ($paths as $path) { $path = FS::clean($path . '/', DS); $details = explode(DS, rtrim($path, DS)); $folder = Str::trim(array_pop($details)); $themeFolder = $path . $name; if (Arr::in($folder, self::$_skipFolder) || !FS::isDir($themeFolder)) { $themeFolder .= self::POSTFIX; } if (FS::isDir($themeFolder)) { return $themeFolder; } } return null; }
/** * Forced clean path with linux-like sleshes * * @param string $path * @return string */ protected function _clean($path) { return FS::clean($path, '/'); }
/** * Progress wrapper (if use stepmode). * * @param string $name * @param int $total * @param int $stepSize * @param \Closure $onStart * @param \Closure $onStepMode * @param \Closure $onFinish */ protected function _progressWrap($name, $total, $stepSize, $onStart, $onStepMode, $onFinish) { $_this = $this; $step = $this->_getOpt('step'); $profile = $this->_getOpt('profile'); $stepMode = $this->_getOpt('stepmode'); $isFinished = false; if ($stepMode) { if ($step >= 0) { $onStepMode($step); } else { $this->_progressBar($name, $total, $stepSize, function ($currentStep) use($profile, $total, $_this, $name) { $phpBin = Env::getBinary(); $binPath = './' . FS::getRelative($_SERVER['SCRIPT_FILENAME'], JPATH_ROOT, '/'); $options = array('profile' => $profile, 'step' => (int) $currentStep, 'stepmode' => '', 'q' => ''); $command = $phpBin . ' ' . $binPath . ' ' . $name; $result = Cli::exec($command, $options, JPATH_ROOT, false); if (0 && $this->_isDebug()) { $_this->_($result); } return $currentStep <= $total; }); $isFinished = true; } } else { $isFinished = $onStart(); } $onFinish($isFinished); }
/** * Adds a timestamp to a file based resource based on the value of `Asset.timestamp` in Configure. * * @param string $path * @return null|string */ public function assetTimestamp($path) { if ($this->_timeStampEnable() && strpos($path, '?') === false) { $Path = $this->_View->getPath(); if ($Path->isVirtual($path)) { $path = $Path->get($path); } if (FS::isFile($path)) { //@codingStandardsIgnoreStart return $path . '?' . @filemtime($path); //@codingStandardsIgnoreEnd } } return $path; }
/** * @param array $options * @return Data * @throws Exception * @SuppressWarnings(PHPMD.Superglobals) */ protected function _prepareOptions(array $options) { // Default data for current system $this->_default['root_url'] = Url::root(); $this->_default['root_path'] = Sys::getDocRoot(); $options = array_merge($this->_default, $options); // Check cache directory $cachePath = FS::clean($options['cache_path']); if (!$cachePath) { throw new Exception('Option "cache_path" is empty!'); } if (!FS::isDir($cachePath)) { mkdir($cachePath, 0755, true); } $options['cache_path'] = FS::real($cachePath); $options['root_url'] = rtrim($options['root_url'], '/'); $options['root_path'] = FS::real($options['root_path']); $options['driver'] = ucfirst(strtolower(trim($options['driver']))); // Check mixin paths $lessFile = (array) $options['autoload']; foreach ($lessFile as $key => $mixin) { $lessFile[$key] = FS::real($mixin); } $options['autoload'] = array_filter($lessFile); // Check imported paths $importPaths = []; foreach ((array) $options['import_paths'] as $path => $uri) { if ($cleanPath = FS::real($path)) { $importPaths[$cleanPath] = $uri; } } $importPaths[$options['root_path']] = $options['root_url']; // Forced add root path in the end of list! $options['import_paths'] = array_filter($importPaths); return new Data($options); }
public function testHasCDBack() { $path = Path::getInstance(__METHOD__); $paths = array($this->_root, $this->_root . '/..', $this->_root . '/../../'); $path->set('default', $paths); list($path1, $path2, $path3) = $paths; $expected = array(realpath(FS::clean($path3)), realpath(FS::clean($path2)), FS::clean($path1, '/')); $this->_is($expected, $path->getPaths('default')); }