├── LICENSE.md ├── README.md ├── composer.json ├── phpstan.neon └── src ├── Internal ├── CacheReader.php ├── ConfigReader.php └── ReaderInterface.php ├── Locator.php └── Resolver ├── LinuxPathResolver.php ├── MacOSPathResolver.php ├── PathResolver.php ├── PathResolverInterface.php ├── UnixAwareResolver.php └── WindowsPathResolver.php /LICENSE.md: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright © Kirill Nesmeyanov 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Bin Locator 2 | 3 |
10 | 13 | 14 | Library for searching binary files in the operating system. 15 | 16 | ## Requirements 17 | 18 | - PHP >= 7.4 19 | 20 | ## Installation 21 | 22 | Library is available as composer repository and can be installed using the 23 | following command in a root of your project. 24 | 25 | ```sh 26 | $ composer require ffi/location 27 | ``` 28 | 29 | ## Usage 30 | 31 | ### Existence Check 32 | 33 | Checking the library for existence. 34 | 35 | ```php 36 | use FFI\Location\Locator; 37 | 38 | $exists = Locator::exists('libGL.so'); 39 | // Expected true in the case that the binary exists and false otherwise 40 | ``` 41 | 42 | ### Binary Pathname 43 | 44 | Getting the full path to the library. 45 | 46 | ```php 47 | use FFI\Location\Locator; 48 | 49 | $pathname = Locator::pathname('libGL.so'); 50 | // Expected "/usr/lib/x86_64-linux-gnu/libGL.so.1.7.0" or null 51 | // in the case that the library cannot be found 52 | ``` 53 | 54 | ### Binary Resolving 55 | 56 | Checking multiple names to find the most suitable library. 57 | 58 | ```php 59 | use FFI\Location\Locator; 60 | 61 | $pathname = Locator::resolve('example.so', 'test.so', 'libvulkan.so'); 62 | // Expected "/usr/lib/x86_64-linux-gnu/libvulkan.so.1.2.131" or null 63 | // in the case that the library cannot be found 64 | ``` 65 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "ffi/location", 3 | "type": "library", 4 | "description": "PHP library for determining the physical location of binaries", 5 | "license": "MIT", 6 | "keywords": ["ffi", "bin", "location", "utility"], 7 | "support": { 8 | "source": "https://github.com/php-ffi/location", 9 | "issues": "https://github.com/php-ffi/location/issues", 10 | "docs": "https://github.com/php-ffi/location/blob/master/README.md" 11 | }, 12 | "authors": [ 13 | { 14 | "name": "Nesmeyanov Kirill", 15 | "email": "nesk@xakep.ru", 16 | "homepage": "https://nesk.me", 17 | "role": "maintainer" 18 | } 19 | ], 20 | "require": { 21 | "php": "^7.4|^8.0" 22 | }, 23 | "autoload": { 24 | "psr-4": { 25 | "FFI\\Location\\": "src" 26 | } 27 | }, 28 | "require-dev": { 29 | "phpunit/phpunit": "^9.5", 30 | "friendsofphp/php-cs-fixer": "^3.53", 31 | "phpstan/phpstan": "^2.0", 32 | "phpstan/phpstan-deprecation-rules": "^2.0", 33 | "phpstan/phpstan-strict-rules": "^2.0" 34 | }, 35 | "autoload-dev": { 36 | "psr-4": { 37 | "FFI\\Location\\Tests\\": "tests" 38 | } 39 | }, 40 | "config": { 41 | "optimize-autoloader": true, 42 | "preferred-install": { 43 | "*": "dist" 44 | }, 45 | "sort-packages": true 46 | }, 47 | "scripts": { 48 | "test": ["@test:unit"], 49 | "test:unit": "phpunit --testdox --testsuite=unit", 50 | "linter": "@linter:check", 51 | "linter:check": "phpstan analyse --configuration phpstan.neon", 52 | "linter:baseline": "@linter:check -- --generate-baseline", 53 | "phpcs": "@phpcs:check", 54 | "phpcs:check": "@phpcs:fix --dry-run", 55 | "phpcs:fix": "php-cs-fixer fix --config=.php-cs-fixer.php --allow-risky=yes --verbose --diff" 56 | }, 57 | "extra": { 58 | "branch-alias": { 59 | "dev-main": "1.0.x-dev", 60 | "dev-master": "1.0.x-dev" 61 | } 62 | }, 63 | "minimum-stability": "dev", 64 | "prefer-stable": true 65 | } 66 | -------------------------------------------------------------------------------- /phpstan.neon: -------------------------------------------------------------------------------- 1 | includes: 2 | - phar://phpstan.phar/conf/bleedingEdge.neon 3 | - vendor/phpstan/phpstan-deprecation-rules/rules.neon 4 | - vendor/phpstan/phpstan-strict-rules/rules.neon 5 | parameters: 6 | level: max 7 | phpVersion: 8 | min: 70400 9 | max: 80400 10 | parallel: 11 | jobSize: 20 12 | maximumNumberOfProcesses: 4 13 | minimumNumberOfJobsPerProcess: 2 14 | paths: 15 | - src 16 | tmpDir: vendor/.cache.phpstan 17 | rememberPossiblyImpureFunctionValues: false 18 | checkTooWideReturnTypesInProtectedAndPublicMethods: true 19 | checkImplicitMixed: true 20 | checkBenevolentUnionTypes: true 21 | reportPossiblyNonexistentGeneralArrayOffset: true 22 | reportPossiblyNonexistentConstantArrayOffset: true 23 | reportAlwaysTrueInLastCondition: true 24 | reportAnyTypeWideningInVarTag: true 25 | checkMissingOverrideMethodAttribute: false 26 | inferPrivatePropertyTypeFromConstructor: true 27 | tipsOfTheDay: false 28 | checkMissingCallableSignature: true 29 | -------------------------------------------------------------------------------- /src/Internal/CacheReader.php: -------------------------------------------------------------------------------- 1 | 20 | */ 21 | private array $paths = []; 22 | 23 | private string $pathname; 24 | 25 | public function __construct(string $pathname) 26 | { 27 | $this->pathname = $pathname; 28 | } 29 | 30 | private function exec(): string 31 | { 32 | if (!\is_file($this->pathname)) { 33 | return ''; 34 | } 35 | 36 | if (!\function_exists('\\shell_exec')) { 37 | return ''; 38 | } 39 | 40 | /** @psalm-suppress ForbiddenCode */ 41 | $output = @\shell_exec(\sprintf(self::EXEC_CMD, \escapeshellarg($this->pathname))); 42 | 43 | if (!\is_string($output)) { 44 | return ''; 45 | } 46 | 47 | return $output; 48 | } 49 | 50 | /** 51 | * @return array