openrat-cms

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README

Publisher.class.php (4466B)


      1 <?php
      2 
      3 
      4 namespace cms\generator;
      5 
      6 
      7 use cms\base\Configuration as C;
      8 use cms\generator\target\Local;
      9 use cms\generator\target\NoTarget;
     10 use cms\generator\target\BaseTarget;
     11 use cms\model\ModelBase;
     12 use cms\model\Project;
     13 use logger\Logger;
     14 use cms\generator\target\TargetFactory;
     15 use util\exception\PublisherException;
     16 use util\Request;
     17 use util\Session;
     18 use util\text\TextMessage;
     19 use util\text\variables\VariableResolver;
     20 
     21 /**
     22  * Publisher for publishing files.
     23  *
     24  * This publisher is publishing files to the live server.
     25  *
     26  * It doesn't care about the node type, it just publishes files, which means, all node types
     27  * are recognized as bare files.
     28  *
     29  * @package cms\generator
     30  */
     31 class Publisher
     32 {
     33 	/**
     34 	 * The target to which the file will be copied to.
     35 	 *
     36 	 * @var BaseTarget
     37 	 */
     38 	private $target;
     39 
     40 	/**
     41 	 * @var PublishOrder[]
     42 	 */
     43 	private $publishingOrders = [];
     44 
     45 	/**
     46 	 * @var Project
     47 	 */
     48 	private $project;
     49 
     50 
     51 	public function __construct( $projectid )
     52 	{
     53 		$this->project = Project::create( $projectid );
     54 		$this->project->load();
     55 
     56 		$this->init();
     57 	}
     58 
     59 	/**
     60 	 * Adds a file to the publishing process.
     61 	 *
     62 	 * @param PublishOrder order
     63 	 */
     64 	public function addOrderForPublishing($publishOrder ) {
     65 
     66 		$this->publishingOrders[] = $publishOrder;
     67 	}
     68 
     69 
     70 	/**
     71 	 * The target is cleaned up, which means, older files (older than the start time of the request) are removed.
     72 	 *
     73 	 * @param int $time unused (always using the start time)
     74 	 */
     75 	public function cleanOlderThan( $time ) {
     76 
     77 		if   ( $this->target instanceof Local )
     78 			$this->target->clean();
     79 	}
     80 
     81 
     82 
     83 	/**
     84 	 * Initializing the Publisher.
     85 	 */
     86 	public function init()
     87 	{
     88 		if	( C::subset('security')->is('nopublish') )
     89 		{
     90 			$this->target = new NoTarget();
     91 			Logger::warn('publishing is disabled.');
     92 		} else {
     93 			$this->target = TargetFactory::getTargetForUrl( $this->project->target_dir );
     94 		}
     95 	}
     96 
     97 
     98 	/**
     99 	 * All files are being processed and published.
    100 	 *
    101 	 * @throws PublisherException
    102 	 */
    103 	public function publish()
    104 	{
    105 		$this->target->open(); // Open the connection to the target.
    106 
    107 		foreach($this->publishingOrders as $publishOrder )
    108 
    109 			$this->target->put( $publishOrder->localFilename,$publishOrder->destinationFilename,$publishOrder->fileTime );
    110 
    111 		$this->target->close();
    112 
    113 		$this->executeSystemCommand();
    114 	}
    115 
    116 
    117 	/**
    118 	 * Gets an array with all destination filenames.
    119 	 *
    120 	 * @return array
    121 	 */
    122 	public function getDestinationFilenames() {
    123 
    124 		return array_map( function($order) {
    125 			return $order->destinationFilename;
    126 		}, $this->publishingOrders );
    127 	}
    128 
    129 
    130 	/**
    131 	 * @return array
    132 	 * @throws PublisherException
    133 	 */
    134 	protected function executeSystemCommand()
    135 	{
    136 		$systemCommand = $this->getSystemCommand();
    137 
    138 		if ( $systemCommand ) {
    139 			$ausgabe = array();
    140 			$rc = false;
    141 			Logger::debug( TextMessage::create('Executing system command: ${0}',[$systemCommand]) );
    142 
    143 			/** @var ModelBase $baseObjectToEnv */
    144 			foreach (['user' => Request::getUser(),
    145 						 'project' => $this->project]
    146 					 as $key => $baseObjectToEnv) {
    147 
    148 				foreach ($baseObjectToEnv->getProperties() as $name => $property)
    149 					putenv('CMS_' . strtoupper($key) . '_' . strtoupper($name) . '=' . $property);
    150 
    151 			}
    152 
    153 			exec($systemCommand, $ausgabe, $rc);
    154 
    155 			if ($rc != 0) // Wenn Returncode ungleich 0, dann Fehler melden.
    156 				throw new PublisherException('System command failed - returncode is ' . $rc . "\n" .
    157 					implode("\n",$ausgabe) );
    158 			else
    159 				Logger::debug('System command successful');
    160 
    161 		}
    162 	}
    163 
    164 
    165 	/**
    166 	 * Calculates the system command to execute after publishing.
    167 	 *
    168 	 * @return string|null The system command (or null, if there is no command)
    169 	 */
    170 	private function getSystemCommand()
    171 	{
    172 		$confPublish   = C::subset('publish');
    173 		$commandConfig = $confPublish->subset('command');
    174 
    175 		if	( ! $commandConfig->is('enable') )
    176 			return null;
    177 
    178 		if	( $commandConfig->is('per_project') && !empty($this->project->cmd_after_publish) )
    179 			$commandAfterPublish   = $this->project->cmd_after_publish;
    180 		else
    181 			$commandAfterPublish   = @$commandConfig->get('command');
    182 
    183 		// Im Systemkommando Variablen ersetzen
    184 		$resolver = new VariableResolver();
    185 		$resolver->addResolver('project', function( $property) {
    186 			return @$this->project->getProperties()[$property];
    187 		});
    188 		$resolver->addResolver('target', function( $property) {
    189 			return @parse_url( $this->project->target_dir )[$property];
    190 		});
    191 
    192 		$commandAfterPublish = $resolver->resolveVariables( $commandAfterPublish );
    193 
    194 		return $commandAfterPublish;
    195 	}
    196 
    197 
    198 }