Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 39 |
|
0.00% |
0 / 7 |
CRAP | |
0.00% |
0 / 1 |
| PhpCodeCompilationDataService | |
0.00% |
0 / 39 |
|
0.00% |
0 / 7 |
240 | |
0.00% |
0 / 1 |
| __construct | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
2 | |||
| getOutputFileType | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| writeCompilationData | |
0.00% |
0 / 7 |
|
0.00% |
0 / 1 |
2 | |||
| readCompilationData | |
0.00% |
0 / 15 |
|
0.00% |
0 / 1 |
30 | |||
| useCompactCacheFile | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
6 | |||
| ensureCacheDirectory | |
0.00% |
0 / 7 |
|
0.00% |
0 / 1 |
12 | |||
| cacheKey | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
6 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace oat\taoQtiTest\models; |
| 4 | |
| 5 | use qtism\data\storage\php\PhpDocument; |
| 6 | use qtism\data\storage\php\PhpStorageException; |
| 7 | use qtism\data\QtiComponent; |
| 8 | |
| 9 | /** |
| 10 | * PHP Code Compilation Data Service. |
| 11 | * |
| 12 | * This Compilation Data Service implementation aims at compiling |
| 13 | * Delivery data as plain PHP code. |
| 14 | */ |
| 15 | class PhpCodeCompilationDataService extends CompilationDataService |
| 16 | { |
| 17 | protected $cacheDir; |
| 18 | |
| 19 | public const OPTION_CACHE_COMPACT_TEST_FILE = 'cacheCompactTestFile'; |
| 20 | |
| 21 | public const OUTPUT_FILE_TYPE = 'php'; |
| 22 | |
| 23 | public function __construct($options = []) |
| 24 | { |
| 25 | parent::__construct($options); |
| 26 | |
| 27 | $this->cacheDir = sys_get_temp_dir() . '/taooldtestrunnerphpcache'; |
| 28 | } |
| 29 | |
| 30 | /** |
| 31 | * @return string |
| 32 | */ |
| 33 | public function getOutputFileType() |
| 34 | { |
| 35 | return self::OUTPUT_FILE_TYPE; |
| 36 | } |
| 37 | |
| 38 | public function writeCompilationData( |
| 39 | \tao_models_classes_service_StorageDirectory $compilationDirectory, |
| 40 | $path, |
| 41 | QtiComponent $object |
| 42 | ) { |
| 43 | $path .= '.' . self::OUTPUT_FILE_TYPE; |
| 44 | $doc = new PhpDocument(); |
| 45 | $doc->setDocumentComponent($object); |
| 46 | |
| 47 | $compilationDirectory->write( |
| 48 | $path, |
| 49 | $doc->saveToString() |
| 50 | ); |
| 51 | } |
| 52 | |
| 53 | public function readCompilationData( |
| 54 | \tao_models_classes_service_StorageDirectory $compilationDirectory, |
| 55 | $path, |
| 56 | $cacheInfo = '' |
| 57 | ) { |
| 58 | $path .= '.' . self::OUTPUT_FILE_TYPE; |
| 59 | $dir = $this->ensureCacheDirectory($compilationDirectory); |
| 60 | $cacheKey = $this->cacheKey($cacheInfo); |
| 61 | $cacheFile = "${dir}/${cacheKey}." . self::OUTPUT_FILE_TYPE; |
| 62 | |
| 63 | if ($this->useCompactCacheFile() && !is_file($cacheFile)) { |
| 64 | file_put_contents($cacheFile, $compilationDirectory->read($path)); |
| 65 | } |
| 66 | |
| 67 | try { |
| 68 | $doc = new PhpDocument(); |
| 69 | if ($this->useCompactCacheFile()) { |
| 70 | $doc->load($cacheFile); |
| 71 | } else { |
| 72 | $doc->loadFromString($compilationDirectory->read($path)); |
| 73 | } |
| 74 | } catch (PhpStorageException $e) { |
| 75 | $msg = "PHP Compilation Data in directory '" . $compilationDirectory->getId() . "' at path '" . $path |
| 76 | . "' could not be executed properly."; |
| 77 | throw new \common_Exception($msg); |
| 78 | } |
| 79 | |
| 80 | return $doc->getDocumentComponent(); |
| 81 | } |
| 82 | |
| 83 | /** |
| 84 | * @return bool|mixed |
| 85 | */ |
| 86 | protected function useCompactCacheFile() |
| 87 | { |
| 88 | $value = $this->getOption(static::OPTION_CACHE_COMPACT_TEST_FILE); |
| 89 | |
| 90 | return is_null($value) ? true : $value; |
| 91 | } |
| 92 | |
| 93 | protected function ensureCacheDirectory(\tao_models_classes_service_StorageDirectory $compilationDirectory) |
| 94 | { |
| 95 | $dirId = md5($compilationDirectory->getId()); |
| 96 | |
| 97 | for ($i = 1; $i < 6; $i += 2) { |
| 98 | $dirId = substr_replace($dirId, '/', $i, 0); |
| 99 | } |
| 100 | |
| 101 | $path = $this->cacheDir . "/${dirId}"; |
| 102 | |
| 103 | if (!is_dir($path)) { |
| 104 | @mkdir($path, 0700, true); |
| 105 | } |
| 106 | |
| 107 | return $path; |
| 108 | } |
| 109 | |
| 110 | protected function cacheKey($cacheInfo = '') |
| 111 | { |
| 112 | $key = 'php-data'; |
| 113 | |
| 114 | if (!empty($cacheInfo)) { |
| 115 | $key .= "-${cacheInfo}"; |
| 116 | } |
| 117 | |
| 118 | return $key; |
| 119 | } |
| 120 | } |