openrat-cms

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

FileGenerator.class.php (3030B)


      1 <?php
      2 
      3 
      4 namespace cms\generator;
      5 
      6 
      7 use cms\generator\filter\AbstractFilter;
      8 use cms\model\File;
      9 use logger\Logger;
     10 use util\exception\GeneratorException;
     11 
     12 class FileGenerator extends BaseGenerator
     13 {
     14 
     15 
     16 	public function __construct($fileContext )
     17 	{
     18 		$this->context = $fileContext;
     19 	}
     20 
     21 	protected function generate()
     22 	{
     23 		$file = new File( $this->context->sourceObjectId );
     24 		$file->load();
     25 		return $this->filterValue( $file );
     26 	}
     27 
     28 	public function getPublicFilename()
     29 	{
     30 		$file = new File( $this->context->sourceObjectId );
     31 		$file->load();
     32 
     33 		$filename = $file->filename();
     34 
     35 		if   ( $file->extension )
     36 			$filename .= '.'.$file->extension;
     37 
     38 		return $file->path().'/'.$filename;
     39 	}
     40 
     41 
     42 	/**
     43 	 * @param $file File
     44 	 * @return string
     45 	 */
     46 	private function filterValue( $file )
     47 	{
     48 		$totalSettings = $file->getTotalSettings();
     49 
     50 		$proxyFileId = @$totalSettings['proxy-file-id'];
     51 
     52 		if   ( $proxyFileId )
     53 			$value = (new File( $proxyFileId ))->loadValue(); // This is a proxy for another file.
     54 		else
     55 			$value = $file->loadValue();
     56 
     57 		foreach(\util\ArrayUtils::getSubArray($totalSettings, array( 'filter')) as $filterEntry )
     58 		{
     59 			$filterName = ucfirst(@$filterEntry['name']);
     60 			$extension  = @$filterEntry['extension'];
     61 
     62 			if   ( $extension && strtolower($extension) != strtolower($file->getRealExtension()) )
     63 				continue; // File extension does not match
     64 
     65 			$filterType = $this->context->scheme==Producer::SCHEME_PUBLIC?'public':'preview';
     66 
     67 			$onPublish = (array) @$filterEntry['on'];
     68 			if ( ! $onPublish || in_array('all',$onPublish ) )
     69 				$onPublish = ['edit','public','preview','show'];
     70 
     71 			if   ( $onPublish && ! in_array($filterType,$onPublish))
     72 				continue; // Publish type does not match
     73 
     74 			$parameter = (array) @$filterEntry['parameter'];
     75 
     76 			$filterClassNameWithNS = 'cms\\generator\\filter\\' . $filterName.'Filter';
     77 
     78 			if   ( !class_exists( $filterClassNameWithNS ) )
     79 				throw new \LogicException("Filter '$filterName' does not exist.");
     80 
     81 			/** @var AbstractFilter $filter */
     82 			$filter = new $filterClassNameWithNS();
     83 			$filter->context = $this->context;
     84 
     85 			// Copy filter configuration to filter instance.
     86 			foreach( $parameter as $parameterName=>$parameterValue) {
     87 				if   ( property_exists($filter,$parameterName))
     88 					$filter->$parameterName = $parameterValue;
     89 			}
     90 
     91 
     92 			// Execute the filter.
     93 			Logger::debug("Filtering '$file->filename' with filter '$filterName'.");
     94 
     95 			try {
     96 
     97 				$value = $filter->filter( $value );
     98 			} catch( \Exception $e ) {
     99 				// Filter has some undefined error.
    100 				Logger::warn( $e->getTraceAsString() );
    101 				throw new GeneratorException('Could not generate file '.$file->objectid.'. Filter '.$filterName.' has an error.', $e );
    102 			}
    103 		}
    104 
    105 		return $value;
    106 
    107 	}
    108 
    109 
    110 	/**
    111 	 * Calculates the MIME type of this file.
    112 	 *
    113 	 * @return string
    114 	 */
    115 	public function getMimeType()
    116 	{
    117 		$file = new File( $this->context->sourceObjectId );
    118 		$file->load();
    119 		$ext = strtolower( $file->getRealExtension() );
    120 
    121 		$mimeType = File::getMimeType( $ext );
    122 
    123 		return( $mimeType );
    124 	}
    125 }