Skip to content

Commit 96a555a

Browse files
committed
Refactor: Modernize entity handling, add PostTypeService, and fix tests
1 parent 6dd4000 commit 96a555a

23 files changed

Lines changed: 123 additions & 3907 deletions

src/Adapter/Out/WordPress/PostTypeRegistryAdapter.php

Lines changed: 0 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -41,31 +41,14 @@ public function registerPostType(string $slug, array $args, array $names): void
4141
{
4242
// Check if required WordPress functions exist
4343
if (! function_exists('\\register_post_type') && ! function_exists('\\register_extended_post_type')) {
44-
if (function_exists('\\error_log')) {
45-
\error_log('Cannot register post type: Neither register_post_type nor register_extended_post_type functions are available');
46-
}
47-
4844
return;
4945
}
5046

5147
// Use global namespace for the function
5248
if (function_exists('\\register_extended_post_type')) {
5349
try {
54-
if (function_exists('\\error_log')) {
55-
\error_log("Calling register_extended_post_type for {$slug} with args: ".json_encode($args));
56-
\error_log('Names: '.json_encode($names));
57-
}
58-
5950
\register_extended_post_type($slug, $args, $names);
60-
61-
if (function_exists('\\error_log')) {
62-
\error_log("Successfully registered extended post type: {$slug}");
63-
}
6451
} catch (\Exception $e) {
65-
if (function_exists('\\error_log')) {
66-
\error_log('Error registering extended post type: '.$e->getMessage());
67-
}
68-
6952
// Fallback to standard WordPress function
7053
$this->fallbackToStandardPostTypeRegistration($slug, $args, $names);
7154
}
@@ -103,22 +86,9 @@ protected function fallbackToStandardPostTypeRegistration(string $slug, array $a
10386
$customSlug = $slug;
10487
}
10588

106-
if (function_exists('\\error_log')) {
107-
\error_log("Falling back to standard register_post_type for {$customSlug}");
108-
}
109-
11089
\register_post_type($customSlug, $args);
111-
112-
if (function_exists('\\error_log')) {
113-
\error_log("Successfully registered standard post type: {$customSlug}");
114-
}
11590
} catch (\Exception $e) {
116-
if (function_exists('\\error_log')) {
117-
\error_log('Error in fallback post type registration: '.$e->getMessage());
118-
}
11991
}
120-
} elseif (function_exists('\\error_log')) {
121-
\error_log('Cannot register post type: register_post_type function not available');
12292
}
12393
}
12494
}

src/Adapter/Out/WordPress/TaxonomyRegistryAdapter.php

Lines changed: 0 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,6 @@ public function register(string $slug, array $args, array $names): void
3131
if (isset($args['object_type'])) {
3232
$this->registerTaxonomy($slug, $args['object_type'], $args, $names);
3333
unset($args['object_type']);
34-
} else {
35-
if (function_exists('\\error_log')) {
36-
\error_log('Cannot register taxonomy: Missing object_type parameter');
37-
}
3834
}
3935
}
4036

@@ -53,21 +49,8 @@ public function registerTaxonomy(string $slug, string|array $objectType, array $
5349
// Use global namespace for the function
5450
if (function_exists('\\register_extended_taxonomy')) {
5551
try {
56-
if (function_exists('\\error_log')) {
57-
\error_log("Calling register_extended_taxonomy for {$slug} with object type: ".
58-
(is_array($objectType) ? implode(', ', $objectType) : $objectType));
59-
}
60-
6152
\register_extended_taxonomy($slug, $objectType, $args, $names);
62-
63-
if (function_exists('\\error_log')) {
64-
\error_log("Successfully registered extended taxonomy: {$slug}");
65-
}
6653
} catch (\Exception $e) {
67-
if (function_exists('\\error_log')) {
68-
\error_log('Error registering extended taxonomy: '.$e->getMessage());
69-
}
70-
7154
// Fallback to standard WordPress function
7255
$this->fallbackToStandardTaxonomyRegistration($slug, $objectType, $args, $names);
7356
}
@@ -106,22 +89,9 @@ protected function fallbackToStandardTaxonomyRegistration(string $slug, string|a
10689
$customSlug = $slug;
10790
}
10891

109-
if (function_exists('\\error_log')) {
110-
\error_log("Falling back to standard register_taxonomy for {$customSlug}");
111-
}
112-
11392
\register_taxonomy($customSlug, $objectType, $args);
114-
115-
if (function_exists('\\error_log')) {
116-
\error_log("Successfully registered standard taxonomy: {$customSlug}");
117-
}
11893
} catch (\Exception $e) {
119-
if (function_exists('\\error_log')) {
120-
\error_log('Error in fallback taxonomy registration: '.$e->getMessage());
121-
}
12294
}
123-
} elseif (function_exists('\\error_log')) {
124-
\error_log('Cannot register taxonomy: register_taxonomy function not available');
12595
}
12696
}
12797
}

src/Application/Service/EntityRegistrationService.php

Lines changed: 6 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -40,43 +40,21 @@ public function __construct(EntityRegistryPort $entityRegistry)
4040
*/
4141
public function registerEntity(Entity $entity): void
4242
{
43-
// Check if WordPress functions are available
44-
if (! function_exists('\\add_action')) {
45-
if (function_exists('\\error_log')) {
46-
\error_log('Cannot register entity: add_action function not available');
47-
}
48-
49-
return;
50-
}
51-
5243
// Hook the actual registration to WordPress init hook
5344
\add_action('init', function () use ($entity) {
5445
$entityType = $entity->getEntity() ?? 'unknown';
5546
$slug = $entity->getSlug() ?? 'unknown';
5647

57-
if (function_exists('\\error_log')) {
58-
\error_log("WordPress init hook triggered for: {$entityType} - {$slug}");
59-
}
48+
$args = $entity->getArgs();
6049

61-
try {
62-
$args = $entity->buildArguments();
63-
$args['names'] = $entity->getNames();
50+
$args['names'] = $entity->getNames();
6451

65-
$args = $entity->translateArguments($args, $entity->getEntity());
66-
$names = $args['names'] ?? [];
67-
unset($args['names']);
52+
$args = $entity->translateArguments($args, $entity->getEntity());
53+
$names = $args['names'] ?? [];
54+
unset($args['names']);
6855

69-
if (function_exists('\\error_log')) {
70-
\error_log('Registration args: '.json_encode($args));
71-
\error_log('Registration names: '.json_encode($names));
72-
}
56+
$this->entityRegistry->register($entity->getSlug(), $args, $names);
7357

74-
$this->entityRegistry->register($entity->getSlug(), $args, $names);
75-
} catch (\Exception $e) {
76-
if (function_exists('\\error_log')) {
77-
\error_log('Error during entity registration: '.$e->getMessage());
78-
}
79-
}
8058
}, 99);
8159
}
8260
}

src/Application/Service/PostTypeService.php

Lines changed: 10 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -7,58 +7,30 @@
77
use Pollora\Entity\Domain\Model\PostType;
88
use Pollora\Entity\Port\Out\PostTypeRegistryPort;
99

10-
/**
11-
* Service class for managing post types.
12-
*
13-
* This service provides methods to register post types with WordPress.
14-
*/
1510
class PostTypeService
1611
{
1712
private PostTypeRegistryPort $postTypeRegistry;
1813

19-
private EntityRegistrationService $registrationService;
14+
private EntityRegistrationService $entityRegistrationService;
2015

21-
/**
22-
* PostTypeService constructor.
23-
*
24-
* @param PostTypeRegistryPort $postTypeRegistry The post type registry port implementation
25-
* @param EntityRegistrationService $registrationService The entity registration service
26-
*/
2716
public function __construct(
2817
PostTypeRegistryPort $postTypeRegistry,
29-
EntityRegistrationService $registrationService
18+
EntityRegistrationService $entityRegistrationService
3019
) {
3120
$this->postTypeRegistry = $postTypeRegistry;
32-
$this->registrationService = $registrationService;
21+
$this->entityRegistrationService = $entityRegistrationService;
3322
}
3423

35-
/**
36-
* Register a post type with WordPress.
37-
*
38-
* This method registers a post type with WordPress through the registration service.
39-
*
40-
* @param PostType $postType The post type to register
41-
*/
42-
public function register(PostType $postType): void
24+
public function createPostType(string $slug, string $singular, string $plural): PostType
4325
{
44-
$this->registrationService->registerEntity($postType);
26+
$postType = new PostType($slug, $singular, $plural);
27+
$this->entityRegistrationService->registerEntity($postType);
28+
29+
return $postType;
4530
}
4631

47-
/**
48-
* Create and register a new post type.
49-
*
50-
* This method creates a new post type and registers it with WordPress.
51-
*
52-
* @param string $slug The post type slug
53-
* @param string|null $singular The singular label for the post type
54-
* @param string|null $plural The plural label for the post type
55-
* @return PostType The created post type
56-
*/
57-
public function createPostType(string $slug, ?string $singular = null, ?string $plural = null): PostType
32+
public function register(PostType $postType): void
5833
{
59-
$postType = PostType::make($slug, $singular, $plural);
60-
$this->register($postType);
61-
62-
return $postType;
34+
$this->entityRegistrationService->registerEntity($postType);
6335
}
6436
}

src/Domain/Model/Entity.php

Lines changed: 38 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
namespace Pollora\Entity\Domain\Model;
66

7-
use Pollora\Entity\Shared\Traits\ArgumentTranslater;
7+
use Pollora\WordPressArgs\ArgumentHelper;
88

99
/**
1010
* The Entity class is an abstract model that provides common functionality
@@ -14,7 +14,7 @@
1414
*/
1515
abstract class Entity
1616
{
17-
use ArgumentTranslater;
17+
use ArgumentHelper;
1818

1919
/**
2020
* Name of the post type or taxonomy shown in the menu. Usually plural.
@@ -164,22 +164,11 @@ abstract class Entity
164164
*/
165165
public $names;
166166

167-
/**
168-
* Static collection to store entity instances to prevent garbage collection
169-
* before registration happens.
170-
*/
171-
protected static array $instances = [];
172-
173167
/**
174168
* The entity name, used for registration.
175169
*/
176170
protected string $entity = '';
177171

178-
/**
179-
* Raw arguments that will be passed directly to WordPress.
180-
*/
181-
protected array $rawArgs = [];
182-
183172
/**
184173
* Retrieves the label for the entity.
185174
*
@@ -766,70 +755,6 @@ public function setAdminCols(array $adminCols): self
766755
return $this;
767756
}
768757

769-
/**
770-
* Build arguments for entity registration.
771-
*
772-
* @return array Array of arguments to be used for entity registration
773-
*/
774-
public function buildArguments(): array
775-
{
776-
// Collect all properties that should be used in arguments
777-
$reflection = new \ReflectionClass($this);
778-
$properties = $reflection->getProperties(\ReflectionProperty::IS_PUBLIC);
779-
780-
$args = [];
781-
foreach ($properties as $property) {
782-
// Skip properties that should not be in arguments
783-
if (in_array($property->getName(), ['slug', 'singular', 'plural', 'names'])) {
784-
continue;
785-
}
786-
787-
// Only include non-null properties
788-
$name = $property->getName();
789-
if (isset($this->$name)) {
790-
$args[$name] = $this->$name;
791-
}
792-
}
793-
794-
// For taxonomies, include the objectType in the args
795-
if ($this->getEntity() === 'taxonomies' && $this instanceof \Pollora\Entity\Domain\Model\Taxonomy) {
796-
$args['object_type'] = $this->getObjectType();
797-
}
798-
799-
// Merge with raw arguments if any are set
800-
if (! empty($this->rawArgs)) {
801-
$args = array_merge($args, $this->rawArgs);
802-
}
803-
804-
return $args;
805-
}
806-
807-
/**
808-
* Sets raw arguments to be passed directly to WordPress.
809-
*
810-
* This method allows you to specify arguments that will be merged with
811-
* automatically generated arguments during registration.
812-
*
813-
* @param array $args Raw arguments to pass to WordPress registration functions
814-
* @return self Returns this object instance.
815-
*/
816-
public function setRawArgs(array $args): self
817-
{
818-
$this->rawArgs = $args;
819-
820-
return $this;
821-
}
822-
823-
/**
824-
* Gets the raw arguments.
825-
*
826-
* @return array Array of raw arguments
827-
*/
828-
public function getRawArgs(): array
829-
{
830-
return $this->rawArgs;
831-
}
832-
833758
/**
834759
* Gets the entity type name.
835760
*
@@ -850,6 +775,32 @@ public function getSlug(): string
850775
return $this->names['slug'] ?? '';
851776
}
852777

778+
/**
779+
* Translate the arguments using the given entity and keys.
780+
*
781+
* This is a stub implementation that returns arguments unchanged.
782+
* Override this method in subclasses if translation is needed.
783+
*
784+
* @param array<string, mixed> $args The arguments to be translated
785+
* @param string $entity The translation domain/entity to use
786+
* @param array<int, string> $keyToTranslate The keys to be translated
787+
* @return array<string, mixed> The arguments (unchanged in this implementation)
788+
*/
789+
public function translateArguments(
790+
array $args,
791+
string $entity,
792+
array $keyToTranslate = [
793+
'label',
794+
'labels.*',
795+
'names.singular',
796+
'names.plural',
797+
]
798+
): array {
799+
// Stub implementation - returns arguments unchanged
800+
// This can be overridden in subclasses if translation is needed
801+
return $args;
802+
}
803+
853804
/**
854805
* Initializes the object by setting its singular and plural forms.
855806
*
@@ -858,4 +809,14 @@ public function getSlug(): string
858809
* @return void
859810
*/
860811
abstract public function init();
812+
813+
/**
814+
* Get the arguments for the entity.
815+
*
816+
* @return array|null The arguments for the entity.
817+
*/
818+
public function getArgs(): ?array
819+
{
820+
return $this->buildArguments();
821+
}
861822
}

0 commit comments

Comments
 (0)