-
-
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 |
oh wait thats not enough for sane autoloading.
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);
}
How would proxies of proxies work with this approach?
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.
ping @beberlei, does (3) look like an actionable plan?
@schmittjoh yes
@schmittjoh appending CG doesn't help if people define proxy namespaces to be equal to existing namespaces. Should we ignore this?
How about making the convention to prefix with exactly one namespace?