Skip to content

Commit

Permalink
fixed issue with static variables
Browse files Browse the repository at this point in the history
  • Loading branch information
koertho committed Mar 1, 2024
1 parent 4531d66 commit 719a432
Show file tree
Hide file tree
Showing 9 changed files with 51 additions and 29 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@
"preferred-install": "dist",
"allow-plugins": {
"contao-components/installer": true,
"contao/manager-plugin": true
"contao/manager-plugin": true,
"php-http/discovery": false
}
},
"extra": {
Expand Down
20 changes: 10 additions & 10 deletions src/Dca/AbstractDcaField.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,30 +4,30 @@

abstract class AbstractDcaField
{
protected static $tables = [];

/**
* Register a dca to have an author field and update logic added.
*/
public static function register(string $table): DcaFieldOptions
public static function register(string $table): DcaFieldConfiguration
{
$config = static::createOptionObject($table);

static::$tables[$table] = $config;

static::storeConfig($config);
return $config;
}

abstract protected static function storeConfig(DcaFieldConfiguration $config): void;

abstract protected static function loadConfig(): array;

/**
* @return array<DcaFieldOptions>
* @return array<DcaFieldConfiguration>
*/
public static function getRegistrations(): array
{
return static::$tables;
return static::loadConfig();
}

protected static function createOptionObject(string $table): DcaFieldOptions
protected static function createOptionObject(string $table): DcaFieldConfiguration
{
return new DcaFieldOptions($table);
return new DcaFieldConfiguration($table);
}
}
18 changes: 14 additions & 4 deletions src/Dca/AuthorField.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@ class AuthorField extends AbstractDcaField
public const TYPE_USER = 'user';
public const TYPE_MEMBER = 'member';

protected static $tables = [];

/**
* @return array<AuthorFieldOptions>
* @return array<AuthorFieldConfiguration>
*/
public static function getRegistrations(): array
{
Expand All @@ -17,12 +19,20 @@ public static function getRegistrations(): array

/**
* @param string $table
* @return AuthorFieldOptions
* @return AuthorFieldConfiguration
*/
protected static function createOptionObject(string $table): DcaFieldOptions
protected static function createOptionObject(string $table): DcaFieldConfiguration
{
return new AuthorFieldOptions($table);
return new AuthorFieldConfiguration($table);
}

protected static function storeConfig(DcaFieldConfiguration $config): void
{
static::$tables[$config->getTable()] = $config;
}

protected static function loadConfig(): array
{
return static::$tables;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace HeimrichHannot\UtilsBundle\Dca;

class AuthorFieldOptions extends DcaFieldOptions
class AuthorFieldConfiguration extends DcaFieldConfiguration
{
/** @var string */
protected $type = AuthorField::TYPE_USER;
Expand All @@ -17,7 +17,7 @@ class AuthorFieldOptions extends DcaFieldOptions
/** @var bool */
protected $filter = true;

public function setType(string $type): AuthorFieldOptions
public function setType(string $type): AuthorFieldConfiguration
{
$this->type = $type;
return $this;
Expand All @@ -33,7 +33,7 @@ public function getFieldNamePrefix(): string
return $this->fieldNamePrefix;
}

public function setFieldNamePrefix(string $fieldNamePrefix): AuthorFieldOptions
public function setFieldNamePrefix(string $fieldNamePrefix): AuthorFieldConfiguration
{
$this->fieldNamePrefix = $fieldNamePrefix;
return $this;
Expand All @@ -44,7 +44,7 @@ public function isUseDefaultLabel(): bool
return $this->useDefaultLabel;
}

public function setUseDefaultLabel(bool $useDefaultLabel): AuthorFieldOptions
public function setUseDefaultLabel(bool $useDefaultLabel): AuthorFieldConfiguration
{
$this->useDefaultLabel = $useDefaultLabel;
return $this;
Expand All @@ -55,7 +55,7 @@ public function isExclude(): bool
return $this->exclude;
}

public function setExclude(bool $exclude): AuthorFieldOptions
public function setExclude(bool $exclude): AuthorFieldConfiguration
{
$this->exclude = $exclude;
return $this;
Expand All @@ -66,7 +66,7 @@ public function isSearch(): bool
return $this->search;
}

public function setSearch(bool $search): AuthorFieldOptions
public function setSearch(bool $search): AuthorFieldConfiguration
{
$this->search = $search;
return $this;
Expand All @@ -77,7 +77,7 @@ public function isFilter(): bool
return $this->filter;
}

public function setFilter(bool $filter): AuthorFieldOptions
public function setFilter(bool $filter): AuthorFieldConfiguration
{
$this->filter = $filter;
return $this;
Expand Down
11 changes: 11 additions & 0 deletions src/Dca/DateAddedField.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,15 @@

class DateAddedField extends AbstractDcaField
{
private static $tables = [];

protected static function storeConfig(DcaFieldConfiguration $config): void
{
static::$tables[$config->getTable()] = $config;
}

protected static function loadConfig(): array
{
return static::$tables;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace HeimrichHannot\UtilsBundle\Dca;

class DcaFieldOptions
class DcaFieldConfiguration
{
/**
* @var string
Expand Down
6 changes: 3 additions & 3 deletions src/EventListener/DcaField/DcaAuthorListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
use Contao\FrontendUser;
use Contao\Model;
use HeimrichHannot\UtilsBundle\Dca\AuthorField;
use HeimrichHannot\UtilsBundle\Dca\AuthorFieldOptions;
use HeimrichHannot\UtilsBundle\Dca\AuthorFieldConfiguration;
use Symfony\Component\Security\Core\Security;

class DcaAuthorListener
Expand Down Expand Up @@ -103,10 +103,10 @@ public function onConfigCopyCallback(int $insertId, DataContainer $dc): void
}

/**
* @param AuthorFieldOptions $options
* @param AuthorFieldConfiguration $options
* @return string
*/
protected function getAuthorFieldName(AuthorFieldOptions $options): string
protected function getAuthorFieldName(AuthorFieldConfiguration $options): string
{
if (!$options->hasFieldNamePrefix()) {
return 'author';
Expand Down
4 changes: 2 additions & 2 deletions tests/Dca/DcaFieldOptionsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@
namespace HeimrichHannot\UtilsBundle\Tests\Dca;


use HeimrichHannot\UtilsBundle\Dca\DcaFieldOptions;
use HeimrichHannot\UtilsBundle\Dca\DcaFieldConfiguration;
use PHPUnit\Framework\TestCase;

class DcaFieldOptionsTest extends TestCase
{
public function testGetTable()
{
$dcaFieldOptions = new DcaFieldOptions('test_table');
$dcaFieldOptions = new DcaFieldConfiguration('test_table');
$this->assertEquals('test_table', $dcaFieldOptions->getTable());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use Contao\DataContainer;
use Contao\Model;
use HeimrichHannot\UtilsBundle\Dca\DateAddedField;
use HeimrichHannot\UtilsBundle\Dca\DcaFieldOptions;
use HeimrichHannot\UtilsBundle\Dca\DcaFieldConfiguration;
use HeimrichHannot\UtilsBundle\EventListener\DcaField\DateAddedFieldListener;
use PHPUnit\Framework\TestCase;

Expand Down

0 comments on commit 719a432

Please sign in to comment.