首页 \ 问答 \ 使用Composer Pusher Library命名空间(Namespacing with Composer Pusher Library)

使用Composer Pusher Library命名空间(Namespacing with Composer Pusher Library)

我已经使用composer安装了https://packagist.org/packages/pusher/pusher-php-server,并且我的应用程序中的所有软件包都是PSR-4自动加载。

我能够使用从没有提到“使用命名空间”的页面调用的基本代码成功连接到Pusher API:

$pusher = new Pusher(
            $app->config->get('pusher.api-key'), 
            $app->config->get('pusher.api-secret'),
            $app->config->get('pusher.app-id'),
            array('encrypted' => true)
        );

我决定在其自己的类中使用所有Pusher连接代码,因此我在其自己的命名空间中创建了下面的类,并将其保存为Smawt / Helpers目录中的PusherConnect.php:

<?php

namespace Smawt\Helpers;

class PusherConnect
{
    protected $app;

    public function __construct($app)
    {
        $this->app = $app;

        $pusher = new Pusher(
            $this->app->config->get('pusher.api-key'), 
            $this->app->config->get('pusher.api-secret'),
            $this->app->config->get('pusher.app-id'),
            array('encrypted' => true)
        );

        $results = $pusher->get_channels( array( 'filter_by_prefix' => 'user_') );
        $channel_count = count($results->channels);

        $this->app->view->appendData([
            'channels_count' => $channel_count
        ]);

        $pusher->channel_count = $channel_count;

        return $pusher;
    }

    public function check()
    {
        if (!$this->app->config->get('pusher.api-key')) {
            $this->app->flash('global', 'No Pusher account has been set up.');
            return $this->app->response->redirect($this->app->urlFor('home'));
        }
    }
}

然后,我用实例化对象

<?php

use Smawt\Helpers\PusherConnect;

$pusher = new PusherConnect($app);

但我遇到了

Fatal error: Class 'Smawt\Helpers\Pusher' not found in C:\Users\...\PusherConnect.php on line 13

所以,我意识到pusher-php-server类没有命名空间,因为Composer生成的autoload_psr4.php文件中没有提到Pusher命名空间。 所以,我编辑了供应商提供的Pusher.php文件:

namespace Pusher;

class PusherException extends \Exception
{
}

class Pusher
{ ...

和我自己的PusherConnect课程

public function __construct($app)
    {
        $this->app = $app;

        $pusher = new \Pusher\Pusher(

一切都开始了。

问题似乎与在我自己的类中实例化非命名空间的Pusher类有关,该类是命名空间的。 所以,我的问题是“我如何阻止Composer覆盖我的更改,下次我编写更新时,以及为什么我无法通过引用我自己的全局命名空间来避免编辑Composer安装的包类?” 例如在我自己的PusherConnect类中

public function __construct($app)
    {
        $this->app = $app;

        $pusher = new \Pusher(

顺便说一下,Pusher Library似乎一度被命名,但不再是: https//github.com/pusher/pusher-http-php/commit/fea385ade9aede97b37267d5be2fce59d0f1b09d

提前致谢


I have installed https://packagist.org/packages/pusher/pusher-php-server using composer and am PSR-4 autoloading all of the packages in my app.

I was able to successfully connect to Pusher API using basic code called from a page that had no "use namespace" mentioned:

$pusher = new Pusher(
            $app->config->get('pusher.api-key'), 
            $app->config->get('pusher.api-secret'),
            $app->config->get('pusher.app-id'),
            array('encrypted' => true)
        );

I decided that I wanted all of the Pusher connection code within its own class, so I created the class below within its own namespace, and saved it as PusherConnect.php in the Smawt/Helpers directory:

<?php

namespace Smawt\Helpers;

class PusherConnect
{
    protected $app;

    public function __construct($app)
    {
        $this->app = $app;

        $pusher = new Pusher(
            $this->app->config->get('pusher.api-key'), 
            $this->app->config->get('pusher.api-secret'),
            $this->app->config->get('pusher.app-id'),
            array('encrypted' => true)
        );

        $results = $pusher->get_channels( array( 'filter_by_prefix' => 'user_') );
        $channel_count = count($results->channels);

        $this->app->view->appendData([
            'channels_count' => $channel_count
        ]);

        $pusher->channel_count = $channel_count;

        return $pusher;
    }

    public function check()
    {
        if (!$this->app->config->get('pusher.api-key')) {
            $this->app->flash('global', 'No Pusher account has been set up.');
            return $this->app->response->redirect($this->app->urlFor('home'));
        }
    }
}

I then instantiated the object with

<?php

use Smawt\Helpers\PusherConnect;

$pusher = new PusherConnect($app);

but I was met with

Fatal error: Class 'Smawt\Helpers\Pusher' not found in C:\Users\...\PusherConnect.php on line 13

So, I realised that the pusher-php-server classes were not namespaced, as no Pusher namespaces were mentioned in the Composer generated autoload_psr4.php file. So, I edited the vendor supplied Pusher.php file:

namespace Pusher;

class PusherException extends \Exception
{
}

class Pusher
{ ...

and my own PusherConnect class

public function __construct($app)
    {
        $this->app = $app;

        $pusher = new \Pusher\Pusher(

and all started working.

The problem appears to be related to instantiating the non-namespaced Pusher class from within my own class, which is namespaced. So, my question is "How do I prevent Composer over-writing my changes, next time I composer update, and also, why am I not able to avoid this editing of the Composer installed package by referring to the global namespace from within my own class?" such as in my own PusherConnect class

public function __construct($app)
    {
        $this->app = $app;

        $pusher = new \Pusher(

Incidentally, it appears that the Pusher Library was namespaced at one time, but is no longer: https://github.com/pusher/pusher-http-php/commit/fea385ade9aede97b37267d5be2fce59d0f1b09d

Thanks in advance


原文:https://stackoverflow.com/questions/33368110
更新时间:2021-03-13 08:03

最满意答案

urlPatterns = {"/flow", "/low"}

urlPatterns = {"/flow", "/low"}

相关问答

更多
  • urlPatterns = {"/flow", "/low"} urlPatterns = {"/flow", "/low"}
  • 你遇到的最初的问题是你的类路径中有两个相互冲突的servlet api版本(不常见,因为该工件有六个奇怪的maven坐标,这使得maven或gradle难以解决冲突) 不要在项目中使用jetty-all,不要在项目中使用该工件。 查看过去对此的回复 。 您在WebSocket支持时遇到的问题应该作为stackoverflow上的单独问题提交。 询问有关您的websocket问题的具体问题,我会在那里回答。 The original problem you are having is that you had ...
  • 你必须重新思考你在做什么。 当您明确地将web.xml中的多个 DispatcherServlet映射到特定URL时,该映射是本地部分或URL的一部分。 都可以添加servlet上下文部分,但servlet前缀是webapplication的一部分 ,没有人会猜到它。 毕竟,你应该有一个从servlet 1服务的页面到servlet 2服务的页面的链接,所有这些仍然在同一个servlet上下文中。 恕我直言,你应该知道为什么你需要在一个Spring MVC应用程序中使 ...
  • 我宁愿只添加Servlet API作为依赖, 说实话,我不知道为什么,但不介意... Brabster的独立依赖已被Java EE 6 Profiles所取代。 有没有证据证明这一假设的来源? 来自Java.net的maven存储库确实为WebProfile提供了以下工件: java.net2 Repository hosting the jee6 artifacts h ...
  • 表单中的操作方法被定义为action="/login.action" 。 /使相对于您的域的网址。 你应该注意到表单发布到一个类似于http:// localhost:8080 / login.action的url,它应该是http:// localhost:8080 / [context root] / [sub dir if defined] /login.action 我重新考虑删除'/'会解决它。 The action method in your form was defined as actio ...
  • 错误的说:“请求的资源不可用”。 我最好的猜测是服务器找不到Servlet1类。 两个建议: @WebServlet("/Servlet1") servlet1servlet1不匹配,前者使用'S'而后者使用's'。 尝试使用相同的名称。 尝试将Servlet1类移动到包而不是将其放在默认包中。 The err says: "The requested resource is not ava ...
  • 没有这样的设施。 最好的办法是将JSP隐藏在/WEB-INF (以便它永远不会被URL直接请求),只需创建一个转发到该JSP并最终将其映射到所需URL模式的servlet。 这很容易: @WebServlet("/foo") public class FooServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) ...
  • 您是否使用正确的上下文作为URL中路径的第一部分? 无论何时部署应用程序,都需要指定标识所有URL的上下文。 在该上下文之后将应用任何url模式。 假设你有一个名为“MyShop”的上下文,然后使用你提供的web.xml,你应该调用http:// yourdomain / MyShop / MyServlet。 Are you using the correct context as the first part of your path in URL? Whenever you deploy an app ...
  • 是的,所以我使用网址修复它 /的GetFile /的GetFile / {documentId} 在我的客户端。 Right so I fixed it by using the url /getFile/getFile/{documentId} on my client side.
  • 我假设您以某种方式“管理”引用位于javaee-web-api之前的类路径中的旧servlet-api库(可能是可传递的)。 这意味着现有的类是从旧的servlet-api中获取的,而旧规范中不存在的类是从javaee-web-api加载的。 您可以运行mvn dependency:tree来查看引用过时库的位置,然后将其排除。 I assume you have somehow "managed" to reference an older servlet-api library (probably tr ...

相关文章

更多

最新问答

更多
  • 您如何使用git diff文件,并将其应用于同一存储库的副本的本地分支?(How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?)
  • 将长浮点值剪切为2个小数点并复制到字符数组(Cut Long Float Value to 2 decimal points and copy to Character Array)
  • OctoberCMS侧边栏不呈现(OctoberCMS Sidebar not rendering)
  • 页面加载后对象是否有资格进行垃圾回收?(Are objects eligible for garbage collection after the page loads?)
  • codeigniter中的语言不能按预期工作(language in codeigniter doesn' t work as expected)
  • 在计算机拍照在哪里进入
  • 使用cin.get()从c ++中的输入流中丢弃不需要的字符(Using cin.get() to discard unwanted characters from the input stream in c++)
  • No for循环将在for循环中运行。(No for loop will run inside for loop. Testing for primes)
  • 单页应用程序:页面重新加载(Single Page Application: page reload)
  • 在循环中选择具有相似模式的列名称(Selecting Column Name With Similar Pattern in a Loop)
  • System.StackOverflow错误(System.StackOverflow error)
  • KnockoutJS未在嵌套模板上应用beforeRemove和afterAdd(KnockoutJS not applying beforeRemove and afterAdd on nested templates)
  • 散列包括方法和/或嵌套属性(Hash include methods and/or nested attributes)
  • android - 如何避免使用Samsung RFS文件系统延迟/冻结?(android - how to avoid lag/freezes with Samsung RFS filesystem?)
  • TensorFlow:基于索引列表创建新张量(TensorFlow: Create a new tensor based on list of indices)
  • 企业安全培训的各项内容
  • 错误:RPC失败;(error: RPC failed; curl transfer closed with outstanding read data remaining)
  • C#类名中允许哪些字符?(What characters are allowed in C# class name?)
  • NumPy:将int64值存储在np.array中并使用dtype float64并将其转换回整数是否安全?(NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?)
  • 注销后如何隐藏导航portlet?(How to hide navigation portlet after logout?)
  • 将多个行和可变行移动到列(moving multiple and variable rows to columns)
  • 提交表单时忽略基础href,而不使用Javascript(ignore base href when submitting form, without using Javascript)
  • 对setOnInfoWindowClickListener的意图(Intent on setOnInfoWindowClickListener)
  • Angular $资源不会改变方法(Angular $resource doesn't change method)
  • 在Angular 5中不是一个函数(is not a function in Angular 5)
  • 如何配置Composite C1以将.m和桌面作为同一站点提供服务(How to configure Composite C1 to serve .m and desktop as the same site)
  • 不适用:悬停在悬停时:在元素之前[复制](Don't apply :hover when hovering on :before element [duplicate])
  • 常见的python rpc和cli接口(Common python rpc and cli interface)
  • Mysql DB单个字段匹配多个其他字段(Mysql DB single field matching to multiple other fields)
  • 产品页面上的Magento Up出售对齐问题(Magento Up sell alignment issue on the products page)