/** * Test on \Includes\Utils\Converter::removeCRLF * * @return void * @see ____func_see____ * @since 1.0.3 */ public function testRemoveCRLF() { $url = $etalonURL = 'admin.php?target=main'; $this->assertEquals($etalonURL, \Includes\Utils\Converter::removeCRLF($url), 'removeCRLF() test #1 failed'); $url = ' admin.php?target=main '; $this->assertEquals($etalonURL, \Includes\Utils\Converter::removeCRLF($url), 'removeCRLF() test #2 failed'); $url = <<<OUT admin.php?target=main OUT; $this->assertEquals($etalonURL, \Includes\Utils\Converter::removeCRLF($url), 'removeCRLF() test #3 failed'); $this->assertEquals('', \Includes\Utils\Converter::removeCRLF(null), 'removeCRLF() test #4 failed'); $this->assertEquals('0', \Includes\Utils\Converter::removeCRLF(0), 'removeCRLF() test #5 failed'); $this->assertEquals('5', \Includes\Utils\Converter::removeCRLF(5), 'removeCRLF() test #6 failed'); }
/** * Returns repository class for model class of current node * * @param \Includes\Decorator\DataStructure\Graph\Classes $node Current node * * @return string */ protected function getRepositoryClass(\Includes\Decorator\DataStructure\Graph\Classes $node) { $repositoryClass = null; if (!($node->isLowLevelNode() || $node->isTopLevelNode() || !$node->isDecorator())) { $children = $node->getChildren(); $repositoryClass = isset($children[0]) ? $this->getRepositoryClass($children[0]) : $this->getDefaultRepositoryClass(''); } else { $repositoryClass = \Includes\Utils\Converter::getPureClassName($node->getClass()); $repositoryClass = \Includes\Utils\Converter::prepareClassName(str_replace('\\Model\\', '\\Model\\Repo\\', $repositoryClass), false); if (!\XLite\Core\Operator::isClassExists($repositoryClass)) { $repositoryClass = $this->getDefaultRepositoryClass($repositoryClass); } } return $repositoryClass; }
/** * Return specific data for address entry. Helper. * * @param \XLite\Model\Address $address Address * @param boolean $showEmpty Show empty fields OPTIONAL * * @return array */ protected function getAddressSectionData(\XLite\Model\Address $address, $showEmpty = false) { $result = array(); $hasStates = $address->getCountry() ? $address->getCountry()->hasStates() : false; foreach (\XLite\Core\Database::getRepo('XLite\\Model\\AddressField')->findAllEnabled() as $field) { $method = 'get' . \Includes\Utils\Converter::convertToCamelCase($field->getViewGetterName() ?: $field->getServiceName()); $addressFieldValue = $address->{$method}(); $cssFieldName = $field->getCSSFieldName(); switch ($field->getServiceName()) { case 'state_id': $addressFieldValue = $hasStates ? $addressFieldValue : null; if (null === $addressFieldValue && $hasStates) { $addressFieldValue = $address->getCustomState(); } break; case 'custom_state': $addressFieldValue = $hasStates ? null : $address->getCustomState(); $cssFieldName = $hasStates ? $cssFieldName : 'address-state'; break; default: } if ($addressFieldValue || $showEmpty) { $result[$field->getServiceName()] = array('css_class' => $cssFieldName, 'title' => $field->getName(), 'value' => $addressFieldValue); } } return $result; }
/** * Add code to replace * * @param \Doctrine\ORM\Mapping\ClassMetadata $data Class metadata * @param string $template Template to use * @param array $substitutes List of entries to substitude * * @return void */ protected function addReplacement(\Doctrine\ORM\Mapping\ClassMetadata $data, $template, array $substitutes) { if (!empty($substitutes)) { $file = \Includes\Utils\Converter::getClassFile($data->reflClass->getName()); if (!isset($this->replacements[$file])) { $this->replacements[$file] = ''; } $this->replacements[$file] .= $this->substituteTemplate($template, $substitutes) . PHP_EOL . PHP_EOL; } }
/** * Constructor * * @param string $majorVersion Core major version * @param string $minorVersion Core minor version * @param integer $revisionDate Core revison date * @param integer $size Pack size * * @return void */ public function __construct($majorVersion, $minorVersion, $revisionDate, $size) { if (!$this->checkMajorVersion($majorVersion) || !$this->checkMinorVersion($majorVersion, $minorVersion)) { $version = \Includes\Utils\Converter::composeVersion($majorVersion, $minorVersion); \Includes\ErrorHandler::fireError('Unallowed core version for upgrade: ' . $version); } if ($revisionDate >= \XLite\Core\Converter::time()) { \Includes\ErrorHandler::fireError('Invalid core revision date: "' . date(DATE_RFC822, $revisionDate) . '"'); } $this->majorVersion = $majorVersion; $this->minorVersion = $minorVersion; $this->revisionDate = $revisionDate; $this->size = $size; parent::__construct(); }
/** * Get application version * * @return string */ public final function getVersion() { return \Includes\Utils\Converter::composeVersion($this->getMajorVersion(), $this->getMinorVersion()); }
/** * Call controller action * * @return void */ protected function callAction() { $action = $this->getAction(); $method = 'doAction' . \Includes\Utils\Converter::convertToPascalCase($action); if (method_exists($this, $method)) { // Call method doAction<action-name-in-camel-case> $this->{$method}(); } else { \XLite\Logger::getInstance()->log('Handler for the action "' . $action . '" is not defined for the "' . get_class($this) . '" class'); } $this->actionPostprocess($action); }
/** * Check if we need to forbid current action * * @return boolean */ protected function checkForDemoController() { $class = \Includes\Utils\Converter::trimLeadingChars(get_class($this), '\\'); return in_array($class, $this->demoControllers) ? !$this->isDemoActionPermitted($class) : $this->isDemoActionForbidden($class); }
/** * Get enpoint URL for certain action * * @param string $action Action name * * @return string */ protected function getMarketplaceActionURL($action) { return \Includes\Utils\Converter::trimTrailingChars($this->getMarketplaceURL(), '/') . '/' . $action; }
} $targetSkinDir = $target = strtolower(preg_replace('/([a-z0-9])([A-Z])([a-z0-9])/Ss', '$1_$2$3', $target)); $targetShort = ucfirst(\Includes\Utils\Converter::convertToCamelCase($target)); $targetClass = macro_assemble_class_name('Controller\\Admin\\' . $targetShort, $moduleAuthor, $moduleName); $targetControllerPath = macro_convert_class_name_to_path($targetClass); $list = array_merge((array) @glob(LC_DIR_CLASSES . 'XLite/Controller/Admin/' . $targetShort . '.php'), (array) @glob(LC_DIR_CLASSES . 'XLite/Module/*/*/Controller/Admin/' . $targetShort . '.php')); $list = array_map('trim', $list); $list = array_filter($list, function ($var) use($targetControllerPath) { return !empty($var) && realpath($var) != $targetControllerPath; }); if ($list) { macro_error('Controller class \'' . $targetShort . '\' already exists (' . implode('; ', $list) . ')'); } $targetOne = isset($targetOne) ? $targetOne : substr($target, 0, -1); $targetOneSkinDir = $targetOne = strtolower(preg_replace('/([a-z0-9])([A-Z])([a-z0-9])/Ss', '$1_$2$3', $targetOne)); $targetOneShort = ucfirst(\Includes\Utils\Converter::convertToCamelCase($targetOne)); $targetOneClass = macro_assemble_class_name('Controller\\Admin\\' . $targetOneShort, $moduleAuthor, $moduleName); $targetOneControllerPath = macro_convert_class_name_to_path($targetOneClass); $list = array_merge((array) @glob(LC_DIR_CLASSES . 'XLite/Controller/Admin/' . $targetOneShort . '.php'), (array) @glob(LC_DIR_CLASSES . 'XLite/Module/*/*/Controller/Admin/' . $targetOneShort . '.php')); $list = array_map('trim', $list); $list = array_filter($list, function ($var) use($targetOneControllerPath) { return !empty($var) && realpath($var) != $targetOneControllerPath; }); if ($list) { macro_error('Controller class \'' . $targetOneShort . '\' already exists (' . implode('; ', $list) . ')'); } // --search $searchFields = $searchFields ? array_map('trim', explode(',', $searchFields)) : array(); foreach ($searchFields as $field) { if (!in_array($field, $keys)) { macro_error('Field \'' . $field . '\' marked as searchable and it not ofund');
/** * Update quick flags for a category * * @param \XLite\Model\Category $entity Category * @param array $flags Flags to set * * @return void */ protected function updateQuickFlags(\XLite\Model\Category $entity, array $flags) { $quickFlags = $entity->getQuickFlags(); if (!isset($quickFlags)) { $quickFlags = new \XLite\Model\Category\QuickFlags(); $quickFlags->setCategory($entity); $entity->setQuickFlags($quickFlags); } foreach ($flags as $name => $delta) { $name = \Includes\Utils\Converter::convertToPascalCase($name); $quickFlags->{'set' . $name}($quickFlags->{'get' . $name}() + $delta); } }
/** * Get entity unique identifier value * * @return integer */ public function getUniqueIdentifier() { return $this->{'get' . \Includes\Utils\Converter::convertToPascalCase($this->getUniqueIdentifierName())}(); }
/** * Build Drupal path string * * @param string $target Target OPTIONAL * @param string $action Action OPTIONAL * @param array $params Parameters list OPTIONAL * @param string $node Node OPTIONAL * * @return string */ public static function buildDrupalPath($target = '', $action = '', array $params = array(), $node = self::DRUPAL_ROOT_NODE) { if (empty($action) && $params) { $action = static::EMPTY_ACTION; } $url = implode('/', array($node, $target, $action)); if ($params) { $url .= '/' . \Includes\Utils\Converter::buildQuery($params, '-', '/'); } return $url; }
/** * setHeaderLocation * * @param string $location URL * @param integer $code Operation code OPTIONAL * * @return void */ protected static function setHeaderLocation($location, $code = 302) { $location = \Includes\Utils\Converter::removeCRLF($location); if (headers_sent()) { // HTML meta tags-based redirect echo '<script type="text/javascript">' . "\n" . '<!--' . "\n" . 'self.location=\'' . $location . '\';' . "\n" . '-->' . "\n" . '</script>' . "\n" . '<noscript><a href="' . $location . '">Click here to redirect</a></noscript><br /><br />'; } elseif (\XLite\Core\Request::getInstance()->isAJAX() && 200 == $code) { // AJAX-based redirct header('AJAX-Location: ' . $location, true, $code); } else { // HTTP-based redirect header('Location: ' . $location, true, $code); } }
/** * Compares two values * * @param mixed $val1 Value 1 * @param mixed $val2 Value 2 * @param mixed $val3 Value 3 OPTIONAL * * @return boolean */ protected function isSelected($val1, $val2, $val3 = null) { if (isset($val1) && isset($val3)) { $method = 'get'; if ($val1 instanceof \XLite\Model\AEntity) { $method .= \Includes\Utils\Converter::convertToPascalCase($val2); } // Get value with get() method and compare it with third value $result = $val1->{$method}() == $val3; } else { $result = $val1 == $val2; } return $result; }
/** * Prepare class name * * @param string $class Class name to prepare * * @return string */ protected function prepareClassName($class) { return \Includes\Utils\Converter::trimLeadingChars($class, '\\'); }
/** * Uninstall module * * @param \XLite\Model\Module $module Module object * @param array &$messages Messages list * * @return boolean */ public function uninstallModule(\XLite\Model\Module $module, &$messages) { $result = false; // Get module pack $pack = new \XLite\Core\Pack\Module($module); $dirs = $pack->getDirs(); $nonWritableDirs = array(); // Check module directories permissions foreach ($dirs as $dir) { if (\Includes\Utils\FileManager::isExists($dir) && !\Includes\Utils\FileManager::isDirWriteable($dir)) { $nonWritableDirs[] = \Includes\Utils\FileManager::getRelativePath($dir, LC_DIR_ROOT); } } $params = array('name' => sprintf('%s v%s (%s)', $module->getModuleName(), $module->getVersion(), $module->getAuthorName())); if (empty($nonWritableDirs)) { $yamlData = array(); $yamlFiles = \Includes\Utils\ModulesManager::getModuleYAMLFiles($module->getAuthor(), $module->getName()); foreach ($yamlFiles as $yamlFile) { $yamlData[] = \Includes\Utils\FileManager::read($yamlFile); } if (!$module->checkModuleMainClass()) { $classFile = LC_DIR_CLASSES . \Includes\Utils\Converter::getClassFile($module->getMainClass()); if (\Includes\Utils\FileManager::isFileReadable($classFile)) { require_once $classFile; } } // Call uninstall event method $r = $module->callModuleMethod('callUninstallEvent', 111); if (111 == $r) { \XLite\Logger::getInstance()->log($module->getActualName() . ': Method callUninstallEvent() was not called'); } // Remove from FS foreach ($dirs as $dir) { \Includes\Utils\FileManager::unlinkRecursive($dir); } \Includes\Utils\ModulesManager::disableModule($module->getActualName()); \Includes\Utils\ModulesManager::removeModuleFromDisabledStructure($module->getActualName()); // Remove module from DB try { // Refresh module entity as it was changed by disableModule() method above $module = $this->find($module->getModuleID()); $this->delete($module); } catch (\Exception $e) { $messages[] = $e->getMessage(); } if ($module->getModuleID()) { $messages[] = \XLite\Core\Translation::getInstance()->translate('A DB error occured while uninstalling the module X', $params); } else { if (!empty($yamlData)) { foreach ($yamlData as $yaml) { \XLite\Core\Database::getInstance()->unloadFixturesFromYaml($yaml); } } $messages[] = \XLite\Core\Translation::getInstance()->translate('The module X has been uninstalled successfully', $params); $result = true; } } else { $messages[] = \XLite\Core\Translation::getInstance()->translate('Unable to delete module X files: some dirs have no writable permissions: Y', $params + array('dirs' => implode(', ', $nonWritableDirs))); } return $result; }
/** * showStepInfo * * @return void */ public static function showStepInfo() { $text = number_format(microtime(true) - static::$stepStart, 2) . 'sec, '; $memory = memory_get_usage(); $text .= \Includes\Utils\Converter::formatFileSize($memory, ''); $text .= ' (' . \Includes\Utils\Converter::formatFileSize(memory_get_usage() - static::$stepMemory, '') . ')'; \Includes\Utils\Operator::showMessage(' [' . $text . ']'); }
/** * Sanitize option new value * * @param \XLite\Model\Config $option Config option * @param string $value New value * * @return string */ protected function sanitizeOptionValue($option, $value) { $category = $option->getCategory(); $name = $option->getName(); $validationMethod = 'sanitize' . \Includes\Utils\Converter::convertToCamelCase($category) . \Includes\Utils\Converter::convertToCamelCase($name); if (method_exists($this, $validationMethod)) { $value = $this->{$validationMethod}($value); } $type = $option->getType(); if ('checkbox' === $type) { $result = empty($value) ? 'N' : 'Y'; } elseif ('serialized' === $type && null !== $value && is_array($value)) { $result = serialize($value); } elseif ('text' === $type) { $result = null !== $value ? trim($value) : ''; } elseif ('XLite\\View\\FormField\\Input\\PasswordWithValue' === $type) { $result = null !== $value ? $value : null; } else { $result = null !== $value ? $value : ''; } return $result; }
/** * Populate model object properties by the passed data * * @param array $data Data to set * * @return void */ protected function setModelProperties(array $data) { $optionsToUpdate = array(); // Find changed options and store them in $optionsToUpdate foreach ($this->getEditableOptions() as $key => $option) { $name = $option->name; $type = $option->type; $value = $option->value; $category = $option->category; $validationMethod = 'sanitize' . \Includes\Utils\Converter::convertToCamelCase($category) . \Includes\Utils\Converter::convertToCamelCase($name); if (method_exists($this, $validationMethod)) { $data[$name] = $this->{$validationMethod}($data[$name]); } if ('checkbox' === $type) { $newValue = empty($data[$name]) ? 'N' : 'Y'; } elseif ('serialized' === $type && isset($data[$name]) && is_array($data[$name])) { $newValue = serialize($data[$name]); } elseif ('text' === $type) { $newValue = array_key_exists($name, $data) ? null !== $data[$name] ? trim($data[$name]) : null : ''; } else { $newValue = array_key_exists($name, $data) ? $data[$name] : ''; } if (null !== $newValue && $value != $newValue) { $option->value = $newValue; $optionsToUpdate[] = $option; } } // Save changed options to the database if (!empty($optionsToUpdate)) { foreach ($optionsToUpdate as $option) { if ($this->preprocessOption($option)) { \XLite\Core\Database::getRepo('\\XLite\\Model\\Config')->createOption(array('category' => $option->category, 'name' => $option->name, 'value' => $option->value)); } } } }
/** * Prepare MySQL connection string * * @return string */ public static function getConnectionString() { return 'mysql:' . \Includes\Utils\Converter::buildQuery(static::getConnectionParams(), '=', ';'); }
/** * Get list of available minor versions for the helpers * * @param string $majorVersion Current major version * * @return array */ protected function getUpgradeHelperMinorVersions($majorVersion) { $new = \Includes\Utils\Converter::composeVersion($this->getMajorVersionNew(), $this->getMinorVersionNew()); $oldMajorVersion = $this->getMajorVersionOld(); $oldMinorVersion = $this->getMinorVersionOld(); $old = strlen($oldMajorVersion) && strlen($oldMinorVersion) ? \Includes\Utils\Converter::composeVersion($oldMajorVersion, $oldMinorVersion) : $new; return array_filter($this->getUpgradeHelperVersions($majorVersion . LC_DS), function ($var) use($majorVersion, $old, $new) { $version = \Includes\Utils\Converter::composeVersion($majorVersion, $var); return version_compare($old, $version, '<') && version_compare($new, $version, '>='); }); }
/** * Remove trailing slashes from URL * * @param string $url URL to prepare * * @return string */ public static function trimTrailingSlashes($url) { return \Includes\Utils\Converter::trimTrailingChars($url, '/'); }
/** * Get module version * * @return string */ public static function getVersion() { return \Includes\Utils\Converter::composeVersion(static::getMajorVersion(), static::getMinorVersion()); }
/** * Check if module will be disabled after upgrade * * :TRICKY: check if the "getMajorVersion" is not declared in the main module class * * @param \XLite\Model\Module $module Module to check * * @return boolean */ protected function isModuleToDisable(\XLite\Model\Module $module) { $versionCore = \XLite\Upgrade\Cell::getInstance()->getCoreMajorVersion(); $versionModule = $module->getMajorVersion(); $classModule = \Includes\Utils\ModulesManager::getClassNameByModuleName($module->getActualName()); $reflection = new \ReflectionMethod($classModule, 'getMajorVersion'); $classModule = \Includes\Utils\Converter::prepareClassName($classModule); $classActual = \Includes\Utils\Converter::prepareClassName($reflection->getDeclaringClass()->getName()); return version_compare($versionModule, $versionCore, '<') || $classModule !== $classActual; }
/** * Get category clean URL by category id * * @param integer $id Category ID * @param array $params URL params OPTIONAL * * @return string|void */ protected function getCategoryCleanURL($id, array $params = array()) { $category = \XLite\Core\Database::getRepo('\\XLite\\Model\\Category')->find($id); return isset($category) && $category->getCleanURL() ? \Includes\Utils\URLManager::trimTrailingSlashes($category->getCleanURL()) . '/' . \Includes\Utils\Converter::buildQuery($params, '-', '/') : null; }
/** * Prepare human-readable output for file size * * @param integer $size Size in bytes * * @return string */ public static function formatFileSize($size) { list($size, $suffix) = \Includes\Utils\Converter::formatFileSize($size); return $size . ' ' . ($suffix ? static::t($suffix) : ''); }
/** * Prepare file path * * @param string $dir Dir to prepare * @param boolean $check Flag OPTIONAL * * @return string */ public static function getCanonicalDir($dir, $check = true) { if ($check) { $dir = static::getRealPath($dir); } if (!$check || !empty($dir) && static::isDir($dir)) { $dir = \Includes\Utils\Converter::trimTrailingChars($dir, LC_DS) . LC_DS; } return $dir ?: null; }
/** * Parse value of a phpDocumenter tag * * @param string $value Value to parse * * @return array */ protected static function parseTagValue($value) { return \Includes\Utils\Converter::parseQuery($value, '=', ',', '"\''); }
/** * Save item state * * @param \XLite\Model\Base\IOrderItem $item Item object * * @return void */ protected function saveItemState(\XLite\Model\Base\IOrderItem $item) { $price = $item->getPrice(); $this->setPrice(\Includes\Utils\Converter::formatPrice($price)); $this->setName($item->getName()); $this->setSku($item->getSku()); }