AppserverIo\Appserver\Meta\Composer\Script\Setup::processTemplate PHP Method

processTemplate() public static method

Processes the template and replace the properties with the OS specific values.
public static processTemplate ( string $template, boolean $override = false, integer $mode = 420 ) : void
$template string The path to the template
$override boolean TRUE if the file should be overwritten if exists, else FALSE
$mode integer The mode of the target file
return void
    public static function processTemplate($template, $override = false, $mode = 0644)
    {
        // prepare the target filename
        $targetFile = Setup::prepareOsSpecificPath($template);
        // query whether we've to override the file
        if ($override === false && is_file($targetFile) === true) {
            return;
        }
        // prepare the target directory
        Setup::prepareDirectory($template);
        // process the template and store the result in the passed file
        ob_start();
        include Setup::prepareOsSpecificPath(sprintf('resources/templates/%s.phtml', $template));
        file_put_contents($targetFile, ob_get_clean());
        // set the correct mode for the file
        Setup::changeFilePermissions($template, $mode);
    }

Usage Example

Example #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));
 }