openrat-cms

OpenRat Content Management System
git clone http://git.code.weiherhei.de/openrat-cms.git
Log | Files | Refs | README

Publisher.class.php (4448B)


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