1
0
mirror of https://github.com/danog/class-finder.git synced 2024-11-26 20:14:59 +01:00

Provide a more helpful error message if files support fails due to exec() being disabled.

This commit is contained in:
Hayden Pierce 2018-10-21 09:39:21 -05:00
parent 6af0937964
commit 631e09d802
2 changed files with 64 additions and 0 deletions

View File

@ -0,0 +1,51 @@
FilesFinder requires that exec() is available.
----------------------------------------------
Example PHP:
```
<?php
require_once __DIR__ . '/../vendor/autoload.php';
use HaydenPierce\ClassFinder\ClassFinder;
ClassFinder::enableFilesSupport();
$classes = ClassFinder::getClassesInNamespace('Acme\Foo\Bar');
```
Results in this exception:
> FilesFinder requires that exec() is available. Check your php.ini to see if it is disabled.
When running ClassFinder with support for autoloaded classes in `files`, ClassFinder must execute the included file in a
shell to determine any defined classes in it. `exec()` is used to accomplish this. In some environments, hosts may
intentionally disable the use `exec()` as a security or performance precaution.
Possible Solution 1
-------------------
Disable `files` support.
The majority of users won't need `files` support. If you've copy / pasted a snippet (including from documentation here)
that enabled it, you should remove it and see if you're part of the 99% that doesn't need this feature. You may also
want to explicitly disable it:
```
ClassFinder::disableFilesSupport();
$classes = ClassFinder::getClassesInNamespace('Acme\Foo\Bar');
```
Possible Solution 2
-------------------
Ensure `exec()` is available to PHP.
Find your `php.ini` file and look for a configuration value called `disabled_functions`:
```
disable_functions = exec,passthru,shell_exec,system,proc_open,popen
```
Remove exec from the list and restart your webserver.

View File

@ -1,15 +1,28 @@
<?php
namespace HaydenPierce\ClassFinder\Files;
use HaydenPierce\ClassFinder\Exception\ClassFinderException;
use HaydenPierce\ClassFinder\FinderInterface;
class FilesFinder implements FinderInterface
{
private $factory;
/**
* FilesFinder constructor.
* @param FilesEntryFactory $factory
* @throws ClassFinderException
*/
public function __construct(FilesEntryFactory $factory)
{
$this->factory = $factory;
if (!function_exists('exec')) {
throw new ClassFinderException(sprintf(
'FilesFinder requires that exec() is available. Check your php.ini to see if it is disabled. See "%s" for details.',
'https://gitlab.com/hpierce1102/ClassFinder/blob/master/docs/exceptions/filesExecNotAvailable.md'
));
}
}
public function isNamespaceKnown($namespace)