Skip to content

Commit

Permalink
Use factory for Pimcore object type instead static call.
Browse files Browse the repository at this point in the history
  • Loading branch information
vmalyk committed Oct 22, 2024
1 parent 9f4a052 commit d04d0ff
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 5 deletions.
6 changes: 1 addition & 5 deletions src/GraphQL/ClassTypeDefinitions.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,8 @@ public static function build(Service $graphQlService, $context = [])
{
$db = Db::get();
$listing = $db->fetchAllAssociative('SELECT id, name FROM classes');

foreach ($listing as $class) {
$id = $class['id'];
$name = $class['name'];
$objectType = new PimcoreObjectType($graphQlService, $name, $id, [], $context);
self::$definitions[$name] = $objectType;
self::$definitions[$class['name']] = $graphQlService->buildDataObjectType($class['name'], [], $context);
}

/**
Expand Down
53 changes: 53 additions & 0 deletions src/GraphQL/DataObjectTypeFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

declare(strict_types=1);

/**
* Pimcore
*
* This source file is available under two different licenses:
* - GNU General Public License version 3 (GPLv3)
* - Pimcore Commercial License (PCL)
* Full copyright and license information is available in
* LICENSE.md which is distributed with this source code.
*
* @copyright Copyright (c) Pimcore GmbH (http://www.pimcore.org)
* @license http://www.pimcore.org/license GPLv3 and PCL
*/

namespace Pimcore\Bundle\DataHubBundle\GraphQL;

use Pimcore\Bundle\DataHubBundle\GraphQL\Traits\ServiceTrait;
use Pimcore\Model\DataObject\ClassDefinition;

class DataObjectTypeFactory
{
use ServiceTrait;

public static $registry = [];

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

public function __construct(Service $graphQlService, string $className)
{
$this->className = $className;
$this->setGraphQLService($graphQlService);
}

/**
* @return mixed
*/
public function build(string $className, $config = [], $context = [])
{
if (!isset(self::$registry[$className])) {
$class = ClassDefinition::getByName($className);
$operatorImpl = new $this->className($this->getGraphQlService(), $className, $class->getId(), $config, $context);
self::$registry[$className] = $operatorImpl;
}

return self::$registry[$className];
}
}
17 changes: 17 additions & 0 deletions src/GraphQL/Service.php
Original file line number Diff line number Diff line change
Expand Up @@ -427,6 +427,23 @@ public function buildGeneralType($typeName)
return $result;
}

/**
* @param string $className
* @param array $config
* @param array $context
*
* @return mixed
*
* @throws \Exception
*/
public function buildDataObjectType(string $className, $config = [], $context = [])
{
$factory = $this->generalTypeGeneratorFactories->get('object');
$result = $factory->build($className, $config, $context);

return $result;
}

/**
* @param string $typeName
*
Expand Down
9 changes: 9 additions & 0 deletions src/Resources/config/graphql.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ services:
pimcore.datahub.graphql.dataobjecttype.quantity_value_input: '@Pimcore\Bundle\DataHubBundle\GraphQL\DataObjectType\QuantityValueInputType'
pimcore.datahub.graphql.dataobjecttype.input_quantity_value_input: '@Pimcore\Bundle\DataHubBundle\GraphQL\DataObjectType\InputQuantityValueInputType'

pimcore.datahub.graphql.dataobjecttype.object: '@Pimcore\Bundle\DataHubBundle\GraphQL\DataObjectType\PimcoreObjectType'
pimcore.datahub.graphql.dataobjecttype.object_tree: '@Pimcore\Bundle\DataHubBundle\GraphQL\DataObjectType\ObjectTreeType'
pimcore.datahub.graphql.dataobjecttype._object_folder: '@Pimcore\Bundle\DataHubBundle\GraphQL\DataObjectType\ObjectFolderType'
pimcore.datahub.graphql.dataobjecttype.asset_metadata_manytoone: '@Pimcore\Bundle\DataHubBundle\GraphQL\DataObjectType\HrefType'
Expand Down Expand Up @@ -85,6 +86,14 @@ services:
tags:
- { name: pimcore.datahub.graphql.generaltype_factory, id: document_tree }


pimcore.datahub.graphql.generaltype.factory.object:
class: Pimcore\Bundle\DataHubBundle\GraphQL\DataObjectTypeFactory
arguments:
$className: Pimcore\Bundle\DataHubBundle\GraphQL\DataObjectType\PimcoreObjectType
tags:
- { name: pimcore.datahub.graphql.generaltype_factory, id: object }

pimcore.datahub.graphql.generaltype.factory.object_tree:
class: Pimcore\Bundle\DataHubBundle\GraphQL\GeneralTypeFactory
arguments:
Expand Down

0 comments on commit d04d0ff

Please sign in to comment.