Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[Dependency Injection - Profiler] Wrong exception throwed when a service class doesn't exist #15297

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Alsatian67 opened this issue Jul 16, 2015 · 11 comments

Comments

@Alsatian67
Copy link

I had an error in a yaml service container.
The class was wrong.

But the error that occurs is "cannot redeclare class ..." instead of a not found exception.

@Alsatian67 Alsatian67 changed the title [Dependency Injection - Profiler] False exception throwed when a service class doesn't exist [Dependency Injection - Profiler] Wrong exception throwed when a service class doesn't exist Jul 16, 2015
@weaverryan weaverryan added the Bug label Jul 16, 2015
@weaverryan
Copy link
Member

@Alsatian67 can you post some code that can be used to reproduce the bad exception?

@Alsatian67
Copy link
Author

Sorry, the problem occurs when the namespace is wrong in the class !

example :

<?php
// Real location : AppBundle\Security
namespace AppBundle\Security\Voters; // <- Here the namespace is wrong.

use Symfony\Component\Security\Core\Authorization\Voter\AbstractVoter;
use Symfony\Component\Security\Core\User\UserInterface;

class AdminMembersVoter extends AbstractVoter
{
    const ADMIN_MEMBER_EDIT = 'admin_member_edit';

    protected function getSupportedAttributes()
    {
        return array(ADMIN_MEMBER_EDIT);
    }

    protected function getSupportedClasses()
    {
        return array('AppBundle\Entity\Members');
    }

    protected function isGranted($attribute,$member,$user=null)
    {
        if (!$user instanceof UserInterface) {
            return false;
        }

        switch($attribute) {
            case self::ADMIN_MEMBER_EDIT:
                if($user === $member){return false;}
                $rolesMember = $member->getRoles();
                $rolesUser = $user->getRoles();
                if(in_array('ROLE_SUPER_ADMIN',$rolesMember)){return false;}
                if(in_array('ROLE_ADMIN',$rolesMember) && !in_array('ROLE_SUPER_ADMIN',$rolesUser)){return false;}
                if(in_array('ROLE_ADMIN',$rolesUser)){return true;}

                break;
        }

        return false;
    }
}

With that services.yml :

services:
    voters.admin_member:
        class:      AppBundle\Security\AdminMembersVoter
        public:     false
        tags:
           - { name: security.voter }

Not a big bug since my class was wrong, but the exception made me search for an eventual double ...

@qferr
Copy link

qferr commented Jul 17, 2015

You must respected PSR-4:

The contiguous sub-namespace names after the "namespace prefix" correspond to a subdirectory within a "base directory", in which the namespace separators represent directory separators. The subdirectory name MUST match the case of the sub-namespace names.

So your namespace should be AppBundle\Security whether the file location is AppBundle\Security or moves your file in the new directory named Voters in AppBundle\Security.

@stof
Copy link
Member

stof commented Jul 17, 2015

@Alsatian67 the issue will happen because the class loader will load the file, and then try again the next time it is asked to load the class, as the first load will not have defined the class.

And this case is not one which is detected by the DebugClassLoader /cc @nicolas-grekas

@Alsatian67
Copy link
Author

Ok it would be nice if the autoloader throws an exception :

The autoloader expected class "AppBundle\Security\AdminMembersVoter" to be defined in file AppBundle\Security\AdminMembersVoter.php". The file was found but ...

When it load the file for the first time.

@stof
Copy link
Member

stof commented Jul 17, 2015

Well, actually, it should.

which Symfony version are you using ?

@stof
Copy link
Member

stof commented Jul 17, 2015

and do you have the debug class loader enabled ? (i.e. to you have the Debug::enable() call in your front controller as in https://github.com/symfony/symfony-standard/blob/v2.7.2/web/app_dev.php#L21 ?)

@Alsatian67
Copy link
Author

I'm using Sf 2.7.2 and the debug is enabled.

I just have seen that this wrong file produce the same invalid exception (here the class name is not like the file name [missing s]) :

<?php
// src/AppBundle/Entity/Partners/Comments.php
namespace AppBundle\Entity\Partners;

use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
use Gedmo\Mapping\Annotation as Gedmo;

use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;

/**
 * Comment
 *
 * @ORM\Table
 * @ORM\Entity
 */
class Comment
{
...
}

@linaori
Copy link
Contributor

linaori commented Jul 19, 2015

I can confirm this issue is real as I experienced it myself, sadly don't remember which version nor the code that triggered it.

@trq
Copy link

trq commented Sep 2, 2015

I can confirm a FatalErrorException

FatalErrorException in AdminMembersVoter.php line 42:
Compile Error: Cannot redeclare class AppBundle\Security\Voters\AdminMembersVoter

Reproduced here.

Status: Reviewed

@nicolas-grekas
Copy link
Member

See #16651
The DebugClassLoader IS able to detect such mistakes, but the exception that is thrown is caught and a class loading happens again, boom. Fixed by using require_once instead of require.

nicolas-grekas added a commit that referenced this issue Nov 24, 2015
…colas-grekas)

This PR was merged into the 2.7 branch.

Discussion
----------

[Debug] Ensure class declarations are loaded only once

| Q             | A
| ------------- | ---
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #15297
| License       | MIT
| Doc PR        | -

Commits
-------

01c08fc [Debug] Ensure class declarations are loaded only once
@xabbuh xabbuh closed this as completed Nov 24, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

9 participants