Skip to content

Commit 3ed89f7

Browse files
authored
Merge pull request #2 from factorial-io/refactor/make-it-dynamic
feat: implement dynamic entity system with code generation (v1.0)
2 parents a2366b9 + 6f1a1e3 commit 3ed89f7

File tree

116 files changed

+17953
-4218
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

116 files changed

+17953
-4218
lines changed

.github/workflows/code-quality.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ jobs:
1212

1313
strategy:
1414
matrix:
15-
php-version: ['8.1', '8.2', '8.3', '8.4']
15+
php-version: ['8.2', '8.3', '8.4']
1616

1717
steps:
1818
- uses: actions/checkout@v4

.github/workflows/release.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ jobs:
2020
- name: Setup PHP
2121
uses: shivammathur/setup-php@v2
2222
with:
23-
php-version: '8.1'
23+
php-version: '8.2'
2424
extensions: mbstring, json
2525
coverage: none
2626

.gitignore

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22
.env
33
.env.local
44

5+
# Code Generation Configuration (may contain API tokens)
6+
.twenty-codegen.yml
7+
.twenty-codegen.yaml
8+
59
# PHPUnit
610
phpunit.xml
711
.phpunit.result.cache
@@ -28,3 +32,7 @@ composer.lock
2832
# OS
2933
.DS_Store
3034
Thumbs.db
35+
36+
# Twenty CRM OpenAPI specs (instance-specific)
37+
docs/core.json
38+
docs/metadata.json

.twenty-codegen.example.yml

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# Twenty CRM Code Generation Configuration
2+
# Copy this file to .twenty-codegen.yml and customize for your project
3+
#
4+
# Usage: vendor/bin/twenty-generate --config=.twenty-codegen.yml
5+
6+
# Base namespace for generated code
7+
# Entity, Service, and Collection subdirectories will be created automatically
8+
namespace: MyApp\TwentyCrm
9+
10+
# Output directory for generated code
11+
# Entity/, Service/, and Collection/ subdirectories will be created here
12+
output_dir: src/TwentyCrm
13+
14+
# Twenty CRM API configuration
15+
# You can use ${VAR_NAME} syntax to reference environment variables
16+
api_url: ${TWENTY_API_BASE_URI}
17+
api_token: ${TWENTY_API_TOKEN}
18+
19+
# Alternative: Hardcode values (not recommended for production)
20+
# api_url: https://your-instance.twenty.com/rest/
21+
# api_token: your_api_token_here
22+
23+
# List of entities to generate
24+
# These should match the entity names in your Twenty CRM instance
25+
# Common entities: person, company, opportunity, note, task, etc.
26+
entities:
27+
- person
28+
- company
29+
- opportunity
30+
31+
# Code generation options
32+
options:
33+
# Generate service classes with CRUD operations
34+
# Creates: PersonService, CompanyService, etc.
35+
generate_services: true
36+
37+
# Generate typed collection classes
38+
# Creates: PersonCollection, CompanyCollection, etc.
39+
generate_collections: true
40+
41+
# Overwrite existing files during generation
42+
# Default: false (will fail if files already exist)
43+
# Use --overwrite flag to override this setting
44+
overwrite: false

DEVELOPMENT.md

Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
# Development Guide
2+
3+
## Repository Structure
4+
5+
This repository contains two distinct packages with clear separation of concerns:
6+
7+
### 1. Core Library (`/`)
8+
9+
The main library providing tools and runtime support for Twenty CRM integration:
10+
11+
- **Location:** Root directory
12+
- **Package:** `factorial-io/twenty-crm-php-client`
13+
- **Purpose:** Generic, schema-agnostic tools
14+
- **Contains:**
15+
- DynamicEntity system
16+
- Code generation CLI (`bin/twenty-generate`)
17+
- Metadata discovery
18+
- Entity relations
19+
- HTTP client infrastructure
20+
- **Tests:** Unit tests only (`tests/Unit/`)
21+
22+
### 2. Usage Example (`/usage-example/`)
23+
24+
Example implementation with generated entities demonstrating the library:
25+
26+
- **Location:** `usage-example/` subdirectory
27+
- **Package:** `factorial-io/twenty-crm-entities` (example, not published)
28+
- **Purpose:** Usage examples, schema-specific entities, and integration tests
29+
- **Contains:**
30+
- Generated Person, Company, Campaign entities
31+
- Integration tests demonstrating library features
32+
- Test helpers and factories
33+
- Example usage patterns
34+
- **Tests:** Integration tests (`usage-example/tests/Integration/`)
35+
36+
## Separation of Concerns
37+
38+
| Concern | Core Library | Usage Example |
39+
|---------|--------------|---------------|
40+
| Generic tools |||
41+
| DynamicEntity || Uses |
42+
| Code generator || Uses |
43+
| Generated entities |||
44+
| Integration tests |||
45+
| Schema-specific code |||
46+
| Unit tests |||
47+
48+
## Running Tests
49+
50+
### Core Library (Unit Tests)
51+
52+
```bash
53+
# Run all unit tests
54+
vendor/bin/phpunit
55+
56+
# Run with coverage
57+
vendor/bin/phpunit --coverage-html coverage/
58+
```
59+
60+
### Usage Example (Integration Tests)
61+
62+
```bash
63+
# Setup
64+
cd usage-example
65+
cp .env.example .env
66+
# Edit .env with your credentials
67+
68+
# Run integration tests
69+
cd usage-example
70+
../vendor/bin/phpunit
71+
```
72+
73+
## Development Workflow
74+
75+
### Working on Core Library
76+
77+
1. Make changes in `src/`
78+
2. Add unit tests in `tests/Unit/`
79+
3. Run `vendor/bin/phpunit`
80+
4. Ensure no entity-specific code
81+
82+
### Working on Usage Example
83+
84+
1. Generate entities: `vendor/bin/twenty-generate --config=usage-example/.twenty-codegen.yml`
85+
2. Add integration tests in `usage-example/tests/Integration/`
86+
3. Run `cd usage-example && ../vendor/bin/phpunit`
87+
4. Commit generated code
88+
89+
## Usage Example as Template
90+
91+
The `usage-example/` directory serves multiple purposes:
92+
93+
1. **Documentation:** Shows how to use the library with real code
94+
2. **Testing:** Integration tests validate library features against a live Twenty CRM instance
95+
3. **Code Generation Example:** Demonstrates the code generation workflow
96+
4. **Template:** Can be copied/adapted for your own Twenty CRM instance
97+
98+
## Why This Structure?
99+
100+
1. **Flexibility:** Users can use core library with any Twenty instance
101+
2. **Type Safety:** Generated typed entities with IDE support
102+
3. **Clean Separation:** No instance-specific code in core library
103+
4. **Living Documentation:** Real, tested examples of library usage
104+
5. **Example Code:** Shows how to generate and use entities
105+
106+
## Code Generation
107+
108+
Generate entities using the example configuration:
109+
110+
```bash
111+
# Using config file
112+
vendor/bin/twenty-generate --config=usage-example/.twenty-codegen.yml
113+
114+
# Or with CLI args
115+
vendor/bin/twenty-generate \
116+
--api-url=https://your-instance.twenty.com/rest/ \
117+
--api-token=$TWENTY_TOKEN \
118+
--namespace="Factorial\\TwentyCrm\\Entities" \
119+
--output=usage-example/src \
120+
--entities=person,company,campaign
121+
```
122+
123+
## Quality Checks
124+
125+
```bash
126+
# PHP CodeSniffer
127+
vendor/bin/phpcs
128+
129+
# PHP CS Fixer
130+
vendor/bin/php-cs-fixer fix --dry-run --diff
131+
132+
# PHPStan
133+
vendor/bin/phpstan analyse src
134+
```

0 commit comments

Comments
 (0)