Skip to content

Commit

Permalink
[Docs] for Resource Bundle Resource Registration
Browse files Browse the repository at this point in the history
  • Loading branch information
dpfaffenbauer committed Feb 11, 2024
1 parent 88b9d82 commit 43196a9
Show file tree
Hide file tree
Showing 4 changed files with 248 additions and 136 deletions.
140 changes: 4 additions & 136 deletions docs/03_Bundles/Resource_Bundle/02_Pimcore_Entities.md
Original file line number Diff line number Diff line change
@@ -1,139 +1,7 @@
# Adding a New Pimcore Entity with Class Installation

## Step 1: Add a New Pimcore Class in Pimcore
There are multiple ways to add a new Pimcore Entity to CoreShop. This guide will show you 3 different ways:

1. Create a new class in Pimcore.
2. Add a Parent Class to your Pimcore Entity.
3. Export Class Definition to AppBundle/Resources/install/pimcore/classes/PimcoreEntity.json.

## Step 2: Create Parent Class

### PimcoreEntityInterface

Create PimcoreEntityInterface.php in the AppBundle/Model directory.

```php
<?php
// AppBundle/Model/PimcoreEntityInterface.php

interface PimcoreEntityInterface extends ResourceInterface {
public function getName($language = null);
public function setName($name, $language = null);
}
```

### PimcoreEntity

Create PimcoreEntity.php in the AppBundle/Model directory.

```php
<?php
// AppBundle/Model/PimcoreEntity.php

class PimcoreEntity extends AbstractPimcoreModel implements PimcoreEntityInterface, PimcoreModelInterface {
public function getName($language = null) {
throw new ImplementedByPimcoreException(__CLASS__, __METHOD__);
}

public function setName($name, $language = null) {
throw new ImplementedByPimcoreException(__CLASS__, __METHOD__);
}
}
```

## Step 3: Create Dependency Injection Configuration

### Configuration.php

Create Configuration.php in AppBundle/DependencyInjection.

```php
<?php
//AppBundle/DependencyInjection/Configuration.php

namespace AppBundle\DependencyInjection;

final class Configuration implements ConfigurationInterface
{
public function getConfigTreeBuilder()
{
$treeBuilder = new TreeBuilder();
$rootNode = $treeBuilder->root('app_custom');

$this->addModelsSection($rootNode);

return $treeBuilder;
}

private function addModelsSection(ArrayNodeDefinition $node)
{
$node
->children()
->arrayNode('pimcore')
->addDefaultsIfNotSet()
->children()
->arrayNode('pimcore_entity')
->addDefaultsIfNotSet()
->children()
->variableNode('PimcoreEntity')->end()
->arrayNode('options')
->scalarNode('path')->defaultValue('path/within/pimcore')->end()
->scalarNode('permission')->defaultValue('pimcore_entity')->cannotBeOverwritten()->end()
->addDefaultsIfNotSet()
->children()
->scalarNode('model')->defaultValue('Pimcore\Model\Object\PimcoreEntity')->cannotBeEmpty()->end()
->scalarNode('interface')->defaultValue(PimcoreEntity::class)->cannotBeEmpty()->end()
->scalarNode('factory')->defaultValue(PimcoreFactory::class)->cannotBeEmpty()->end()
->scalarNode('repository')->cannotBeEmpty()->end()
->scalarNode('admin_controller')->cannotBeEmpty()->end()
->scalarNode('install_file')->defaultValue('@AppBundle/Resources/install/pimcore/classes/PimcoreEntity.json')->end()
->scalarNode('type')->defaultValue(CoreShopResourceBundle::PIMCORE_MODEL_TYPE_OBJECT)->cannotBeOverwritten(true)->end()
->end()
->end()
->end()
->end()
->end()
->end()
->end()
;
}
}
```

### AppBundleExtension.php

Create AppBundleExtension.php in the same directory.

```php
<?php
//AppBundle/DependencyInjection/AppBundleExtension.php

namespace AppBundle\DependencyInjection;

final class AppBundleExtension extends AbstractModelExtension
{
public function load(array $config, ContainerBuilder $container)
{
$config = $this->processConfiguration($this->getConfiguration([], $container), $config);
$this->registerPimcoreModels('app', $config['pimcore'], $container);
}
}
```

## Step 4: Use Your Pimcore Entity

You can either use Pimcore Listing Classes or the automatically generated Factory/Repository Classes.

### Using Pimcore Listing Classes

```php
$list = new Pimcore\Model\Object\PimcoreEntity\Listing();
```

### Using Factory/Repository Classes

```php
$pimcoreEntityObject = $container->get('app.repository.pimcore_entity')->findBy($id);

$list = $container->get('app.repository.pimcore_entity')->getList();
```
- For Bundles that take advantage of the CoreShop Resource Bundle: [Pimcore Entities for Bundles](03_Pimocre_Entities_for_Bundles.md)
- For Projects using the Resource Config: [Pimcore for Projects using Resource Config](05_Pimocre_Entities_for_Projects_using_Resource_Config.md)
- For Projects using the `AsPimcoreModel` Attribute: [Pimcore for Projects using AsPimcoreModel Attribute](04_Pimocre_Entities_for_Projects_using_AsPimcoreModel_Attribute.md)
139 changes: 139 additions & 0 deletions docs/03_Bundles/Resource_Bundle/03_Pimocre_Entities_for_Bundles.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
# Pimcore Entities for Bundles

## Step 1: Add a New Pimcore Class in Pimcore

1. Create a new class in Pimcore.
2. Add a Parent Class to your Pimcore Entity.
3. Export Class Definition to YourBundle/Resources/install/pimcore/classes/PimcoreEntity.json.

## Step 2: Create Parent Class

### PimcoreEntityInterface

Create PimcoreEntityInterface.php in the YourBundle/Model directory.

```php
<?php
// YourBundle/Model/PimcoreEntityInterface.php

interface PimcoreEntityInterface extends ResourceInterface {
public function getName($language = null);
public function setName($name, $language = null);
}
```

### PimcoreEntity

Create PimcoreEntity.php in the YourBundle/Model directory.

```php
<?php
// YourBundle/Model/PimcoreEntity.php

class PimcoreEntity extends AbstractPimcoreModel implements PimcoreEntityInterface, PimcoreModelInterface {
public function getName($language = null) {
throw new ImplementedByPimcoreException(__CLASS__, __METHOD__);
}

public function setName($name, $language = null) {
throw new ImplementedByPimcoreException(__CLASS__, __METHOD__);
}
}
```

## Step 3: Create Dependency Injection Configuration

### Configuration.php

Create Configuration.php in YourBundle/DependencyInjection.

```php
<?php
//YourBundle/DependencyInjection/Configuration.php

namespace YourBundle\DependencyInjection;

final class Configuration implements ConfigurationInterface
{
public function getConfigTreeBuilder()
{
$treeBuilder = new TreeBuilder();
$rootNode = $treeBuilder->root('your_bundle');

$this->addModelsSection($rootNode);

return $treeBuilder;
}

private function addModelsSection(ArrayNodeDefinition $node)
{
$node
->children()
->arrayNode('pimcore')
->addDefaultsIfNotSet()
->children()
->arrayNode('pimcore_entity')
->addDefaultsIfNotSet()
->children()
->variableNode('PimcoreEntity')->end()
->arrayNode('options')
->scalarNode('path')->defaultValue('path/within/pimcore')->end()
->scalarNode('permission')->defaultValue('pimcore_entity')->cannotBeOverwritten()->end()
->addDefaultsIfNotSet()
->children()
->scalarNode('model')->defaultValue('Pimcore\Model\Object\PimcoreEntity')->cannotBeEmpty()->end()
->scalarNode('interface')->defaultValue(PimcoreEntity::class)->cannotBeEmpty()->end()
->scalarNode('factory')->defaultValue(PimcoreFactory::class)->cannotBeEmpty()->end()
->scalarNode('repository')->cannotBeEmpty()->end()
->scalarNode('admin_controller')->cannotBeEmpty()->end()
->scalarNode('install_file')->defaultValue('@YourBundle/Resources/install/pimcore/classes/PimcoreEntity.json')->end()
->scalarNode('type')->defaultValue(CoreShopResourceBundle::PIMCORE_MODEL_TYPE_OBJECT)->cannotBeOverwritten(true)->end()
->end()
->end()
->end()
->end()
->end()
->end()
->end()
;
}
}
```

### YourBundleExtension.php

Create YourBundleExtension.php in the same directory.

```php
<?php
//YourBundle/DependencyInjection/YourBundleExtension.php

namespace YourBundle\DependencyInjection;

final class YourBundleExtension extends AbstractModelExtension
{
public function load(array $config, ContainerBuilder $container)
{
$config = $this->processConfiguration($this->getConfiguration([], $container), $config);
$this->registerPimcoreModels('app', $config['pimcore'], $container);
}
}
```

## Step 4: Use Your Pimcore Entity

You can either use Pimcore Listing Classes or the automatically generated Factory/Repository Classes.

### Using Pimcore Listing Classes

```php
$list = new Pimcore\Model\Object\PimcoreEntity\Listing();
```

### Using Factory/Repository Classes

```php
$pimcoreEntityObject = $container->get('app.repository.pimcore_entity')->findBy($id);

$list = $container->get('app.repository.pimcore_entity')->getList();
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# Pimcore for Projects using AsPimcoreModel Attribute

## Step 1: Add a New Pimcore Class in Pimcore

1. Create a new class in Pimcore.
2. Add a Parent Class to your Pimcore Entity.

## Step 2: Create Parent Class

### PimcoreEntity

Create PimcoreEntity.php in the AppBundle/Model directory.

```php
<?php
// AppBundle/Model/PimcoreEntity.php

namespace AppBundle\Model;

use CoreShop\Bundle\ResourceBundle\Attribute\AsPimcoreModel;
use CoreShop\Component\Resource\Pimcore\Model\AbstractPimcoreModel;
use CoreShop\Component\Resource\Pimcore\Model\PimcoreModelInterface;

#[AsPimcoreModel(
pimcoreModel: 'Pimcore\Model\DataObject\PimcoreEntity',
type: 'object',
// optional: Resource Identifier
alias: 'app.pimcore_entity'
)]
abstract class PimcoreEntity extends AbstractPimcoreModel implements PimcoreModelInterface
{

}
```

## Step 4: Use Your Pimcore Entity

You can either use Pimcore Listing Classes or the automatically generated Factory/Repository Classes.

### Using Pimcore Listing Classes

```php
$list = new Pimcore\Model\Object\PimcoreEntity\Listing();
```

### Using Factory/Repository Classes

```php
$pimcoreEntityObject = $container->get('app.repository.pimcore_entity')->findBy($id);

$list = $container->get('app.repository.pimcore_entity')->getList();
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# Pimcore for Projects using Resource Config

## Step 1: Add a New Pimcore Class in Pimcore

1. Create a new class in Pimcore.
2. Add a Parent Class to your Pimcore Entity.

## Step 2: Create Parent Class

### PimcoreEntity

Create PimcoreEntity.php in the AppBundle/Model directory.

```php
<?php
// AppBundle/Model/PimcoreEntity.php

abstract class PimcoreEntity extends AbstractPimcoreModel implements PimcoreModelInterface
{

}
```

## Step 3: Create the Resource Configuration

### config/config.yaml

```yaml
core_shop_resource:
pimcore:
app.pimcore_entity:
classes:
model: Pimcore\Model\DataObject\PimcoreEntity
interface: CoreShop\Component\Resource\Model\ResourceInterface
```

## Step 4: Use Your Pimcore Entity

You can either use Pimcore Listing Classes or the automatically generated Factory/Repository Classes.

### Using Pimcore Listing Classes

```php
$list = new Pimcore\Model\Object\PimcoreEntity\Listing();
```

### Using Factory/Repository Classes

```php
$pimcoreEntityObject = $container->get('app.repository.pimcore_entity')->findBy($id);

$list = $container->get('app.repository.pimcore_entity')->getList();
```

0 comments on commit 43196a9

Please sign in to comment.