Skip to content

Commit

Permalink
PSR-4 Autoloading
Browse files Browse the repository at this point in the history
- Exceptions were moved in separated files
- Added aliases for exceptions
- Composer require PHP at ^7.1
  • Loading branch information
jelen07 committed Mar 21, 2019
1 parent e894e0e commit 4cfdf5d
Show file tree
Hide file tree
Showing 31 changed files with 346 additions and 305 deletions.
15 changes: 6 additions & 9 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"issues": "https://github.com/kdyby/doctrine/issues"
},
"require": {
"php": "^5.6 || ^7.0",
"php": "^7.1",
"doctrine/orm": "~2.5",
"doctrine/dbal": "~2.5",
"kdyby/console": "^2.7.1",
Expand All @@ -44,18 +44,15 @@
},
"minimum-stability": "dev",
"autoload": {
"psr-0": {
"psr-4": {
"Kdyby\\Doctrine\\": "src/",
"Kdyby\\Persistence\\": "src/"
},
"classmap": [
"src/Kdyby/Doctrine/exceptions.php"
]
}
},
"autoload-dev": {
"classmap": [
"tests/KdybyTests"
]
"psr-4": {
"KdybyTests\\": "tests/KdybyTests/"
}
},
"extra": {
"branch-alias": {
Expand Down
5 changes: 4 additions & 1 deletion src/Kdyby/Doctrine/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
use Doctrine\DBAL\Types\Type;
use Doctrine\DBAL\Driver;
use Kdyby;
use Kdyby\Doctrine\Exception\DBALException;
use Kdyby\Doctrine\Exception\DuplicateEntryException;
use Kdyby\Doctrine\Exception\EmptyValueException;
use Nette;
use PDO;
use Tracy;
Expand Down Expand Up @@ -375,7 +378,7 @@ public static function create(array $params, Doctrine\DBAL\Configuration $config
* @param \Exception|\Throwable $e
* @param string $query
* @param array $params
* @return \Kdyby\Doctrine\DBALException|\Exception|\Throwable
* @return \Kdyby\Doctrine\Exception\DBALException|\Exception|\Throwable
*/
public function resolveException($e, $query = NULL, $params = [])
{
Expand Down
8 changes: 4 additions & 4 deletions src/Kdyby/Doctrine/DI/OrmExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -171,12 +171,12 @@ public function loadConfiguration()
}

if (empty($config)) {
throw new Kdyby\Doctrine\UnexpectedValueException("Please configure the Doctrine extensions using the section '{$this->name}:' in your config file.");
throw new Kdyby\Doctrine\Exception\UnexpectedValueException("Please configure the Doctrine extensions using the section '{$this->name}:' in your config file.");
}

foreach ($config as $name => $emConfig) {
if (!is_array($emConfig) || (empty($emConfig['dbname']) && empty($emConfig['driver']))) {
throw new Kdyby\Doctrine\UnexpectedValueException("Please configure the Doctrine extensions using the section '{$this->name}:' in your config file.");
throw new Kdyby\Doctrine\Exception\UnexpectedValueException("Please configure the Doctrine extensions using the section '{$this->name}:' in your config file.");
}

/** @var mixed[] $emConfig */
Expand Down Expand Up @@ -224,7 +224,7 @@ protected function loadConsole()
$cli->setClass($command);

} else {
throw new Kdyby\Doctrine\NotSupportedException;
throw new Kdyby\Doctrine\Exception\NotSupportedException;
}
}
}
Expand Down Expand Up @@ -845,7 +845,7 @@ private function addCollapsePathsToTracy(Method $init)
$blueScreen = \Tracy\Debugger::class . '::getBlueScreen()';
$commonDirname = dirname(Nette\Reflection\ClassType::from(Doctrine\Common\Version::class)->getFileName());

$init->addBody($blueScreen . '->collapsePaths[] = ?;', [dirname(Nette\Reflection\ClassType::from(Kdyby\Doctrine\Exception::class)->getFileName())]);
$init->addBody($blueScreen . '->collapsePaths[] = ?;', [dirname(Nette\Reflection\ClassType::from(Kdyby\Doctrine\Exception\IException::class)->getFileName())]);
$init->addBody($blueScreen . '->collapsePaths[] = ?;', [dirname(dirname(dirname(dirname($commonDirname))))]); // this should be vendor/doctrine
foreach ($this->proxyAutoloaders as $dir) {
$init->addBody($blueScreen . '->collapsePaths[] = ?;', [$dir]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ public function renderEntityManagerException($e)
public function bindEntityManager(Doctrine\ORM\EntityManager $em)
{
if ($this->em !== NULL) {
throw new Kdyby\Doctrine\InvalidStateException(sprintf('%s is already bound to an entity manager.', __CLASS__));
throw new Kdyby\Doctrine\Exception\InvalidStateException(sprintf('%s is already bound to an entity manager.', __CLASS__));
}

$this->em = $em;
Expand Down
10 changes: 5 additions & 5 deletions src/Kdyby/Doctrine/Diagnostics/Panel.php
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,7 @@ public function renderQueryException($e)
'panel' => $this->dumpQuery($sql, $params, $types, $source),
];

} elseif ($e instanceof Kdyby\Doctrine\QueryException && $e->query !== NULL) {
} elseif ($e instanceof Kdyby\Doctrine\Exception\QueryException && $e->query !== NULL) {
if ($e->query instanceof Doctrine\ORM\Query) {
return [
'tab' => 'DQL',
Expand Down Expand Up @@ -409,7 +409,7 @@ public static function renderException($e, Nette\DI\Container $dic)
];
}

} elseif ($e instanceof Kdyby\Doctrine\DBALException && $e->query !== NULL) {
} elseif ($e instanceof Kdyby\Doctrine\Exception\DBALException && $e->query !== NULL) {
return [
'tab' => 'SQL',
'panel' => self::highlightQuery(static::formatQuery($e->query, $e->params, [])),
Expand Down Expand Up @@ -600,7 +600,7 @@ public static function formatQuery($query, $params, array $types = [], AbstractP
if (Nette\Utils\Validators::isList($params)) {
$parts = explode('?', $query);
if (count($params) > $parts) {
throw new Kdyby\Doctrine\InvalidStateException("Too mny parameters passed to query.");
throw new Kdyby\Doctrine\Exception\InvalidStateException("Too mny parameters passed to query.");
}

return implode('', Kdyby\Doctrine\Helpers::zipper($parts, $params));
Expand Down Expand Up @@ -803,7 +803,7 @@ private static function editorLink($file, $line, $text = NULL)
public function enableLogging()
{
if ($this->connection === NULL) {
throw new Kdyby\Doctrine\InvalidStateException("Doctrine Panel is not bound to connection.");
throw new Kdyby\Doctrine\Exception\InvalidStateException("Doctrine Panel is not bound to connection.");
}

$config = $this->connection->getConfiguration();
Expand All @@ -826,7 +826,7 @@ public function enableLogging()
public function bindConnection(Doctrine\DBAL\Connection $connection)
{
if ($this->connection !== NULL) {
throw new Kdyby\Doctrine\InvalidStateException("Doctrine Panel is already bound to connection.");
throw new Kdyby\Doctrine\Exception\InvalidStateException("Doctrine Panel is already bound to connection.");
}

$this->connection = $connection;
Expand Down
2 changes: 1 addition & 1 deletion src/Kdyby/Doctrine/Entities/SerializableMixin.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
namespace Kdyby\Doctrine\Entities;

use Kdyby;
use Kdyby\Doctrine\StaticClassException;
use Kdyby\Doctrine\Exception\StaticClassException;
use Nette;
use Nette\Reflection\ClassType;
use Serializable;
Expand Down
2 changes: 2 additions & 0 deletions src/Kdyby/Doctrine/EntityManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
use Doctrine\ORM\Query;
use Kdyby;
use Kdyby\Doctrine\Diagnostics\EntityManagerUnitOfWorkSnapshotPanel;
use Kdyby\Doctrine\Exception\NotSupportedException;
use Kdyby\Doctrine\Exception\QueryException;
use Kdyby\Doctrine\QueryObject;
use Kdyby\Doctrine\Tools\NonLockingUniqueInserter;
use Kdyby\Persistence;
Expand Down
5 changes: 3 additions & 2 deletions src/Kdyby/Doctrine/EntityRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Doctrine\ORM\NoResultException;
use Doctrine\ORM\NonUniqueResultException;
use Kdyby;
use Kdyby\Doctrine\Exception\QueryException;
use Kdyby\Persistence;
use Nette;

Expand Down Expand Up @@ -286,7 +287,7 @@ public function getReference($id)
/**
* @param \Exception $e
* @param \Kdyby\Persistence\Query $queryObject
* @return \Kdyby\Doctrine\QueryException
* @return \Kdyby\Doctrine\Exception\QueryException
*/
private function handleQueryException(\Exception $e, Persistence\Query $queryObject)
{
Expand All @@ -301,7 +302,7 @@ private function handleQueryException(\Exception $e, Persistence\Query $queryObj
* @param \Exception $e
* @param \Doctrine\ORM\Query $query
* @param string $message
* @return \Exception|\Kdyby\Doctrine\QueryException
* @return \Exception|\Kdyby\Doctrine\Exception\QueryException
*/
private function handleException(\Exception $e, Doctrine\ORM\Query $query = NULL, $message = NULL)
{
Expand Down
57 changes: 57 additions & 0 deletions src/Kdyby/Doctrine/Exception/DBALException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php

namespace Kdyby\Doctrine\Exception;
use Doctrine;

/**
* @author Filip Procházka <[email protected]>
* @deprecated
*/
class DBALException extends \RuntimeException implements IException
{

/**
* @var string|NULL
*/
public $query;

/**
* @var array
*/
public $params = [];

/**
* @var \Doctrine\DBAL\Connection|NULL
*/
public $connection;


/**
* @param \Exception|\Throwable $previous
* @param string|NULL $query
* @param array $params
* @param \Doctrine\DBAL\Connection|NULL $connection
* @param string|NULL $message
*/
public function __construct($previous, $query = NULL, $params = [], Doctrine\DBAL\Connection $connection = NULL, $message = NULL)
{
parent::__construct($message ?: $previous->getMessage(), $previous->getCode(), $previous);
$this->query = $query;
$this->params = $params;
$this->connection = $connection;
}


/**
* This is just a paranoia, hopes no one actually serializes exceptions.
*
* @return array
*/
public function __sleep()
{
return ['message', 'code', 'file', 'line', 'errorInfo', 'query', 'params'];
}

}

class_alias(DBALException::class, 'Kdyby\Doctrine\DBALException');
45 changes: 45 additions & 0 deletions src/Kdyby/Doctrine/Exception/DuplicateEntryException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

namespace Kdyby\Doctrine\Exception;

use Doctrine;
use Kdyby\Doctrine\Exception;

/**
* @author Filip Procházka <[email protected]>
* @deprecated
*/
class DuplicateEntryException extends DBALException
{

/**
* @var array
*/
public $columns;


/**
* @param \Exception|\Throwable $previous
* @param array $columns
* @param string $query
* @param array $params
* @param \Doctrine\DBAL\Connection $connection
*/
public function __construct($previous, $columns = [], $query = NULL, $params = [], Doctrine\DBAL\Connection $connection = NULL)
{
parent::__construct($previous, $query, $params, $connection);
$this->columns = $columns;
}


/**
* @return array
*/
public function __sleep()
{
return array_merge(parent::__sleep(), ['columns']);
}

}

class_alias(DuplicateEntryException::class, 'Kdyby\Doctrine\DuplicateEntryException');
44 changes: 44 additions & 0 deletions src/Kdyby/Doctrine/Exception/EmptyValueException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

namespace Kdyby\Doctrine\Exception;

use Doctrine;

/**
* @author Filip Procházka <[email protected]>
* @deprecated
*/
class EmptyValueException extends DBALException
{

/**
* @var string|NULL
*/
public $column;


/**
* @param \Exception|\Throwable $previous
* @param string|NULL $column
* @param string $query
* @param array $params
* @param \Doctrine\DBAL\Connection $connection
*/
public function __construct($previous, $column = NULL, $query = NULL, $params = [], Doctrine\DBAL\Connection $connection = NULL)
{
parent::__construct($previous, $query, $params, $connection);
$this->column = $column;
}


/**
* @return array
*/
public function __sleep()
{
return array_merge(parent::__sleep(), ['column']);
}

}

class_alias(EmptyValueException::class, 'Kdyby\Doctrine\EmptyValueException');
15 changes: 15 additions & 0 deletions src/Kdyby/Doctrine/Exception/IException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

declare(strict_types=1);

namespace Kdyby\Doctrine\Exception;

/**
* @author Filip Procházka <[email protected]>
*/
interface IException
{

}

class_alias(IException::class, 'Kdyby\Doctrine\Exception');
15 changes: 15 additions & 0 deletions src/Kdyby/Doctrine/Exception/InvalidArgumentException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

declare(strict_types=1);

namespace Kdyby\Doctrine\Exception;

/**
* @author Filip Procházka <[email protected]>
*/
class InvalidArgumentException extends \InvalidArgumentException implements IException
{

}

class_alias(InvalidArgumentException::class, 'Kdyby\Doctrine\InvalidArgumentException');
15 changes: 15 additions & 0 deletions src/Kdyby/Doctrine/Exception/InvalidStateException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

declare(strict_types=1);

namespace Kdyby\Doctrine\Exception;

/**
* @author Filip Procházka <[email protected]>
*/
class InvalidStateException extends \RuntimeException implements IException
{

}

class_alias(InvalidStateException::class, 'Kdyby\Doctrine\InvalidStateException');
13 changes: 13 additions & 0 deletions src/Kdyby/Doctrine/Exception/MissingClassException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace Kdyby\Doctrine\Exception;

/**
* When class is not found
*/
class MissingClassException extends \LogicException implements IException
{

}

class_alias(MissingClassException::class, 'Kdyby\Doctrine\MissingClassException');
Loading

0 comments on commit 4cfdf5d

Please sign in to comment.