从Command Symfony将存储库传递给类

时间:2017-07-18 11:12:29

标签: php symfony

我在Symfony 3.2中有一个代码,我试图将存储库从Commmand类传递到我的自定义类,我正在运行websockets

<?php
namespace MyBundle\Command;

use MyBundle\Server\Notification;
use Ratchet\Http\HttpServer;
use Ratchet\Server\IoServer;
use Ratchet\WebSocket\WsServer;
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

class ServerCommand extends ContainerAwareCommand
{

    /**
     * Configure a new Command Line
     */
    protected function configure()
    {
        $this->setName('Project:notification:server')
            ->setDescription('Start the notification server.');
    }

    protected function execute(InputInterface $input, OutputInterface $output)
    {
        $em = $this->getApplication()->getKernel()->getContainer()->get('My.repository.car');‏

        $server = IoServer::factory(new HttpServer(
            new WsServer(
                new Notification($this->getContainer(),$em)
            )
        ), 8080);

        $server->run();
    }

}

对于我的所有数据库查询我使用的是存储库而且没有实体。我以前做过这个,它工作正常。 但现在当我从终端运行命令时,我收到此错误

  

解析错误:解析错误   /Applications/XAMPP/xamppfiles/htdocs/lavtaxi/src/MyBundle/Command/ServerCommand.php   在第28行

第28行

  

&#34; $ server = IoServer :: factory(新的HttpServer(&#34; part

这是我的services.yml

services:
    My.repository.car:
        class: MyBundle\Repository\CarRepository
        arguments: ["@doctrine.orm.entity_manager"]

这是Notification.php,我试图获取数据并通过套接字发送

<?php
namespace MyBundle\Server;

use Doctrine\ORM\EntityManager;
use Ratchet\ConnectionInterface;
use Ratchet\MessageComponentInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;

class Notification implements MessageComponentInterface
{
    protected $connections = array();
    private $users = array();
    protected $container;
    private $drivers = array();
    private $repository = null;

    public function __construct(ContainerInterface $container, $em)
    {
        $this->repository = $em;
        $this->container = $container;
        $this->users = [];
        $this->drivers = [];
    }

    /**
     * A new websocket connection
     *
     * @param ConnectionInterface $conn
     */
    public function onOpen(ConnectionInterface $conn)
    {
        $this->connections[] = $conn;
        $this->users[$conn->resourceId] = $conn;
        $this->drivers = $this->repository->getCarsData();
        $conn->send('..:: Hello from the Notification Center ::..');
        $conn->send(json_encode($this->drivers));
        echo "New connection \n";
        }
}

当我删除上面的存储库调用部分时,错误消失了,所以我知道问题不在IoServer上

0 个答案:

没有答案