File modules/cms/generator/FileGenerator.class.php
Last commit: Fri Feb 17 02:02:18 2023 +0100 Jan Dankert Refactoring: Script-context should be the same in all environments; New: DslPdf for creating PDF with scriptbox ;)
1 <?php 2 3 4 namespace cms\generator; 5 6 7 use cms\generator\dsl\CMSDslInterpreter; 8 use cms\generator\dsl\DslObject; 9 use cms\generator\filter\AbstractFilter; 10 use cms\model\BaseObject; 11 use cms\model\File; 12 use cms\model\Value; 13 use dsl\DslException; 14 use logger\Logger; 15 use util\exception\GeneratorException; 16 use util\Text; 17 18 class FileGenerator extends BaseGenerator 19 { 20 21 22 public function __construct($fileContext ) 23 { 24 $this->context = $fileContext; 25 } 26 27 protected function generate() 28 { 29 $file = new File( $this->context->sourceObjectId ); 30 $file->load(); 31 32 try { 33 34 return $this->filterValue( $file ); 35 } 36 catch ( \Exception $e) { 37 throw new GeneratorException('Could not generate value of file '.$file->__toString().'.', $e ); 38 } 39 } 40 41 public function getPublicFilename() 42 { 43 $file = new File( $this->context->sourceObjectId ); 44 $file->load(); 45 46 $filename = $file->filename(); 47 48 if ( $file->extension ) 49 $filename .= '.'.$file->extension; 50 51 return $file->path().'/'.$filename; 52 } 53 54 55 /** 56 * @param $file File 57 * @return string 58 */ 59 protected function filterValue( $file ) 60 { 61 $contentId = $file->contentid; 62 63 $totalSettings = $file->getTotalSettings(); 64 $proxyFileId = @$totalSettings['proxy-file-id']; 65 66 if ( $proxyFileId ) { 67 $proxyFile = new File( $proxyFileId ); // This is a proxy for another file. 68 $proxyFile->load(); 69 $contentId = $proxyFile->contentid; 70 } 71 72 $v = new Value(); 73 $v->contentid = $contentId; 74 75 if ( $this->context->scheme == Producer::SCHEME_PREVIEW ) 76 $v->load(); 77 else 78 $v->loadPublished(); 79 80 $value = $v->file; 81 82 if ( $file->isScript ) { 83 $executor = new CMSDslInterpreter(); 84 85 $executor->setContext( $this->context ); 86 $executor->addContext( [ 87 'file' => new DslObject( (new BaseObject($this->context->getObjectId()))->load() ), 88 ]); 89 90 try { 91 $executor->runCode( $value ); 92 $value = $executor->getOutput(); 93 } 94 catch( DslException $e ) { 95 Logger::info($e); 96 throw new GeneratorException('Script error in script'."\n".Text::makeLineNumbers($value),$e); 97 } 98 } 99 100 foreach(\util\ArrayUtils::getSubArray($totalSettings, array( 'filter')) as $filterEntry ) 101 { 102 $filterName = ucfirst(@$filterEntry['name']); 103 $extension = @$filterEntry['extension']; 104 105 if ( $extension && strtolower($extension) != strtolower($file->getRealExtension()) ) 106 continue; // File extension does not match 107 108 $filterType = $this->context->scheme==Producer::SCHEME_PUBLIC?'public':'preview'; 109 110 $onPublish = (array) @$filterEntry['on']; 111 if ( ! $onPublish || in_array('all',$onPublish ) ) 112 $onPublish = ['edit','public','preview','show']; 113 114 if ( $onPublish && ! in_array($filterType,$onPublish)) 115 continue; // Publish type does not match 116 117 $parameter = (array) @$filterEntry['parameter']; 118 119 $filterClassNameWithNS = 'cms\\generator\\filter\\' . $filterName.'Filter'; 120 121 if ( !class_exists( $filterClassNameWithNS ) ) 122 throw new \LogicException("Filter '$filterName' does not exist."); 123 124 /** @var AbstractFilter $filter */ 125 $filter = new $filterClassNameWithNS(); 126 $filter->context = $this->context; 127 128 // Copy filter configuration to filter instance. 129 foreach( $parameter as $parameterName=>$parameterValue) { 130 if ( property_exists($filter,$parameterName)) 131 $filter->$parameterName = $parameterValue; 132 else 133 throw new \LogicException("Filter '".$filterName."' has no property '".$parameterName ); 134 } 135 136 137 // Execute the filter. 138 Logger::debug("Filtering '$file->filename' with filter '$filterName'."); 139 140 try { 141 142 $value = $filter->filter( $value ); 143 } catch( \Exception $e ) { 144 // Filter has some undefined error. 145 Logger::warn( $e->getTraceAsString() ); 146 throw new GeneratorException('Filter \''.$filterName.'\' has an error.', $e ); 147 } 148 } 149 150 return $value; 151 152 } 153 154 155 /** 156 * Calculates the MIME type of this file. 157 * 158 * @return string 159 */ 160 public function getMimeType() 161 { 162 $file = new File( $this->context->sourceObjectId ); 163 $file->load(); 164 $ext = strtolower( $file->getRealExtension() ); 165 166 $mimeType = File::getMimeType( $ext ); 167 168 return( $mimeType ); 169 } 170 }
Downloadmodules/cms/generator/FileGenerator.class.php
History Fri, 17 Feb 2023 02:02:18 +0100 Jan Dankert Refactoring: Script-context should be the same in all environments; New: DslPdf for creating PDF with scriptbox ;) Tue, 14 Feb 2023 21:22:00 +0100 Jan Dankert New: ScriptFilter for filtering file values with the script language. Tue, 14 Feb 2023 00:23:13 +0100 Jan Dankert New filters: Robots (for robots.txt) and Sitemap. Sun, 29 Jan 2023 00:20:21 +0100 Jan Dankert New node type "Script". Sun, 13 Feb 2022 20:14:39 +0100 dankert Refactoring: removed duplicate code, use inheritance ;) Tue, 9 Nov 2021 23:52:56 +0100 Jan Dankert Some fixes for reading content from the new content table. Sun, 14 Mar 2021 02:14:31 +0100 Jan Dankert Fix: The public filename of files must contain their path... Sat, 13 Mar 2021 22:38:30 +0100 Jan Dankert New filter for using links in text nodes (useful in CSS or script files) Thu, 4 Mar 2021 02:24:45 +0100 Jan Dankert New: The calculation of the mime types should be done in the generators. Sat, 27 Feb 2021 01:38:36 +0100 Jan Dankert Fix: Unknown variable if no filter is set. Sat, 27 Feb 2021 00:01:45 +0100 Jan Dankert Fix: File-filtering. Sat, 14 Nov 2020 22:20:32 +0100 Jan Dankert Fix: Publish Files with extension. Wed, 14 Oct 2020 00:02:28 +0200 Jan Dankert Fix: File must be loaded when loading the value. Sat, 26 Sep 2020 05:02:20 +0200 Jan Dankert Fix: The file filtering must be done in the file generator. 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.