File modules/cms/generator/Publisher.class.php
Last commit: Fri Apr 15 14:51:22 2022 +0200 dankert Refactoring: User,Config and Database info is now stored in the Request, because so there is no session required for clients which are using Basic Authorization.
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 }
Downloadmodules/cms/generator/Publisher.class.php
History Fri, 15 Apr 2022 14:51:22 +0200 dankert Refactoring: User,Config and Database info is now stored in the Request, because so there is no session required for clients which are using Basic Authorization. Sat, 6 Mar 2021 00:41:10 +0100 Jan Dankert New: Notice are collapsible. Mon, 26 Oct 2020 22:21:42 +0100 Jan Dankert Refactoring: Using TextMessage for creating Messages with user content. Wed, 14 Oct 2020 23:49:54 +0200 Jan Dankert Refactoring: Creating the target instance with a Factory (Java style); Asynchronous publishing of files. Sat, 26 Sep 2020 18:46:36 +0200 Jan Dankert Now compatible with PHP 5.4 again. Sat, 26 Sep 2020 04:26:55 +0200 Jan Dankert Refactoring: read configuration values with a class. Mon, 21 Sep 2020 22:48:59 +0200 Jan Dankert Complexe refactoring: Moving all generation logic from the model (Value,Page,File) to generators classes.