Skip to content

Scope to constrain - compatability update #209

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

Merged
merged 8 commits into from
Sep 3, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ vendor/
*.db
clover.xml
docker-compose.override.yml
/.phpunit.result.cache
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"mockery/mockery": "^1.1",
"spiral/dumper": "^2.7",
"ramsey/uuid": "^3.8",
"spiral/code-style": "^1.0"
"spiral/code-style": "^1.0.6"
},
"autoload": {
"psr-4": {
Expand Down
14 changes: 7 additions & 7 deletions src/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@
use Cycle\ORM\Exception\TypecastException;
use Cycle\ORM\Mapper\Mapper;
use Cycle\ORM\Relation\RelationInterface;
use Cycle\ORM\Select\ConstrainInterface;
use Cycle\ORM\Select\LoaderInterface;
use Cycle\ORM\Select\Repository;
use Cycle\ORM\Select\ScopeInterface;
use Cycle\ORM\Select\Source;
use Cycle\ORM\Select\SourceInterface;
use Spiral\Core\Container;
Expand All @@ -41,7 +41,7 @@ final class Factory implements FactoryInterface
Schema::REPOSITORY => Repository::class,
Schema::SOURCE => Source::class,
Schema::MAPPER => Mapper::class,
Schema::CONSTRAIN => null,
Schema::SCOPE => null,
];

/**
Expand Down Expand Up @@ -194,17 +194,17 @@ public function source(
$schema->define($role, Schema::TABLE)
);

$constrain = $schema->define($role, Schema::CONSTRAIN) ?? $this->defaults[Schema::CONSTRAIN];
$scope = $schema->define($role, Schema::SCOPE) ?? $this->defaults[Schema::SCOPE];

if ($constrain === null) {
if ($scope === null) {
return $source;
}

if (!is_subclass_of($constrain, ConstrainInterface::class)) {
throw new TypecastException($constrain . ' does not implement ' . ConstrainInterface::class);
if (!is_subclass_of($scope, ScopeInterface::class)) {
throw new TypecastException($scope . ' does not implement ' . ScopeInterface::class);
}

return $source->withConstrain(is_object($constrain) ? $constrain : $this->factory->make($constrain));
return $source->withConstrain(is_object($scope) ? $scope : $this->factory->make($scope));
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/ORM.php
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ public function getRepository($entity): RepositoryInterface

if ($this->schema->define($role, Schema::TABLE) !== null) {
$select = new Select($this, $role);
$select->constrain($this->getSource($role)->getConstrain());
$select->scope($this->getSource($role)->getConstrain());
}

return $this->repositories[$role] = $this->factory->repository($this, $this->schema, $role, $select);
Expand Down
6 changes: 3 additions & 3 deletions src/Relation/Pivoted/PivotedPromise.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,9 +113,9 @@ public function __resolve()

/** @var ManyToManyLoader $loader */
$loader = $loader->withContext($loader, [
'constrain' => $this->orm->getSource($this->target)->getConstrain(),
'as' => $this->target,
'method' => JoinableLoader::POSTLOAD
'scope' => $this->orm->getSource($this->target)->getConstrain(),
'as' => $this->target,
'method' => JoinableLoader::POSTLOAD
]);

$query = $loader->configureQuery($query, [$this->innerKey]);
Expand Down
4 changes: 2 additions & 2 deletions src/SchemaInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ interface SchemaInterface
public const TYPECAST = 13;
public const SCHEMA = 14;

/** @deprecated Use {@see SCOPE} instead. */
public const CONSTRAIN = 12;
/** @deprecated Use {@see SchemaInterface::SCOPE} instead. */
public const CONSTRAIN = self::SCOPE;

/**
* Return all roles defined within the schema.
Expand Down
16 changes: 11 additions & 5 deletions src/Select.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use Cycle\ORM\Select\JoinableLoader;
use Cycle\ORM\Select\QueryBuilder;
use Cycle\ORM\Select\RootLoader;
use Cycle\ORM\Select\ScopeInterface;
use IteratorAggregate;
use Spiral\Database\Query\SelectQuery;
use Spiral\Pagination\PaginableInterface;
Expand Down Expand Up @@ -114,14 +115,19 @@ public function __clone()
}

/**
* Create new Selector with applied scope. By default no constrain used.
*
* @param ConstrainInterface|null $constrain
* @return Select
* @deprecated Will be dropped in next major release. Use {@see scope()} instead.
*/
public function constrain(ConstrainInterface $constrain = null): self
{
$this->loader->setConstrain($constrain);
return $this->scope($constrain);
}

/**
* Create new Selector with applied scope. By default no scope used.
*/
public function scope(ScopeInterface $scope = null): self
{
$this->loader->setScope($scope);

return $this;
}
Expand Down
18 changes: 15 additions & 3 deletions src/Select/AbstractLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,16 +52,16 @@ abstract class AbstractLoader implements LoaderInterface
public const JOIN = 3;
public const LEFT_JOIN = 4;

/** @var ORMInterface|SourceProviderInterface @internal */
/** @var ORMInterface @internal */
protected $orm;

/** @var string */
protected $target;

/** @var array */
protected $options = [
'load' => false,
'constrain' => true,
'load' => false,
'scope' => true,
];

/** @var LoaderInterface[] */
Expand Down Expand Up @@ -132,6 +132,8 @@ public function getSource(): SourceInterface
*/
public function withContext(LoaderInterface $parent, array $options = []): LoaderInterface
{
$options = $this->prepareOptions($options);

// check that given options are known
if (!empty($wrong = array_diff(array_keys($options), array_keys($this->options)))) {
throw new LoaderException(
Expand Down Expand Up @@ -325,4 +327,14 @@ protected function getEagerRelations(): \Generator
}
}
}

protected function prepareOptions(array $options): array
{
if (array_key_exists('constrain', $options) && !array_key_exists('scope', $options)) {
$options['scope'] = $options['constrain'];
}
unset($options['constrain']);

return $options;
}
}
24 changes: 8 additions & 16 deletions src/Select/ConstrainInterface.php
Original file line number Diff line number Diff line change
@@ -1,25 +1,17 @@
<?php

/**
* Cycle DataMapper ORM
*
* @license MIT
* @author Anton Titov (Wolfy-J)
*/
// phpcs:ignoreFile

declare(strict_types=1);

namespace Cycle\ORM\Select;

/**
* Provides the ability to modify the selector and/or entity loader. Can be used to implement multi-table inheritance.
*/
interface ConstrainInterface
{
\class_alias(ScopeInterface::class, __NAMESPACE__ . '\ConstrainInterface');

if (false) {
/**
* Configure query and loader pair using proxy strategy.
*
* @param QueryBuilder $query
* @deprecated Use {@see ScopeInterface} instead.
*/
public function apply(QueryBuilder $query);
interface ConstrainInterface extends ScopeInterface
{
}
}
22 changes: 12 additions & 10 deletions src/Select/JoinableLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
use Cycle\ORM\Parser\AbstractNode;
use Cycle\ORM\Schema;
use Cycle\ORM\Select\Traits\ColumnsTrait;
use Cycle\ORM\Select\Traits\ConstrainTrait;
use Cycle\ORM\Select\Traits\ScopeTrait;
use Spiral\Database\Query\SelectQuery;
use Spiral\Database\StatementInterface;

Expand All @@ -26,7 +26,7 @@
abstract class JoinableLoader extends AbstractLoader implements JoinableInterface
{
use ColumnsTrait;
use ConstrainTrait;
use ScopeTrait;

/**
* Default set of relation options. Child implementation might defined their of default options.
Expand All @@ -38,7 +38,7 @@ abstract class JoinableLoader extends AbstractLoader implements JoinableInterfac
'load' => false,

// true or instance to enable, false or null to disable
'constrain' => true,
'scope' => true,

// scope to be used for the relation
'method' => null,
Expand Down Expand Up @@ -102,6 +102,8 @@ public function getAlias(): string
*/
public function withContext(LoaderInterface $parent, array $options = []): LoaderInterface
{
$options = $this->prepareOptions($options);

/**
* @var AbstractLoader $parent
* @var self $loader
Expand All @@ -122,14 +124,14 @@ public function withContext(LoaderInterface $parent, array $options = []): Loade
//Calculate table alias
$loader->options['as'] = $loader->calculateAlias($parent);

if (array_key_exists('constrain', $options)) {
if ($loader->options['constrain'] instanceof ConstrainInterface) {
$loader->setConstrain($loader->options['constrain']);
} elseif (is_string($loader->options['constrain'])) {
$loader->setConstrain($this->orm->getFactory()->make($loader->options['constrain']));
if (array_key_exists('scope', $options)) {
if ($loader->options['scope'] instanceof ScopeInterface) {
$loader->setScope($loader->options['scope']);
} elseif (is_string($loader->options['scope'])) {
$loader->setScope($this->orm->getFactory()->make($loader->options['scope']));
}
} else {
$loader->setConstrain($this->getSource()->getConstrain());
$loader->setScope($this->getSource()->getConstrain());
}

if ($loader->isLoaded()) {
Expand Down Expand Up @@ -218,7 +220,7 @@ public function configureQuery(SelectQuery $query, array $outerKeys = []): Selec
$this->mountColumns($query, $this->options['minify'], '', true);
}

if ($this->options['load'] instanceof ConstrainInterface) {
if ($this->options['load'] instanceof ScopeInterface) {
$this->options['load']->apply($this->makeQueryBuilder($query));
}

Expand Down
14 changes: 7 additions & 7 deletions src/Select/Loader/BelongsToLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,13 @@ class BelongsToLoader extends JoinableLoader
* @var array
*/
protected $options = [
'load' => false,
'constrain' => true,
'method' => self::POSTLOAD,
'minify' => true,
'as' => null,
'using' => null,
'where' => null,
'load' => false,
'scope' => true,
'method' => self::POSTLOAD,
'minify' => true,
'as' => null,
'using' => null,
'where' => null,
];

/**
Expand Down
16 changes: 8 additions & 8 deletions src/Select/Loader/HasManyLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,14 @@ class HasManyLoader extends JoinableLoader
* @var array
*/
protected $options = [
'load' => false,
'constrain' => true,
'method' => self::POSTLOAD,
'minify' => true,
'as' => null,
'using' => null,
'where' => null,
'orderBy' => null,
'load' => false,
'scope' => true,
'method' => self::POSTLOAD,
'minify' => true,
'as' => null,
'using' => null,
'where' => null,
'orderBy' => null,
];

/**
Expand Down
14 changes: 7 additions & 7 deletions src/Select/Loader/HasOneLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,13 @@ class HasOneLoader extends JoinableLoader
* @var array
*/
protected $options = [
'load' => false,
'constrain' => true,
'method' => self::INLOAD,
'minify' => true,
'as' => null,
'using' => null,
'where' => null
'load' => false,
'scope' => true,
'method' => self::INLOAD,
'minify' => true,
'as' => null,
'using' => null,
'where' => null
];

/**
Expand Down
21 changes: 11 additions & 10 deletions src/Select/Loader/ManyToManyLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
use Cycle\ORM\Select\Traits\WhereTrait;
use Spiral\Database\Injection\Parameter;
use Spiral\Database\Query\SelectQuery;
use Spiral\Database\StatementInterface;

class ManyToManyLoader extends JoinableLoader
{
Expand All @@ -37,15 +36,15 @@ class ManyToManyLoader extends JoinableLoader
* @var array
*/
protected $options = [
'load' => false,
'constrain' => true,
'method' => self::POSTLOAD,
'minify' => true,
'as' => null,
'using' => null,
'where' => null,
'orderBy' => null,
'pivot' => null,
'load' => false,
'scope' => true,
'method' => self::POSTLOAD,
'minify' => true,
'as' => null,
'using' => null,
'where' => null,
'orderBy' => null,
'pivot' => null,
];

/** @var PivotLoader */
Expand Down Expand Up @@ -78,6 +77,8 @@ public function __clone()
*/
public function withContext(LoaderInterface $parent, array $options = []): LoaderInterface
{
$options = $this->prepareOptions($options);

/** @var ManyToManyLoader $loader */
$loader = parent::withContext($parent, $options);
$loader->pivot = $loader->pivot->withContext(
Expand Down
12 changes: 6 additions & 6 deletions src/Select/Loader/PivotLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,12 @@ class PivotLoader extends JoinableLoader
* @var array
*/
protected $options = [
'load' => false,
'constrain' => true,
'method' => self::JOIN,
'minify' => true,
'as' => null,
'using' => null
'load' => false,
'scope' => true,
'method' => self::JOIN,
'minify' => true,
'as' => null,
'using' => null
];

/**
Expand Down
Loading