1
0
Fork 0

Let JsonMapper intialize the options

To sanitize the file type option, we provide a setter method for the property that will be called by the JsonMapper.
pull/668/head
Jürgen Steitz 2017-02-18 19:30:34 +01:00
parent f7175bc195
commit 94336941bd
3 changed files with 10 additions and 37 deletions

View File

@ -112,7 +112,7 @@ class Indexer
public function index(): Promise public function index(): Promise
{ {
return coroutine(function () { return coroutine(function () {
$fileTypes = implode(',', $this->options->getFileTypes()); $fileTypes = implode(',', $this->options->fileTypes);
$pattern = Path::makeAbsolute('**/*{' . $fileTypes . '}', $this->rootPath); $pattern = Path::makeAbsolute('**/*{' . $fileTypes . '}', $this->rootPath);
$uris = yield $this->filesFinder->find($pattern); $uris = yield $this->filesFinder->find($pattern);

View File

@ -167,10 +167,10 @@ class LanguageServer extends AdvancedJsonRpc\Dispatcher
* @param ClientCapabilities $capabilities The capabilities provided by the client (editor) * @param ClientCapabilities $capabilities The capabilities provided by the client (editor)
* @param string|null $rootPath The rootPath of the workspace. Is null if no folder is open. * @param string|null $rootPath The rootPath of the workspace. Is null if no folder is open.
* @param int|null $processId The process Id of the parent process that started the server. Is null if the process has not been started by another process. If the parent process is not alive then the server should exit (see exit notification) its process. * @param int|null $processId The process Id of the parent process that started the server. Is null if the process has not been started by another process. If the parent process is not alive then the server should exit (see exit notification) its process.
* @param mixed $initializationOptions The options send from client to initialize the server * @param Options $initializationOptions The options send from client to initialize the server
* @return Promise <InitializeResult> * @return Promise <InitializeResult>
*/ */
public function initialize(ClientCapabilities $capabilities, string $rootPath = null, int $processId = null, $initializationOptions = null): Promise public function initialize(ClientCapabilities $capabilities, string $rootPath = null, int $processId = null, Options $initializationOptions = null): Promise
{ {
return coroutine(function () use ($capabilities, $rootPath, $processId, $initializationOptions) { return coroutine(function () use ($capabilities, $rootPath, $processId, $initializationOptions) {
@ -191,7 +191,6 @@ class LanguageServer extends AdvancedJsonRpc\Dispatcher
$this->projectIndex = new ProjectIndex($sourceIndex, $dependenciesIndex, $this->composerJson); $this->projectIndex = new ProjectIndex($sourceIndex, $dependenciesIndex, $this->composerJson);
$stubsIndex = StubsIndex::read(); $stubsIndex = StubsIndex::read();
$this->globalIndex = new GlobalIndex($stubsIndex, $this->projectIndex); $this->globalIndex = new GlobalIndex($stubsIndex, $this->projectIndex);
$options = new Options($initializationOptions);
// The DefinitionResolver should look in stubs, the project source and dependencies // The DefinitionResolver should look in stubs, the project source and dependencies
$this->definitionResolver = new DefinitionResolver($this->globalIndex); $this->definitionResolver = new DefinitionResolver($this->globalIndex);
@ -238,7 +237,7 @@ class LanguageServer extends AdvancedJsonRpc\Dispatcher
$this->documentLoader, $this->documentLoader,
$this->composerLock, $this->composerLock,
$this->composerJson, $this->composerJson,
$options $initializationOptions
); );
$indexer->index()->otherwise('\\LanguageServer\\crash'); $indexer->index()->otherwise('\\LanguageServer\\crash');
} }

View File

@ -7,51 +7,25 @@ class Options
/** /**
* Filetypes the indexer should process * Filetypes the indexer should process
* *
* @var array * @var string[]
*/ */
private $fileTypes = [".php"]; public $fileTypes = ['.php'];
/** /**
* @param \Traversable|\stdClass|array|null $options * Validate/Filter input and set options for file types
*/
public function __construct($options = null)
{
// Do nothing when the $options parameter is not an object
if (!is_object($options) && !is_array($options) && (!$options instanceof \Traversable)) {
return;
}
foreach ($options as $option => $value) {
$method = 'set' . ucfirst($option);
call_user_func([$this, $method], $value);
}
}
/**
* Validate and set options for file types
* *
* @param array $fileTypes List of file types * @param array $fileTypes List of file types
*/ */
public function setFileTypes(array $fileTypes) public function setFileTypes(array $fileTypes)
{ {
$fileTypes = filter_var_array($fileTypes, FILTER_SANITIZE_STRING); $fileTypes = filter_var_array($fileTypes, FILTER_SANITIZE_STRING);
$fileTypes = filter_var($fileTypes, FILTER_CALLBACK, ['options' => [$this, 'filterFileTypes']]); $fileTypes = filter_var($fileTypes, FILTER_CALLBACK, ['options' => [$this, 'filterFileTypes']]); // validate file type format
$fileTypes = array_filter($fileTypes); $fileTypes = array_filter($fileTypes, 'strlen'); // filter empty items
$fileTypes = array_values($fileTypes); //rebase indexes
$this->fileTypes = !empty($fileTypes) ? $fileTypes : $this->fileTypes; $this->fileTypes = !empty($fileTypes) ? $fileTypes : $this->fileTypes;
} }
/**
* Get list of registered file types
*
* @return array
*/
public function getFileTypes(): array
{
return $this->fileTypes;
}
/** /**
* Filter valid file type * Filter valid file type
* *