0
votes

I'm working on a RESTful application powered by Symfony 3, using FOSRestBundle, FOSUserBundle and FOSOAuthBundle.

My problem is I want to extend the FOS UserManager class, but this throws a fatal error :

Type error: Argument 1 passed to FOS\UserBundle\Doctrine\UserManager::__construct() must implement interface FOS\UserBundle\Util\PasswordUpdaterInterface, none given, called in /var/www/html/var/cache/dev/appDevDebugProjectContainer.php on line 2538

Here are parts of my application files:

# app/config/config.yml

fos_user:
    # [...]
    service:
        user_manager: foo.user_manager
# src/FooBundle/Resources/config/services.yml

services:
    # [...]
    foo.user_manager:
        class: FooBundle\Service\UserManager

    FooBundle\Service\UserManager:
        arguments:
            $class: '%fos_user.model.user.class%'
<?php

// src/FooBundle/Service/UserManager.php

namespace FooBundle\Service;

use FOS\UserBundle\Doctrine\UserManager as BaseUserManager;

class UserManager extends BaseUserManager implements UserManagerInterface
{
    /**
    * {@inheritDoc}
    * @see \FooBundle\Service\UserManagerInterface::findUsersAtPage()
    */
    public function findUsersAtPage($usersPerPage, $page) {
        $offset = $usersPerPage * $page;
        return $this->getRepository()->findBy(array(), null, $usersPerPage, $offset);
    }
}
<?php

// src/FooBundle/Service/UserManagerInterface.php

namespace FooBundle\Service;

use FOS\UserBundle\Model\UserManagerInterface as BaseUserManagerInterface;

interface UserManagerInterface extends BaseUserManagerInterface
{
    /**
     * Returns a collection with specified number of user instances at specified page.
     * @param int $usersPerPage The number of users per page.
     * @param int $page The page number.
     * @return \Traversable
     */
    public function findUsersAtPage($usersPerPage, $page);
}

The thing I observed is that when I use the default FOS user manager, the appDevDebugProjectContainer.php generates the getter method (with parameters) :

<?php

// var/cache/dev/appDevDebugProjectContainer.php

class appDevDebugProjectContainer extends Container
{
    // [...]

    protected function getFosUser_UserManagerService()
    {
    return $this->services['fos_user.user_manager'] = new \FOS\UserBundle\Doctrine\UserManager(${($_ = isset($this->services['fos_user.util.password_updater']) ? $this->services['fos_user.util.password_updater'] : $this->getFosUser_Util_PasswordUpdaterService()) && false ?: '_'}, ${($_ = isset($this->services['fos_user.util.canonical_fields_updater']) ? $this->services['fos_user.util.canonical_fields_updater'] : $this->getFosUser_Util_CanonicalFieldsUpdaterService()) && false ?: '_'}, ${($_ = isset($this->services['fos_user.object_manager']) ? $this->services['fos_user.object_manager'] : $this->getFosUser_ObjectManagerService()) && false ?: '_'}, 'FooBundle\\Entity\\User');
    }

    // [...]
}

and when I use mine (without parameters) :

<?php

// var/cache/dev/appDevDebugProjectContainer.php

class appDevDebugProjectContainer extends Container
{
    // [...]

    protected function getFosUser_UserManagerService()
    {
        return $this->services['fos_user.user_manager'] = new \FooBundle\Service\UserManager();
    }

    // [...]
}

Please, can someone help me ?

2

2 Answers

1
votes

Ok guys that's the answer:

The service arguments had to be set in the service definition, not the class service definition. That's not really clear, so better than word, some code :

# src/FooBundle/Resources/config/services.yml

services:
    # [...]
    foo.user_manager:
        class: FooBundle\Service\UserManager
        arguments:
            $class: '%fos_user.model.user.class%'

    FooBundle\Service\UserManager: '@foo.user_manager'

I was thinking I tried it, but I didn't...

1
votes

Just resolved the similar problem. Maybe will be helpful for someone. Check this answer

Or just try this one out

services:
    you_bundle.user_manager:
        class: YourBundle\Services\UserManager
        arguments: ["@fos_user.util.password_updater", "@fos_user.util.canonical_fields_updater", "@doctrine.orm.entity_manager", "%fos_user.model.user.class%"]