【问题标题】:Using a composer script with a hyphen in the name?使用名称中带有连字符的作曲家脚本?
【发布时间】:2020-05-04 11:07:40
【问题描述】:

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

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

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

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

PHP 解析错误:语法错误,意外 '-',期待 '{' in /mnt/hgfs/test.php 在第 3 行

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

PHP 致命错误:未捕获的错误:类 'php_webdriver\WebDriver\Remote\DesiredCapabilities' 未在 /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);

我应该如何加载这个类?

【问题讨论】:

  • 连字符是无效的命名空间字符。他们的example 似乎没有使用带有连字符的路径。您是否可以删除它并将其重新安装到目录/命名空间结构中?

标签: php composer-php


【解决方案1】:

这里有几个问题:

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;

我觉得您对 PSR-4 / 自动加载的工作原理并不是 100% 熟悉。命名空间通过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;

【讨论】:

  • 谢谢,现在上面的代码可以工作了。但是现在在生产中我需要这个命名空间,因为我得到:PHP致命错误:未捕获的错误:在/mnt/hgfs/test.php:861中找不到类'WebDriverBy',如果我将其设置为使用状态(我正在使用Facebook /Webdriver 之前,但是作曲家抱怨beeing被放弃了,我应该改用php-webdriver)有什么建议吗?
  • WebDriverBy 类存在 (github.com/php-webdriver/php-webdriver/blob/community/lib/…),但您似乎没有在测试脚本的顶部导入它。在test.php 顶部添加use Facebook\WebDriver\WebDriverBy;
  • 再次感谢您,但现在下一个是“PHP 致命错误:未捕获的错误:在 /mnt/hgfs/test.php:893 中找不到类 'WebDriverExpectedCondition'”这就是我需要命名空间的原因.在他们将名称从 facebook 更改为 php-webdriver 之前,我只坐在 Namespace 和问题中的那 4 个用户,它自己找到了这些类
  • 如果你想从给定的命名空间导入所有类,你可以just import the namespace。例如,如果你想在一个命名空间中指定 some 个类,该命名空间包含许多使用单个 import 语句的类(例如,你需要类 A、B、H 和 H,但不需要 C、D、E , 或 F), 你可以做use Whatever\Namespace\{ClassA, ClassB, ClassG, ClassH};
  • 当名称中有连字符时,我只是不知道如何导入名称空间,但在您的帮助下,我尝试使用“Facebook\WebDriver”作为名称空间,并且不再缺少任何类,谢谢非常感谢您的帮助和时间!
猜你喜欢
  • 1970-01-01
  • 2015-08-22
  • 2016-06-24
  • 1970-01-01
  • 2018-07-08
  • 2016-06-06
  • 1970-01-01
  • 2016-03-26
  • 1970-01-01
相关资源
最近更新 更多