/** * Initializes a Zephir extension * * @param CommandInterface $command * * @throws Exception */ public function init(CommandInterface $command) { /** * If init namespace is specified */ $namespace = $command->getParameter('namespace'); if (!$namespace) { throw new Exception("Cannot obtain a valid initial namespace for the project"); } /** * Tell the user the name could be reserved by another extension */ if (extension_loaded($namespace)) { $this->logger->output('This extension can have conflicts with an existing loaded extension'); } $this->config->set('namespace', $namespace); $this->config->set('name', $namespace); if (!is_dir($namespace)) { mkdir($namespace); } chdir($namespace); if (!is_dir($namespace)) { mkdir($namespace); } /** * Create 'kernel' */ if (!is_dir('ext/kernel')) { mkdir('ext/kernel', 0755, true); } // Copy the latest kernel files $this->recursiveProcess($this->backend->getInternalKernelPath(), 'ext/kernel'); }
/** * @param string $zephirCode * * @throws \Exception */ public function isValid($namespace) { if (!defined('ZEPHIRPATH')) { define('ZEPHIRPATH', realpath(__DIR__ . '/../../vendor/phalcon/zephir') . '/'); } $generateCommand = new CommandGenerate(); $cleanCommand = new CommandFullClean(); try { $config = new Config(); $config->set('namespace', $namespace); $config->set('silent', true); $cleanCommand->execute($config, new ZephirLogger($config)); $generateCommand->execute($config, new ZephirLogger($config)); } catch (CompilerException $e) { throw new \Exception(sprintf('Error on %s', $e->getMessage())); } return true; }
/** * Build a zephir extension and return the extension path * * @param string $zephir * @param boolean $silent * @throws \Exception * @return string */ public function build($zephir, $silent) { $dto = $this->zephirClassInfo->getZephirCodeInfo($zephir); $this->fileWorker->writeZephirFile($dto, $zephir); $this->defineZephirHome(); try { $config = new Config(); $config->set('namespace', $dto->getExtensionName()); $config->set('silent', $silent); if (is_dir('ext')) { $this->commandFullClean->execute($config, new ZephirLogger($config)); } $this->commandBuild->execute($config, new ZephirLogger($config)); } catch (\Exception $e) { $this->fileWorker->rmdirRecursive($dto->getBaseDir()); throw new \Exception(sprintf('Error on %s', $e->getMessage())); } return 'ext/modules/' . $dto->getExtensionName() . '.so'; }
/** * Test saveOnExit method. */ public function testSaveOnExit() { chdir(sys_get_temp_dir()); $config = new Config(); $config->set('name', 'foo'); $config->saveOnExit(); $configJson = json_decode(file_get_contents('config.json'), true); $this->assertInternalType('array', $configJson); $this->assertSame($configJson['name'], 'foo'); $this->cleanTmpConfigFile(); }
/** * @param string $zephirCode * * @throws \Exception */ public function isValid($namespace) { $currentDir = getcwd(); chdir(FileWriter::BASE_DESTINATION . $namespace); if (!defined('ZEPHIRPATH')) { define('ZEPHIRPATH', realpath(__DIR__ . '/../../vendor/phalcon/zephir') . '/'); } $generateCommand = new CommandGenerate(); $cleanCommand = new CommandFullClean(); try { $config = new Config(); $config->set('namespace', strtolower($namespace)); $config->set('silent', true); if (is_dir('ext')) { $cleanCommand->execute($config, new ZephirLogger($config)); } $generateCommand->execute($config, new ZephirLogger($config)); } catch (Exception $e) { chdir($currentDir); throw new \Exception(sprintf('Error on %s', $e->getMessage())); } chdir($currentDir); return true; }
public function testSetWithNamespace() { $config = new Config(); $config->set('unused-variable', false, 'warnings'); $this->assertFalse($config->get('unused-variable', 'warnings')); }
/** * Boots the compiler executing the specified action */ public static function boot() { try { /** * Global config */ $config = new Config(); register_shutdown_function(array($config, 'saveOnExit')); /** * Global logger */ $logger = new Logger($config); if (isset($_SERVER['argv'][1])) { $action = $_SERVER['argv'][1]; } else { $action = 'help'; } /** * Change configurations flags */ if ($_SERVER['argc'] >= 2) { for ($i = 2; $i < $_SERVER['argc']; $i++) { $parameter = $_SERVER['argv'][$i]; if (preg_match('/^-fno-([a-z0-9\\-]+)$/', $parameter, $matches)) { $config->set($matches[1], false, 'optimizations'); continue; } if (preg_match('/^-f([a-z0-9\\-]+)$/', $parameter, $matches)) { $config->set($matches[1], true, 'optimizations'); } if (preg_match('/^-W([a-z0-9\\-]+)$/', $parameter, $matches)) { $logger->set($matches[1], false, 'warnings'); continue; } if (preg_match('/^-w([a-z0-9\\-]+)$/', $parameter, $matches)) { $logger->set($matches[1], true, 'warnings'); continue; } if (preg_match('/^--([a-z0-9\\-]+)$/', $parameter, $matches)) { $config->set($matches[1], true, 'extra'); continue; } if (preg_match('/^--([a-z0-9\\-]+)=(.*)$/', $parameter, $matches)) { $config->set($matches[1], $matches[2], 'extra'); continue; } switch ($parameter) { case '-w': $config->set('silent', true); break; case '-v': $config->set('verbose', true); break; case '-V': $config->set('verbose', false); break; default: break; } } } /** * Register built-in commands * @var $item \DirectoryIterator */ foreach (new \DirectoryIterator(ZEPHIRPATH . 'Library/Commands') as $item) { if (!$item->isDir()) { $className = 'Zephir\\Commands\\' . str_replace('.php', '', $item->getBaseName()); $class = new \ReflectionClass($className); if (!$class->isAbstract() && !$class->isInterface()) { /** * @var $command CommandAbstract */ $command = new $className(); if (!$command instanceof CommandAbstract) { throw new \Exception('Class ' . $class->name . ' must be instance of CommandAbstract'); } self::$commands[$command->getCommand()] = $command; } } } if (!isset(self::$commands[$action])) { $message = 'Unrecognized action "' . $action . '"'; $metaphone = metaphone($action); foreach (self::$commands as $key => $command) { if (metaphone($key) == $metaphone) { $message .= PHP_EOL . PHP_EOL . 'Did you mean "' . $key . '"?'; } } throw new \Exception($message); } /** * Execute the command */ self::$commands[$action]->execute($config, $logger); } catch (\Exception $e) { self::showException($e, isset($config) ? $config : null); } }