使用名称中带有连字符的作曲家脚本?

Using a composer script with a hyphen in the name?

我正在尝试使用来自 Github 的以下脚本:https://github.com/php-webdriver/php-webdriver

在“/mnt/hgfs/”中使用 composer 安装很容易,但在 php 文件中加载 class 似乎是不可能的

如您所见,名称中有一个连字符,我似乎无法以任何方式加载 class。我在谷歌上搜索了很多并尝试了很多东西,但同样的问题,要么我得到:

尝试在命名空间中使用连字符并使用 i get

PHP Parse error: syntax error, unexpected '-', expecting '{' in /mnt/hgfs/test.php on line 3

用下划线替换连字符,或者只是删除它我得到:

PHP Fatal error: Uncaught Error: Class 'php_webdriver\WebDriver\Remote\DesiredCapabilities' not found in /mnt/hgfs/test.php:10

我的代码是这样的 (/mnt/hgfs/test.php):

namespace php_webdriver\WebDriver;
require 'vendor/autoload.php';
use php_webdriver\WebDriver\Chrome\ChromeOptions;
use php_webdriver\WebDriver\Chrome\ChromeDriver;
use php_webdriver\WebDriver\Remote\DesiredCapabilities;
use php_webdriver\WebDriver\Remote\RemoteWebDriver;

$host = 'http://localhost:4444/wd/hub'; // this is the default
$capabilities = DesiredCapabilities::htmlUnitWithJS();
{
    $options = new ChromeOptions();
    $options->addArguments(array(
        '--disable-extensions',
        '--no-sandbox',
        '--headless',
        '--no-proxy-server'
    ));
    $capabilities = DesiredCapabilities::chrome();
    $capabilities->setCapability(ChromeOptions::CAPABILITY, $options);
    $capabilities->setPlatform("Linux");
}
$driver_spec = RemoteWebDriver::create($host, $capabilities, 600000, 600000);

我应该如何加载这个 class?

这里有一些错误:

namespace php_webdriver\WebDriver; 

您不应该尝试将您的代码添加到 webdriver 命名空间。对于测试脚本,您不需要自己的名称空间。你可以删除这一行。

至于:

require 'vendor/autoload.php';
use php_webdriver\WebDriver\Chrome\ChromeOptions;
use php_webdriver\WebDriver\Chrome\ChromeDriver;
use php_webdriver\WebDriver\Remote\DesiredCapabilities;
use php_webdriver\WebDriver\Remote\RemoteWebDriver;

我觉得您并不是 100% 熟悉 PSR-4 / 自动加载的工作原理。命名空间通过autoload.php映射到一个代码目录,两者不一定要有相同的命名结构。

看看composer.json in the webdriver project,注意PSR-4部分。

"Facebook\WebDriver\": "lib/" 告诉您 lib 目录中的任何内容都将被视为在 Facebook\WebDriver 命名空间中。

尝试

require 'vendor/autoload.php';
use Facebook\WebDriver\Chrome\ChromeOptions;
use Facebook\WebDriver\Chrome\ChromeDriver;
use Facebook\WebDriver\Remote\DesiredCapabilities;
use Facebook\WebDriver\Remote\RemoteWebDriver;