prepareContext() public static method

Prepares the context by given event or without event for other usage
public static prepareContext ( null | string $installDir = null, null | Composer\Script\Event $event = null ) : void
$installDir null | string The install dir to check for info files
$event null | Composer\Script\Event The event instance or null
return void
Esempio n. 1
0
 /**
  * This method will be invoked by composer after a successful installation and creates
  * the application server configuration file under etc/appserver/appserver.xml.
  *
  * @param \Composer\Script\Event $event The event that invokes this method
  *
  * @return void
  */
 public static function postInstall(Event $event)
 {
     // initialize the installation directory
     $override = false;
     $installDir = getcwd();
     // check the arguments for an installation directory
     foreach ($event->getArguments() as $arg) {
         // extract arguments
         list($key, ) = explode('=', $arg);
         // query we want to override files
         if ($key === SetupKeys::ARG_OVERRIDE) {
             $override = true;
         }
         // query for a custom installation directory
         if ($key === SetupKeys::ARG_INSTALL_DIR) {
             $installDir = str_replace("{$key}=", '', $arg);
         }
     }
     Setup::prepareContext($installDir, $event);
     // process and move the configuration files their target directory
     Setup::processTemplate('etc/appserver/appserver.xml', $override);
     // write a message to the console
     $event->getIo()->write(sprintf('%s<info>Successfully invoked appserver.io Composer post-install-cmd script ...</info>', Setup::$logo));
 }
 /**
  * Switches the setup mode to the passed value.
  *
  * @param string $newMode               The mode to switch to
  * @param string $configurationFilename The path of the configuration filename
  * @param string $user                  The name of the user who started the application server
  *
  * @return void
  * @throws \Exception Is thrown for an invalid setup mode passed
  */
 public function switchSetupMode($newMode, $configurationFilename, $user)
 {
     // log a message that we switch setup mode now
     $this->getInitialContext()->getSystemLogger()->info(sprintf('Now switch mode to %s!!!', $newMode));
     // init setup context
     Setup::prepareContext($this->getBaseDirectory());
     // init variable for the group
     $group = null;
     // pattern to replace the user in the etc/appserver/appserver.xml file
     $configurationUserReplacePattern = '/(<appserver[^>]+>[^<]+<params>.*<param name="user[^>]+>)([^<]+)/s';
     // check setup modes
     switch ($newMode) {
         // prepares everything for developer mode
         case ContainerService::SETUP_MODE_DEV:
             // get defined group from configuration
             $group = Setup::getValue(SetupKeys::GROUP);
             // replace user in configuration file
             file_put_contents($configurationFilename, preg_replace($configurationUserReplacePattern, '${1}' . $user, file_get_contents($configurationFilename)));
             // add everyone write access to configuration files for dev mode
             FileSystem::recursiveChmod($this->getEtcDir(), 0777, 0777);
             break;
             // prepares everything for production mode
         // prepares everything for production mode
         case ContainerService::SETUP_MODE_PROD:
             // get defined user and group from configuration
             $user = Setup::getValue(SetupKeys::USER);
             $group = Setup::getValue(SetupKeys::GROUP);
             // replace user to be same as user in configuration file
             file_put_contents($configurationFilename, preg_replace($configurationUserReplacePattern, '${1}' . $user, file_get_contents($configurationFilename)));
             // set correct file permissions for configurations
             FileSystem::recursiveChmod($this->getEtcDir());
             break;
             // prepares everything for first installation which is default mode
         // prepares everything for first installation which is default mode
         case ContainerService::SETUP_MODE_INSTALL:
             // load the flag marked the server as installed
             $isInstalledFlag = $this->getIsInstalledFlag();
             // first check if it is a fresh installation
             if ($isInstalledFlag->isReadable() === false) {
                 // set example app dodeploy flag to be deployed for a fresh installation
                 touch($this->getDeployDir('example.phar.dodeploy'));
             }
             // create is installed flag for prevent further setup install mode calls
             touch($isInstalledFlag);
             // get defined user and group from configuration
             $user = Setup::getValue(SetupKeys::USER);
             $group = Setup::getValue(SetupKeys::GROUP);
             // set correct file permissions for configurations
             FileSystem::recursiveChmod($this->getEtcDir());
             break;
         default:
             throw new \Exception(sprintf('Invalid setup mode %s given', $newMode));
     }
     // check if user and group is set
     if (!is_null($user) && !is_null($group)) {
         // get needed files as accessable for all root files remove "." and ".." from the list
         $rootFiles = scandir($this->getBaseDirectory());
         // iterate all files
         foreach ($rootFiles as $rootFile) {
             // we want just files on root dir
             if (is_file($rootFile) && !in_array($rootFile, array('.', '..'))) {
                 FileSystem::chmod($rootFile, 0644);
                 FileSystem::chown($rootFile, $user, $group);
             }
         }
         // ... and change own and mod of following directories
         FileSystem::chown($this->getBaseDirectory(), $user, $group);
         FileSystem::chown($this->getWebappsDir(), $user, $group);
         FileSystem::recursiveChown($this->getTmpDir(), $user, $group);
         FileSystem::recursiveChmod($this->getTmpDir());
         FileSystem::recursiveChown($this->getDeployDir(), $user, $group);
         FileSystem::recursiveChmod($this->getDeployDir());
         FileSystem::recursiveChown($this->getBaseDirectory('resources'), $user, $group);
         FileSystem::recursiveChmod($this->getBaseDirectory('resources'));
         FileSystem::recursiveChown($this->getBaseDirectory('src'), $user, $group);
         FileSystem::recursiveChmod($this->getBaseDirectory('src'));
         FileSystem::recursiveChown($this->getBaseDirectory('var'), $user, $group);
         FileSystem::recursiveChmod($this->getBaseDirectory('var'));
         FileSystem::recursiveChown($this->getBaseDirectory('tests'), $user, $group);
         FileSystem::recursiveChmod($this->getBaseDirectory('tests'));
         FileSystem::recursiveChown($this->getBaseDirectory('vendor'), $user, $group);
         FileSystem::recursiveChmod($this->getBaseDirectory('vendor'));
         // make server.php executable
         FileSystem::chmod($this->getBaseDirectory('server.php'), 0755);
         // log a message that we successfully switched to the new setup mode
         $this->getInitialContext()->getSystemLogger()->info(sprintf("Setup for mode '%s' done successfully!", $newMode));
     } else {
         throw new \Exception('No user or group given');
     }
 }