Chris@13: parser = $parserFactory->createParser(); Chris@13: } Chris@13: Chris@13: /** Chris@13: * Reload code on input. Chris@13: * Chris@13: * @param Shell $shell Chris@13: * @param string $input Chris@13: */ Chris@13: public function onInput(Shell $shell, $input) Chris@13: { Chris@13: $this->reload($shell); Chris@13: } Chris@13: Chris@13: /** Chris@13: * Look through included files and update anything with a new timestamp. Chris@13: * Chris@13: * @param Shell $shell Chris@13: */ Chris@13: private function reload(Shell $shell) Chris@13: { Chris@17: \clearstatcache(); Chris@13: $modified = []; Chris@13: Chris@17: foreach (\get_included_files() as $file) { Chris@17: $timestamp = \filemtime($file); Chris@13: Chris@13: if (!isset($this->timestamps[$file])) { Chris@13: $this->timestamps[$file] = $timestamp; Chris@13: continue; Chris@13: } Chris@13: Chris@13: if ($this->timestamps[$file] === $timestamp) { Chris@13: continue; Chris@13: } Chris@13: Chris@13: if (!$this->lintFile($file)) { Chris@17: $msg = \sprintf('Modified file "%s" could not be reloaded', $file); Chris@13: $shell->writeException(new ParseErrorException($msg)); Chris@13: continue; Chris@13: } Chris@13: Chris@13: $modified[] = $file; Chris@13: $this->timestamps[$file] = $timestamp; Chris@13: } Chris@13: Chris@13: // switch (count($modified)) { Chris@13: // case 0: Chris@13: // return; Chris@13: Chris@13: // case 1: Chris@13: // printf("Reloading modified file: \"%s\"\n", str_replace(getcwd(), '.', $file)); Chris@13: // break; Chris@13: Chris@13: // default: Chris@13: // printf("Reloading %d modified files\n", count($modified)); Chris@13: // break; Chris@13: // } Chris@13: Chris@13: foreach ($modified as $file) { Chris@13: runkit_import($file, ( Chris@13: RUNKIT_IMPORT_FUNCTIONS | Chris@13: RUNKIT_IMPORT_CLASSES | Chris@13: RUNKIT_IMPORT_CLASS_METHODS | Chris@13: RUNKIT_IMPORT_CLASS_CONSTS | Chris@13: RUNKIT_IMPORT_CLASS_PROPS | Chris@13: RUNKIT_IMPORT_OVERRIDE Chris@13: )); Chris@13: } Chris@13: } Chris@13: Chris@13: /** Chris@13: * Should this file be re-imported? Chris@13: * Chris@13: * Use PHP-Parser to ensure that the file is valid PHP. Chris@13: * Chris@13: * @param string $file Chris@13: * Chris@13: * @return bool Chris@13: */ Chris@13: private function lintFile($file) Chris@13: { Chris@13: // first try to parse it Chris@13: try { Chris@17: $this->parser->parse(\file_get_contents($file)); Chris@13: } catch (\Exception $e) { Chris@13: return false; Chris@13: } Chris@13: Chris@13: return true; Chris@13: } Chris@13: }