Skip to content

Instantly share code, notes, and snippets.

@schmittjoh
Created October 27, 2011 20:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save schmittjoh/b493493ecdb22c21590e to your computer and use it in GitHub Desktop.
Save schmittjoh/b493493ecdb22c21590e to your computer and use it in GitHub Desktop.
class ClassUtils
{
const MARKER = '__CG__';
public static function getUserClassAlternative1($class)
{
if (false === $pos = strrpos($class, self::MARKER)) {
return $class;
}
return substr($class, 0, $pos);
}
public static function getUserClassAlternative2($class)
{
if (false === strpos($class, self::MARKER)) {
return $class;
}
return get_parent_class($class);
}
public static function getUserClassAlternative3($class)
{
if (false === $pos = strrpos($class, '\\'.self::MARKER.'\\')) {
return $class;
}
return substr($class, $pos + strlen(self::MARKER) + 2);
}
}
Alternative 1:
- works over several levels, e.g. a proxy of a proxy, A (proxy) inherits from B (proxy) inherits from C (user class)
- requires the proxy to use a suffix
- autoloaders must be silent, or the proxy autoloader must be registered first
- user may not configure proxy namespace
Alternative 2:
- does only work for one level
- does not require to use a suffix, but would also allow a different namespace DoctrineProxy/FooName
- allows autoloader to be non-silent assuming that you use a unique namespace for the proxy
- user may still configure proxy namespace
Alternative 3:
- works over several levels
- requires \__CG__\ directly after the namespace prefix
- allows autoloader to be non-silent
- user may still configure proxy namespace
@beberlei
Copy link

How about making the convention to prefix with exactly one namespace?

MyProjec\User
DoctrineORM__CG__\MyProject\User
Security__CG__\DoctrineORM__CG__\MyProject\User

@beberlei
Copy link

oh wait thats not enough for sane autoloading.

@schmittjoh
Copy link
Author

No, that should work better with autoloading as we are doing that on a prefix basis.

We could also keep the feature to let the user specify the proxy namespace, but simply append a __CG__.

e.g.

MyProject\User
DoctrineORM\__CG__\MyProject\User

then the implementation would change to

public static function getUserClass($class)
{
    if (false === $pos = strrpos($class, '\\__CG__\\')) {
        return $class;
    }

    return substr($class, $pos + 8);
}

@beberlei
Copy link

How would proxies of proxies work with this approach?

@schmittjoh
Copy link
Author

Using strrpos, we would take everything after the last occurrence of \__CG__\ as the user's class name. So you could have any number of proxies for a given class, we wouldn't impose anything on the naming of these proxies except that \__CG__\ must be directly before the user class part starts.

@schmittjoh
Copy link
Author

ping @beberlei, does (3) look like an actionable plan?

@beberlei
Copy link

beberlei commented Dec 1, 2011

@beberlei
Copy link

beberlei commented Dec 3, 2011

@schmittjoh appending CG doesn't help if people define proxy namespaces to be equal to existing namespaces. Should we ignore this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment