openrat-cms

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

FileGenerator.class.php (2414B)


      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 $filename;
     39 	}
     40 
     41 
     42 	/**
     43 	 * @param $file File
     44 	 * @return string
     45 	 */
     46 	private function filterValue($file )
     47 	{
     48 		$value = $file->loadValue();
     49 
     50 		foreach(\util\ArrayUtils::getSubArray($file->getTotalSettings(), array( 'filter')) as $filterEntry )
     51 		{
     52 			$filterName = @$filterEntry['name'];
     53 			$extension  = @$filterEntry['extension'];
     54 
     55 			if   ( $extension && strtolower($extension) != strtolower($file->getRealExtension()) )
     56 				continue; // File extension does not match
     57 
     58 			$filterType = $this->context->scheme==Producer::SCHEME_PUBLIC?'public':'preview';
     59 
     60 			$onPublish = (array) @$filterEntry['on'];
     61 			if ( ! $onPublish || in_array('all',$onPublish ) )
     62 				$onPublish = ['edit','public','preview','show'];
     63 
     64 			if   ( $onPublish && ! in_array($filterType,$onPublish))
     65 				continue; // Publish type does not match
     66 
     67 			$parameter = (array) @$filterEntry['parameter'];
     68 
     69 			$filterClassNameWithNS = 'cms\\publish\\filter\\' . $filterName.'Filter';
     70 
     71 			if   ( !class_exists( $filterClassNameWithNS ) ) {
     72 				Logger::warn("Filter '$filterName' does not exist.");
     73 				continue;
     74 			}
     75 
     76 			/** @var AbstractFilter $filter */
     77 			$filter = new $filterClassNameWithNS();
     78 
     79 			// Copy filter configuration to filter instance.
     80 			foreach( $parameter as $name=>$value) {
     81 				if   ( property_exists($filter,$name))
     82 					$filter->$name = $value;
     83 			}
     84 
     85 
     86 			// Execute the filter.
     87 			Logger::debug("Filtering '$file->filename' with filter '$filterName'.");
     88 
     89 			try {
     90 
     91 				$value = $filter->filter( $value );
     92 			} catch( \Exception $e ) {
     93 				// Filter has some undefined error.
     94 				Logger::warn( $e->getTraceAsString() );
     95 				throw new GeneratorException('Could not generate file '.$file->objectid.'. Filter '.$filterName.' has an error.', $e );
     96 			}
     97 		}
     98 
     99 		return $value;
    100 
    101 	}
    102 
    103 }