public function testEscape() { // Initialize $sInput = '"key":"value"'; // Assert $this->assertEquals('\\"key\\":\\"value\\"', ExtendedString::escape($sInput, '"')); $this->assertEquals('b"keyb":"valueb"', ExtendedString::escape($sInput, '"', 'b%s', ':')); }
public function getRepository($sRepositoryName, $sNamespace) { if (empty($this->aRepositories[$sRepositoryName])) { // Get class name $sClassName = sprintf('\\%1$s\\Repository\\%2$s', $sNamespace === '' ? $this->sNamespace : $sNamespace, ExtendedString::toCamelCase($sRepositoryName, '_', true)); // Check class exists if (!class_exists($sClassName)) { throw new RuntimeException(sprintf('Invalid class name %s', $sClassName)); } // Create repository $this->aRepositories[$sRepositoryName] = new $sClassName($this->oMapperFactory->getMapper($sRepositoryName)); } return $this->aRepositories[$sRepositoryName]; }
public function getMapper($sMapperName, $sNamespace = '') { if (empty($this->aMappers[$sMapperName])) { // Get class name $sClassName = sprintf('\\%1$s\\Mapper\\%2$s', $sNamespace === '' ? $this->sNamespace : $sNamespace, ExtendedString::toCamelCase($sMapperName, '_', true)); // Check class exists if (!class_exists($sClassName)) { throw new RuntimeException(sprintf('Invalid class name %s', $sClassName)); } // Create mapper $this->aMappers[$sMapperName] = new $sClassName($this->oDbConnectionLocator->getWrite(ExtendedString::toSnakeCase(explode('\\', $sMapperName)[0], '_', true))); } return $this->aMappers[$sMapperName]; }
public static function encode(array $aInput, $sKey, DateTime $oTimestamp = null, $sNonce = '') { // Add mandatory information if (is_null($oTimestamp)) { $oTimestamp = new DateTime(); } $aInput['timestamp'] = $oTimestamp->getTimestamp(); $aInput['nonce'] = empty($sNonce) ? ExtendedString::random(24) : $sNonce; // Build payload $sPayload = static::buildPayload($aInput); // Sign payload $sSignature = static::signPayload($sPayload, $sKey); // Return return sprintf('%1$s.%2$s', base64_encode($sPayload), base64_encode($sSignature)); }
public function addHandler($sHandlerName, $sClassName, array $aConfig, $bDefaultHandler = false, $sNamespace = '') { // Get class name $sClassName = ExtendedString::toCamelCase(sprintf('%s\\%sHandler', $sNamespace === '' ? $this->sNamespace : $sNamespace, $sClassName), '_', true); // Class name is valid if (!class_exists($sClassName)) { throw new RuntimeException(sprintf('Invalid class name %s', $sClassName)); } // Add handler /** @var $oHandler \Asticode\FileManager\Handler\HandlerInterface */ $oHandler = new $sClassName($aConfig); $this->aHandlers[$sHandlerName] = $oHandler; // Default handler if ($bDefaultHandler) { $this->sDefaultHandlerName = $sHandlerName; } // Add file methods $this->aCopyMethods = array_merge($this->aCopyMethods, $oHandler->getCopyMethods()); $this->aMoveMethods = array_merge($this->aMoveMethods, $oHandler->getMoveMethods()); }
/** * @param $sHandlerName * @return \Asticode\DeploymentManager\Service\Build\Handler\HandlerInterface */ private function getHandler($sHandlerName) { if (empty($this->aHandlers[$sHandlerName])) { // Get class name $sClassName = sprintf('\\Asticode\\DeploymentManager\\Service\\Build\\Handler\\%1$sHandler', ExtendedString::toCamelCase($sHandlerName, '_', true)); // Check class exists if (!class_exists($sClassName)) { throw new RuntimeException(sprintf('Invalid class name %s', $sClassName)); } // Create handler $oHandler = new $sClassName($this->oFileManager, $this->aConfig); // Handler implements the interface if (!$oHandler instanceof HandlerInterface) { throw new RuntimeException(sprintf('Class name %s doesn\'t implement the HandlerInterface', $sClassName)); } // Add handler $this->aHandlers[$sHandlerName] = $oHandler; } return $this->aHandlers[$sHandlerName]; }
/** * @param $sHandlerName * @param $sClassName * @param $iPriority * @param array $aConfig * @param string $sNamespace * @return CacheManager */ public function addHandler($sHandlerName, $sClassName, $iPriority, array $aConfig, $sNamespace = '') { // Get class name $sClassName = ExtendedString::toCamelCase(sprintf('%s\\%sHandler', $sNamespace === '' ? $this->sNamespace : $sNamespace, $sClassName), '_', true); // Class name is valid if (!class_exists($sClassName)) { throw new RuntimeException(sprintf('Invalid class name %s', $sClassName)); } // Priority is valid $iPriority = intval($iPriority); if (array_key_exists($iPriority, $this->aPriorityToNameMapping)) { throw new RuntimeException(sprintf('Priority %s is already in use', $iPriority)); } // Add handler $oHandler = new $sClassName($aConfig); $this->aHandlers[$sHandlerName] = $oHandler; // Add priority $this->aPriorityToNameMapping[$iPriority] = $sHandlerName; ksort($this->aPriorityToNameMapping); // Return return $this; }
protected function stepReplace($sTempDirPath, $aReplacementConfig) { return new Step('Replace', StepDatasource::PHP, function () use($sTempDirPath, $aReplacementConfig) { // Initialize $aOutputs = []; // Loop through files to replace foreach ($aReplacementConfig as $sFilePath => $aStringsToReplace) { // Update filepath $sFilePath = sprintf('%s%s', $sTempDirPath, $sFilePath); // File exists if ($this->oFileManager->exists($sFilePath)) { // Get content $sContent = $this->oFileManager->read($sFilePath); // Loop through strings to replace foreach ($aStringsToReplace as $sStringToReplace => $sReplacement) { $sContent = preg_replace(sprintf('/%s/', ExtendedString::pregQuote($sStringToReplace)), $sReplacement, $sContent); } // Overwrite $this->oFileManager->write($sContent, $sFilePath, WriteMethod::OVERWRITE); // Add output $aOutputs[] = sprintf('Replaced values in path %s', $sFilePath); } else { // Add output $aOutputs[] = sprintf('Path %s is invalid', $sFilePath); } } // Return return $aOutputs; }); }
private function escapeSingleQuotes($sInput) { return ExtendedString::escape($sInput, "'", "'\\%s'"); }
public static function postInstall(Event $oEvent) { // Initialize $sAppDirPath = __DIR__ . '/../../../app'; $sLocalDistConfigPath = sprintf('%s/%s', $sAppDirPath, 'install/local.php.dist'); $sLocalConfigPath = sprintf('%s/%s', $sAppDirPath, 'config/local.php'); // Create file manager $oFileManager = new FileManager([]); $oFileManager->addHandler('local', 'PHP', []); // Make sure console is executable $sOutput = 'Make sure the deployment manager console is executable'; $sErrorMessage = ''; try { ExtendedShell::exec(sprintf('chmod +x %s', sprintf('%s/%s', $sAppDirPath, 'console'))); } catch (Exception $oException) { // Update error message $sErrorMessage = $oException->getMessage(); } // No error if ($sErrorMessage === '') { $sResult = 'OK'; } else { $sResult = 'KO'; } // Output $oEvent->getIO()->write(sprintf("\n%s: %s", $sOutput, $sResult)); // Copy dist config $sOutput = 'Copy the local dist config'; $sErrorMessage = ''; try { $oFileManager->copy($sLocalDistConfigPath, $sLocalConfigPath); } catch (Exception $oException) { // Update error message $sErrorMessage = $oException->getMessage(); } // Error if ($sErrorMessage === '') { // Output $oEvent->getIO()->write(sprintf("\n%s: OK", $sOutput)); // Explain $oEvent->getIO()->write("\nTo install the manager, you need a valid UTF-8 database as well as a user with " . "read/write privileges on it. Once you have it, please fill in the information below:\n"); // Set ouput $sOutput = 'Update local config parameters'; // Get local config content $sLocalConfigContent = $oFileManager->read($sLocalConfigPath); // Get values to ask $aValuesToAsk = ['%DATASOURCE_HOSTNAME%' => ['label' => 'database host', 'default' => 'localhost', 'mandatory' => true], '%DATASOURCE_DATABASE%' => ['label' => 'database name', 'default' => 'deployment', 'mandatory' => true], '%DATASOURCE_USERNAME%' => ['label' => 'database user name', 'mandatory' => true], '%DATASOURCE_PASSWORD%' => ['label' => 'database user password', 'mandatory' => false], '%BUILD_nb_backups_per_project%' => ['label' => 'number of backups kept per project', 'default' => '2', 'mandatory' => true], '%BUILD_BIN_COMPOSER%' => ['label' => 'full path to composer binary', 'default' => '/usr/bin/composer', 'mandatory' => true, 'binary' => ['check_command' => '%s -v', 'name' => 'composer']], '%BUILD_BIN_GIT%' => ['label' => 'full path to git binary', 'default' => '/usr/local/bin/git', 'mandatory' => true, 'binary' => ['check_command' => '%s --version', 'name' => 'git']], '%BUILD_BIN_PHP%' => ['label' => 'full path to php binary', 'default' => '/usr/bin/php', 'mandatory' => true, 'binary' => ['check_command' => '%s -v', 'name' => 'php']]]; // Loop through values to ask foreach ($aValuesToAsk as $sKeyToReplace => $aValueToAsk) { // Initialize $sDefault = isset($aValueToAsk['default']) ? $aValueToAsk['default'] : null; // Binary path if (isset($aValueToAsk['binary'])) { // Get value $sValue = ExtendedComposer::askBinaryPath($oEvent, $aValueToAsk['label'], $aValueToAsk['binary']['name'], $aValueToAsk['binary']['check_command'], $sDefault, true, $aValueToAsk['mandatory']); } else { // Get value $sValue = ExtendedComposer::askString($oEvent, $aValueToAsk['label'], $sDefault, $aValueToAsk['mandatory']); } // Replace config $sLocalConfigContent = preg_replace(sprintf('/%s/', ExtendedString::pregQuote($sKeyToReplace)), $sValue, $sLocalConfigContent); } // Put local config content $oFileManager->write($sLocalConfigContent, $sLocalConfigPath, WriteMethod::OVERWRITE); // Output $oEvent->getIO()->write(sprintf("\n%s: OK", $sOutput)); // Get config $aConfig = ExtendedArray::extendWithDefaultValues(require __DIR__ . '/../../../app/config/local.php', require __DIR__ . '/../../../app/config/global.php'); // Build extended PDO $aDatasourceConfig = $aConfig['datasources']['write']['deployment']; $oExtendedPDO = new ExtendedPdo("mysql:host={$aDatasourceConfig['hostname']};" . "dbname={$aDatasourceConfig['database']};", $aDatasourceConfig['username'], $aDatasourceConfig['password'], $aConfig['pdo_options']); // Execute SQL commands $sOutput = 'Execute SQL commands'; $sErrorMessage = ''; try { // Get SQL files $aSQLFiles = $oFileManager->explore(__DIR__ . '/../../../sql', OrderField::BASENAME); // Loop through SQL files /** @var $oSQLFile \Asticode\FileManager\Entity\File */ foreach ($aSQLFiles as $oSQLFile) { // Split statements $aStatements = explode(';', $oFileManager->read($oSQLFile->getPath())); // Loop through statements foreach ($aStatements as $sStatement) { if ($sStatement !== '') { $oExtendedPDO->exec($sStatement); } } } } catch (Exception $oException) { // Get error message $sErrorMessage = $oException->getMessage(); } // Error if ($sErrorMessage === '') { // Output $oEvent->getIO()->write(sprintf("\n%s: OK", $sOutput)); // Create dirs $sOutput = 'Create directories'; $aDirsToCreate = ['backups', 'gits', 'tmp']; // Check keys exist ExtendedArray::checkRequiredKeys($aConfig['build']['dirs'], $aDirsToCreate); // Loop through dirs to create foreach ($aDirsToCreate as $sDirToCreate) { // Create dir $oFileManager->createDir($aConfig['build']['dirs'][$sDirToCreate]); } // Output $oEvent->getIO()->write(sprintf("\n%s: OK", $sOutput)); // Conclude $oEvent->getIO()->write("\n\nInstallation successful!\n\n\nYou can now add a new project with\n\n" . " \$ <your path>/app/console project:add\n\nOr remove a project with\n\n" . " \$ <your path>/app/console project:remove\n"); } else { // Output $oEvent->getIO()->write(sprintf("\n%s: KO\n", $sOutput)); // Throw exception throw new RuntimeException($sErrorMessage); } } else { // Output $oEvent->getIO()->write(sprintf("\n%s: KO\n", $sOutput)); // Throw exception throw new RuntimeException($sErrorMessage); } }