/** * Executes the command * * @param Config $config * @param Logger $logger */ public function execute(Config $config, Logger $logger) { echo self::LOGO, PHP_EOL; echo "Zephir version ", Compiler::getCurrentVersion(), PHP_EOL, PHP_EOL; echo "Usage: ", PHP_EOL; echo "\tcommand [options]", PHP_EOL; echo PHP_EOL; echo "Available commands:", PHP_EOL; foreach (Bootstrap::getCommands() as $command) { echo sprintf("\t%-20s%s\n", $command->getUsage(), $command->getDescription()); } echo PHP_EOL; echo "Options:", PHP_EOL; echo sprintf("\t%-20s%s\n", "-f([a-z0-9\\-]+)", "Enables compiler optimizations"); echo sprintf("\t%-20s%s\n", "-fno-([a-z0-9\\-]+)", "Disables compiler optimizations"); echo sprintf("\t%-20s%s\n", "-w([a-z0-9\\-]+)", "Turns a warning on"); echo sprintf("\t%-20s%s\n", "-W([a-z0-9\\-]+)", "Turns a warning off"); echo PHP_EOL; }
/** * Compiles the file generating a JSON intermediate representation * * @param Compiler $compiler * @return array * * @throws Exception */ public function genIR(Compiler $compiler) { $normalizedPath = str_replace(array(DIRECTORY_SEPARATOR, ":", '/'), '_', realpath($this->_filePath)); $compilePath = DIRECTORY_SEPARATOR . Compiler::getCurrentVersion() . DIRECTORY_SEPARATOR . $normalizedPath . ".js"; $zepRealPath = realpath($this->_filePath); $changed = false; $fileSystem = $compiler->getFileSystem(); if ($fileSystem->exists($compilePath)) { $modificationTime = $fileSystem->modificationTime($compilePath); if ($modificationTime < filemtime($zepRealPath)) { $changed = true; } } else { $changed = true; } $ir = null; if ($changed) { if (!function_exists('zephir_parse_file')) { throw new Exception("Parser extension couldn't be loaded"); } $ir = zephir_parse_file(file_get_contents($zepRealPath), $zepRealPath); $fileSystem->write($compilePath, json_encode($ir, JSON_PRETTY_PRINT)); } if ($changed || !$fileSystem->exists($compilePath . '.php')) { if (!isset($ir)) { $ir = json_decode($fileSystem->read($compilePath), true); } $data = '<?php return ' . var_export($ir, true) . ';'; $fileSystem->write($compilePath . '.php', $data); } return $fileSystem->requireFile($compilePath . '.php'); }
/** * This function does not perform operations in the temporary * directory but it caches the results to avoid reprocessing * * @param string $algorithm * @param string $path * @param boolean $cache * @return string */ public function getHashFile($algorithm, $path, $cache = false) { if ($cache == false) { return hash_file($algorithm, $path); } else { $changed = false; $cacheFile = $this->basePath . Compiler::getCurrentVersion() . DIRECTORY_SEPARATOR . str_replace(array(DIRECTORY_SEPARATOR, ':', '/'), '_', $path) . '.md5'; if (!file_exists($cacheFile)) { $hash = hash_file($algorithm, $path); file_put_contents($cacheFile, $hash); $changed = true; } else { if (filemtime($path) > filemtime($cacheFile)) { $hash = hash_file($algorithm, $path); file_put_contents($cacheFile, $hash); $changed = true; } } if (!$changed) { return file_get_contents($cacheFile); } return $hash; } }
/** * Executes the command * @param Config $config * @param Logger $logger */ public function execute(Config $config, Logger $logger) { echo Compiler::getCurrentVersion(), PHP_EOL; }