FOSUserBundle icon indicating copy to clipboard operation
FOSUserBundle copied to clipboard

[RFC] Compatibility with Symfony Flex

Open lemoinem opened this issue 8 years ago • 13 comments

Symfony FOSUserBundle versions: dev-master

Description of the problem including expected versus actual behavior: When trying to install FOSUB in a Symfony Flex project, the assets:install commands fails, which prevents the bundle from being installed (see log).

I'm not sure what the best solution would be here... As far as I could see, there are no current solutions for interactive configuration via Flex's bundle autoconfiguration system. However, most project rely on the doctrine ORM to store users, AFAIK.

Maybe a solution could be to provide a "dummy" driver that allows the default configuration to go through but prevents actually using the bundle until an actual driver has been configured? What do you think?

Steps to reproduce:

  1. Start a Symfony project using Symfony Flex
  2. composer require composer require friendsofsymfony/user-bundle

Provide logs (if relevant):

mlemoine@local ~/project $ composer require friendsofsymfony/user-bundle
Using version ^2.0@dev for friendsofsymfony/user-bundle                              
./composer.json has been updated                                                            
Loading composer repositories with package information                               
Updating dependencies (including require-dev)                                        
Package operations: 6 installs, 0 updates, 0 removals
  - Installing symfony/templating (3.4.x-dev f333054): Downloading (100%)
  - Installing symfony/options-resolver (3.4.x-dev c6db86e): Downloading (100%)
  - Installing symfony/intl (3.4.x-dev fbdc2d8): Downloading (100%)
  - Installing symfony/polyfill-intl-icu (dev-master d50e463): Downloading (100%)
  - Installing symfony/form (3.4.x-dev f44a4a0): Downloading (100%)    
  - Installing friendsofsymfony/user-bundle (dev-master 039b826): Downloading (100%)
    Detected auto-configuration settings for "friendsofsymfony/user-bundle"                                                                                                                       
    Enabling the package as a Symfony bundle                                                                                                                                                       
Writing lock file                                                                                                                                                                                     
Generating autoload files                                                                                                                                                                                    
Executing script make cache-warmup [OK]                                                                                                                                                           
Executing script assets:install --symlink --relative %WEB_DIR% [KO]                                                                                                                               
 [KO]                                                                                                                                                                                                  
Script assets:install --symlink --relative %WEB_DIR% returned with error code 1                                                                                                                   
!!                                                                                                                                                                              
!!                                                     
!!                                                                                   
!!                                                 
!!    [Symfony\Component\Config\Definition\Exception\InvalidConfigurationException]  
!!                                                           
!!    The child node "db_driver" at path "fos_user" must be configured.              
!!                                                                                   
!!                                                                                                      
!!                                                             
!!                                              
!!                                                 
                                                              
Installation failed, reverting ./composer.json to its original content.

lemoinem avatar May 22 '17 16:05 lemoinem

Steps required to be able to install FOSUserBundle in a Symfony Flex project:

  • Install symfony/swiftmailer-bundle
  • Add the following configuration file in etc/packages/foo.yaml:
framework:
    templating: { engines: [twig] }

fos_user:
    db_driver: orm
    firewall_name: main
    user_class: FOS\UserBundle\Model\User
    from_email: {address: "fosub@localhost", sender_name: FOSUB}
    use_flash_notifications: false

What I would recommend:

  1. Provide a dummy Database driver (custom won't cut it as it doesn't work out-of-the-box)
  2. Add symfony/swiftmailer-bundle as a required dependency
  3. Add a Symfony Flex recipe providing a default configuration working out-of-the-box

What do you think? I'm ready to start opening PRs if you think this is the way to go!

lemoinem avatar May 22 '17 18:05 lemoinem

I also had to enable the "session" service

Inside config/packages/framework.yaml

framework:
    session:
        handler_id: session.handler.native_file
        save_path: '%kernel.project_dir%/var/sessions/%kernel.environment%'
    templating:
      engines: ['twig']

and had to create config/packages/fos_user.yaml with:

fos_user:
  db_driver: orm
  firewall_name: main
  user_class: App\Entity\User
  from_email:
      address: "[email protected]"
      sender_name: "Sender Name"

enlego avatar Jul 29 '17 22:07 enlego

As we can find here, we have to define the mailer service.

On freshly installed SF3.3 with Flex i had to set:

fos_user:
    db_driver: orm # other valid values are 'mongodb' and 'couchdb'
    firewall_name: main
    user_class: App\Entity\User
    from_email:
        address: '%env(MAILER_USER_ADDRESS)%'
        sender_name: '%env(MAILER_USER_NAME)%'
    service:
        mailer: 'fos_user.mailer.noop'
        # mailer: 'fos_user.mailer.twig_swift' # Works as well

worked for me, I hope this will help you.

allemas avatar Oct 02 '17 15:10 allemas

I don't want to add SwiftmailerBundle as a required dependency, as this would hurt anyone not using it (and Swiftmailer is not the only option to send emails, with the rise of tools like Sendgrid or Mandrill doing all the heavy work). This must stay an optional dependency.

However, I do want to improve the setup with Flex. I just haven't had time to do it yet.

stof avatar Oct 02 '17 16:10 stof

Thanks @allemas It works for me

allucardster avatar Oct 13 '17 03:10 allucardster

+1

stayeronglass avatar Oct 27 '17 11:10 stayeronglass

Hello,

I am struggling to get this package working with Flex. Finding this issue I assume that it is currently not possible. As such I wont proceed yet until there is some new documentation for this that takes into consideration the new config directory and the service autowiring.

Is there any update?

tip2tail avatar Nov 03 '17 20:11 tip2tail

@tip2tail I am using this following @allemas indications. You just have to create the configuration yaml before doing the composer require.

If using swiftmailer then you have to require it before fos-user.

jsampedro77 avatar Nov 24 '17 09:11 jsampedro77

May be fos_user.mailer.twig_swift should be a default fosub mailer service instead of fos_user.mailer.default? Then templating service won't be used any more by default.

See FOS\UserBundle\DependencyInjection\Configuration::addServiceSection

covex-nn avatar Dec 28 '17 11:12 covex-nn

I don't want to add SwiftmailerBundle as a required dependency, as this would hurt anyone not using it (and Swiftmailer is not the only option to send emails, with the rise of tools like Sendgrid or Mandrill doing all the heavy work). This must stay an optional dependency.

@stof The dependency is optional, but the FOS mailer seems to require a Swift_Mailer instance according to PHPDoc:

/**
 * Mailer constructor.
 *
 * @param \Swift_Mailer         $mailer
 * @param UrlGeneratorInterface $router
 * @param EngineInterface       $templating
 * @param array                 $parameters
 */
public function __construct($mailer, UrlGeneratorInterface  $router, EngineInterface $templating, array $parameters)
{
    $this->mailer = $mailer;
    $this->router = $router;
    $this->templating = $templating;
    $this->parameters = $parameters;
}

soullivaneuh avatar Jan 08 '18 15:01 soullivaneuh

Forget my last comment, I read the code too fast.

soullivaneuh avatar Jan 08 '18 15:01 soullivaneuh

Any news on this ?

leup avatar May 17 '18 08:05 leup

Hi. Any news?

vladdnepr avatar Jul 16 '19 12:07 vladdnepr