-
-
Notifications
You must be signed in to change notification settings - Fork 294
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
[DCOM-96] ProxyFactory logic moved to doctrine common #168
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,3 +2,4 @@ build/ | |
logs/ | ||
reports/ | ||
dist/ | ||
tests/Doctrine/Tests/Common/Proxy/generated/ |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,7 @@ env: | |
- OPCODE_CACHE=apc | ||
|
||
php: | ||
- 5.3.3 | ||
- 5.3 | ||
- 5.4 | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
<?php | ||
/* | ||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | ||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | ||
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | ||
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | ||
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | ||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | ||
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | ||
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | ||
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | ||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | ||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
* | ||
* This software consists of voluntary contributions made by many individuals | ||
* and is licensed under the MIT license. For more information, see | ||
* <http://www.doctrine-project.org>. | ||
*/ | ||
|
||
namespace Doctrine\Common\Proxy; | ||
|
||
use Doctrine\Common\Proxy\Exception\InvalidArgumentException; | ||
|
||
/** | ||
* Special Autoloader for Proxy classes, which are not PSR-0 compliant. | ||
* | ||
* @author Benjamin Eberlei <kontakt@beberlei.de> | ||
*/ | ||
class Autoloader | ||
{ | ||
/** | ||
* Resolves proxy class name to a filename based on the following pattern. | ||
* | ||
* 1. Remove Proxy namespace from class name | ||
* 2. Remove namespace separators from remaining class name. | ||
* 3. Return PHP filename from proxy-dir with the result from 2. | ||
* | ||
* @param string $proxyDir | ||
* @param string $proxyNamespace | ||
* @param string $className | ||
* | ||
* @return string | ||
* | ||
* @throws InvalidArgumentException | ||
*/ | ||
public static function resolveFile($proxyDir, $proxyNamespace, $className) | ||
{ | ||
if (0 !== strpos($className, $proxyNamespace)) { | ||
throw InvalidArgumentException::notProxyClass($className, $proxyNamespace); | ||
} | ||
|
||
$className = str_replace('\\', '', substr($className, strlen($proxyNamespace) + 1)); | ||
|
||
return $proxyDir . DIRECTORY_SEPARATOR . $className . '.php'; | ||
} | ||
|
||
/** | ||
* Register and return autoloader callback for the given proxy dir and | ||
* namespace. | ||
* | ||
* @param string $proxyDir | ||
* @param string $proxyNamespace | ||
* @param callable $notFoundCallback Invoked when the proxy file is not found. | ||
* | ||
* @return \Closure | ||
* | ||
* @throws InvalidArgumentException | ||
*/ | ||
public static function register($proxyDir, $proxyNamespace, $notFoundCallback = null) | ||
{ | ||
$proxyNamespace = ltrim($proxyNamespace, '\\'); | ||
|
||
if ( ! (null === $notFoundCallback || is_callable($notFoundCallback))) { | ||
throw InvalidArgumentException::invalidClassNotFoundCallback($notFoundCallback); | ||
} | ||
|
||
$autoloader = function ($className) use ($proxyDir, $proxyNamespace, $notFoundCallback) { | ||
if (0 === strpos($className, $proxyNamespace)) { | ||
$file = Autoloader::resolveFile($proxyDir, $proxyNamespace, $className); | ||
|
||
if ($notFoundCallback && ! file_exists($file)) { | ||
call_user_func($notFoundCallback, $proxyDir, $proxyNamespace, $className); | ||
} | ||
|
||
require $file; | ||
} | ||
}; | ||
|
||
spl_autoload_register($autoloader); | ||
|
||
return $autoloader; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
<?php | ||
/* | ||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | ||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | ||
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | ||
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | ||
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | ||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | ||
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | ||
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | ||
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | ||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | ||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
* | ||
* This software consists of voluntary contributions made by many individuals | ||
* and is licensed under the MIT license. For more information, see | ||
* <http://www.doctrine-project.org>. | ||
*/ | ||
|
||
namespace Doctrine\Common\Proxy\Exception; | ||
|
||
use InvalidArgumentException as BaseInvalidArgumentException; | ||
|
||
/** | ||
* Proxy Invalid Argument Exception | ||
* | ||
* @link www.doctrine-project.com | ||
* @since 2.4 | ||
* @author Marco Pivetta <ocramius@gmail.com> | ||
*/ | ||
class InvalidArgumentException extends BaseInvalidArgumentException implements ProxyException | ||
{ | ||
/** | ||
* @return self | ||
*/ | ||
public static function proxyDirectoryRequired() | ||
{ | ||
return new self('You must configure a proxy directory. See docs for details'); | ||
} | ||
|
||
/** | ||
* @param string $className | ||
* @param string $proxyNamespace | ||
* | ||
* @return self | ||
*/ | ||
public static function notProxyClass($className, $proxyNamespace) | ||
{ | ||
return new self(sprintf('The class "%s" is not part of the proxy namespace "%s"', $className, $proxyNamespace)); | ||
} | ||
|
||
/** | ||
* @param string $name | ||
* | ||
* @return self | ||
*/ | ||
public static function invalidPlaceholder($name) | ||
{ | ||
return new self(sprintf('Provided placeholder for "%s" must be either a string or a valid callable', $name)); | ||
} | ||
|
||
/** | ||
* @return self | ||
*/ | ||
public static function proxyNamespaceRequired() | ||
{ | ||
return new self('You must configure a proxy namespace'); | ||
} | ||
|
||
/** | ||
* @param mixed $callback | ||
* | ||
* @return self | ||
*/ | ||
public static function invalidClassNotFoundCallback($callback) | ||
{ | ||
$type = is_object($callback) ? get_class($callback) : gettype($callback); | ||
|
||
return new self(sprintf('Invalid \$notFoundCallback given: must be a callable, "%s" given', $type)); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<?php | ||
/* | ||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | ||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | ||
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | ||
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | ||
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | ||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | ||
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | ||
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | ||
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | ||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | ||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
* | ||
* This software consists of voluntary contributions made by many individuals | ||
* and is licensed under the MIT license. For more information, see | ||
* <http://www.doctrine-project.org>. | ||
*/ | ||
|
||
namespace Doctrine\Common\Proxy\Exception; | ||
|
||
/** | ||
* Base exception interface for proxy exceptions | ||
* | ||
* @link www.doctrine-project.com | ||
* @since 2.4 | ||
* @author Marco Pivetta <ocramius@gmail.com> | ||
*/ | ||
interface ProxyException | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why don't we call this class ProxyExceptionInterface ? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I agree, but the other interfaces are not called like that in doctrine2. This may be a good change for 3.0 (along with all other interfaces) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We do not suffix with Interface because we do not describe data structure type on class name. It doesn't make sense. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @guilhermeblanco I'd push this to |
||
{ | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
<?php | ||
/* | ||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | ||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | ||
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | ||
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | ||
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | ||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | ||
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | ||
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | ||
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | ||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | ||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
* | ||
* This software consists of voluntary contributions made by many individuals | ||
* and is licensed under the MIT license. For more information, see | ||
* <http://www.doctrine-project.org>. | ||
*/ | ||
|
||
namespace Doctrine\Common\Proxy\Exception; | ||
|
||
use UnexpectedValueException as BaseUnexpectedValueException; | ||
|
||
/** | ||
* Proxy Unexpected Value Exception | ||
* | ||
* @link www.doctrine-project.com | ||
* @since 2.4 | ||
* @author Marco Pivetta <ocramius@gmail.com> | ||
*/ | ||
class UnexpectedValueException extends BaseUnexpectedValueException implements ProxyException | ||
{ | ||
/** | ||
* @return self | ||
*/ | ||
public static function proxyDirectoryNotWritable() | ||
{ | ||
return new self('Your proxy directory must be writable'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i see sometimes for a simple string is '' and other times as above in other files is "" perhaps we want to follow a determined pattern? |
||
} | ||
|
||
/** | ||
* @param string $className | ||
* @param string $methodName | ||
* @param string $parameterName | ||
* | ||
* @return self | ||
*/ | ||
public static function invalidParameterTypeHint($className, $methodName, $parameterName, \Exception $previous) | ||
{ | ||
return new self( | ||
sprintf( | ||
'The type hint of parameter "%s" in method "%s" in class "%s" is invalid.', | ||
$parameterName, | ||
$methodName, | ||
$className | ||
), | ||
0, | ||
$previous | ||
); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Curly brace should be on a new line.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
not for closures, at least in PSR-2
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok then