/** * @param string $name command name (case-insensitive) * * @return \CConsoleCommand The command object. Null if the name is invalid. */ public function createCommand($name) { $name = StringHelper::toLowerCase($name); $command = null; if (isset($this->commands[$name])) { $command = $this->commands[$name]; } else { $commands = array_change_key_case($this->commands); if (isset($commands[$name])) { $command = $commands[$name]; } } if ($command !== null) { if (is_string($command)) { $className = 'NerdsAndCompany\\Schematic\\ConsoleCommands\\' . IOHelper::getFileName($command, false); return new $className($name, $this); } else { // an array configuration return Craft::createComponent($command, $name, $this); } } elseif ($name === 'help') { return new \CHelpCommand('help', $this); } else { return; } }
/** * Import the database dump by running mysql from the command line (assumes Bash?) * * @param BaseModel $settings The task's settings * @param mixed $arg An optional second argument * * @return bool */ public function importBackup($settings, $arg = null) { $keepAfterCopy = $this->getSettings()['importBackup']['keepBackup'] === '1' ? true : false; $pathToBackup = craft()->path->getDbBackupPath() . StringHelper::toLowerCase(IOHelper::cleanFilename($settings->backupFileName)); $destination = $this->getSettings()['copyBackup']['destination']; $destination = rtrim($destination, '/') . '/'; $destination = $destination . basename($pathToBackup); $sshHostname = $this->getSettings()['ssh']['remote']['hostname']; $sshUsername = $this->getSettings()['ssh']['remote']['username']; $sshPassword = craft()->goLive_security->decrypt($this->getSettings()['ssh']['remote']['password']); $mysqlHostname = $this->getSettings()['mysql']['hostname']; $mysqlUsername = $this->getSettings()['mysql']['username']; $mysqlPassword = craft()->goLive_security->decrypt($this->getSettings()['mysql']['password']); $mysqlDb = $this->getSettings()['mysql']['dbname']; $ssh = new Net_SSH2($sshHostname); $ssh->login($sshUsername, $sshPassword); $commands = array(array('command' => sprintf('mysql -h %s -u %s -p%s %s < %s', $mysqlHostname, $mysqlUsername, $mysqlPassword, $mysqlDb, $destination), 'output' => '')); if (!$keepAfterCopy) { array_push($commands, array('command' => sprintf('rm %s', $destination), 'output' => '')); } for ($i = 0; $i < count($commands); $i++) { $commands[$i]['output'] = $ssh->exec($commands[$i]['command']); } return true; }
/** * Determines if there is enough memory to process this image. * * The code was adapted from http://www.php.net/manual/en/function.imagecreatefromjpeg.php#64155. It will first * attempt to do it with available memory. If that fails, Craft will bump the memory to amount defined by the * [phpMaxMemoryLimit](http://buildwithcraft.com/docs/config-settings#phpMaxMemoryLimit) config setting, then try again. * * @param string $filePath The path to the image file. * @param bool $toTheMax If set to true, will set the PHP memory to the config setting phpMaxMemoryLimit. * * @return bool */ public function checkMemoryForImage($filePath, $toTheMax = false) { if (StringHelper::toLowerCase(IOHelper::getExtension($filePath)) == 'svg') { return true; } if (!function_exists('memory_get_usage')) { return false; } if ($toTheMax) { // Turn it up to 11. craft()->config->maxPowerCaptain(); } // Find out how much memory this image is going to need. $imageInfo = getimagesize($filePath); $K64 = 65536; $tweakFactor = 1.7; $bits = isset($imageInfo['bits']) ? $imageInfo['bits'] : 8; $channels = isset($imageInfo['channels']) ? $imageInfo['channels'] : 4; $memoryNeeded = round(($imageInfo[0] * $imageInfo[1] * $bits * $channels / 8 + $K64) * $tweakFactor); $memoryLimit = AppHelper::getPhpConfigValueInBytes('memory_limit'); if (memory_get_usage() + $memoryNeeded < $memoryLimit) { return true; } if (!$toTheMax) { return $this->checkMemoryForImage($filePath, true); } // Oh well, we tried. return false; }