PHP-DI 按名称注入参数

指挥聪明

我正在使用 php-di 和 Doctrine。要使用 Doctrine,有一个bootstrap.php构建$entityManager对象文件$entityManager对象是在该文件中全局定义的,因此要在我的类中使用它,我必须注入它。

例如假设下面的类:

<?php
interface IAccountService{
    function login(string $username, string $password);
}
class AccountService implements IAccountService {

    private $entityManager;

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

    public function login(string $email, string $password){
        $q = $this->entityManager->createQueryBuilder()
            ->select('us.id, us.name, us.email, us.passwordHashed')
            ->from('User', 'us')
            ->where('us.email = ?1 AND us.passwordHashed = ?2')
            ->setMaxResults( '1' )
            ->setParameter(1,$email)
            ->setParameter(2, HASHHELPER::hashPasswordSHA512($password, $email))
            ->getQuery();

        // echo $q->getSql();

        $users = $q->getResult();

        // print_r($users);

        if(!empty($users) && count($users) > 0){
            $_SESSION["USER"] = $users[0];
            return true;
        }
        else{
            return false;
        }
    }
}
?>

但是类型$entityManager没有很好地定义,或者当我调用echo gettype($entityManager);它时打印"object"结果。所以我想我需要通过它的名字而不是它的类型来注入这个参数。我的意思是这样的:

$container->set('$entityManager', $entityManager);

但这不起作用。什么是解决方案和最好的方法?

指挥聪明

面对这个显示命名空间和类名的链接后问题已经解决$entityManager但根据变量名注入的问题仍然存在。至此,我的新源代码是这样的:

账户服务.php

<?php
interface IAccountService{
    function login(string $username, string $password);
}
class AccountService implements IAccountService {

    private $entityManager;

    public function __construct(Doctrine\ORM\EntityManagerInterface $entityManager) {
        $this->entityManager = $entityManager;
    }

    public function login(string $email, string $password){
        $q = $this->entityManager->createQueryBuilder()
            ->select('us.id, us.name, us.email, us.passwordHashed')
            ->from('User', 'us')
            ->where('us.email = ?1 AND us.passwordHashed = ?2')
            ->setMaxResults( '1' )
            ->setParameter(1,$email)
            ->setParameter(2, HASHHELPER::hashPasswordSHA512($password, $email))
            ->getQuery();

        // echo $q->getSql();

        $users = $q->getResult();

        // print_r($users);

        if(!empty($users) && count($users) > 0){
            $_SESSION["USER"] = $users[0];
            return true;
        }
        else{
            return false;
        }
    }
}
?>

路由文件

<?php
  spl_autoload_register(function ($class_name) {
    switch ($class_name){
      case 'AccountController':
        require_once 'controllers/account_controller.php';
        break;
      case 'AccountService':
      case 'IAccountService':
        require_once 'services/account_service.php';
        break;
      case 'URLHELPER':
        require_once 'helpers/URLHELPER.php';
        break;
      case 'STRINGHELPER':
        require_once 'helpers/STRINGHELPER.php';
        break;
      case 'HASHHELPER':
        require_once "helpers/HASHHELPER.php";
        break;
      case 'User':
        require_once "models/entities/user.php";
        break;
    }
  });

  function call($controller, $action) {
    global $entityManager;
    $container = DI\ContainerBuilder::buildDevContainer();
    $container->set('IAccountService', \DI\object('AccountService'));
    $container->set('Doctrine\ORM\EntityManagerInterface', $entityManager);

    // require the file that matches the controller name
    require_once('controllers/' . $controller . '_controller.php');

    // create a new instance of the needed controller
    switch($controller) {
      case 'home':
        $controller = $container->get('HomeController');
        break;
      case 'account':
        $controller = $container->get('AccountController');
        break;
      default:
        $controller = 'home';
        $action = 'index';
    }

    // call the action
    $controller->{ $action }();
  }

  // just a list of the controllers we have and their actions
  // we consider those "allowed" values
  $controllers = array(
    'home' => ['index', 'error']
    ,'account' => ['login']
  );

  // check that the requested controller and action are both allowed
  // if someone tries to access something else he will be redirected to the error action of the pages controller
  if (array_key_exists($controller, $controllers)) {
    if (in_array($action, $controllers[$controller])) {
      call($controller, $action);
    } else {
      call('home', 'error');
    }
  } else {
    call('home', 'error');
  }
?>

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章