|
| 1 | +# Migrating from Doctrine Annotations to PHP 8 Attributes |
| 2 | + |
| 3 | +## Overview |
| 4 | + |
| 5 | +Ray.AuraSqlModule v2.x has removed the dependency on `doctrine/annotations` and now exclusively uses native PHP 8 attributes. This guide will help you migrate your application code from Doctrine annotations to PHP 8 attributes. |
| 6 | + |
| 7 | +## Why Migrate? |
| 8 | + |
| 9 | +- **doctrine/annotations is Abandoned**: The `doctrine/annotations` package has been officially abandoned by its maintainers. Continuing to use it poses security and compatibility risks as it will no longer receive updates or security patches. |
| 10 | +- **Native PHP Support**: PHP 8 attributes are a built-in language feature, providing first-class support without external dependencies |
| 11 | +- **Better Performance**: No runtime annotation parsing overhead - attributes are compiled and cached by PHP itself |
| 12 | +- **Modern Syntax**: Cleaner, more readable code that follows PHP 8+ best practices |
| 13 | +- **No Extra Dependencies**: Eliminates the need for the abandoned doctrine/annotations package |
| 14 | +- **Future-Proof**: Attributes are the official PHP standard for metadata, ensuring long-term compatibility |
| 15 | + |
| 16 | +## Migration Steps |
| 17 | + |
| 18 | +### Step 1: Update Ray.AuraSqlModule |
| 19 | + |
| 20 | +First, ensure you're using Ray.AuraSqlModule v2.x: |
| 21 | + |
| 22 | +```bash |
| 23 | +composer require ray/aura-sql-module:^2.0 |
| 24 | +``` |
| 25 | + |
| 26 | +### Step 2: Install Rector (if not already installed) |
| 27 | + |
| 28 | +Rector is an automated refactoring tool that can convert annotations to attributes: |
| 29 | + |
| 30 | +```bash |
| 31 | +composer require --dev rector/rector |
| 32 | +``` |
| 33 | + |
| 34 | +### Step 3: Run Automated Migration |
| 35 | + |
| 36 | +Ray.AuraSqlModule provides a Rector configuration file for automated migration: |
| 37 | + |
| 38 | +```bash |
| 39 | +# Dry-run to preview changes |
| 40 | +vendor/bin/rector process src --config=vendor/ray/aura-sql-module/rector-migrate.php --dry-run |
| 41 | + |
| 42 | +# Apply the changes |
| 43 | +vendor/bin/rector process src --config=vendor/ray/aura-sql-module/rector-migrate.php |
| 44 | +``` |
| 45 | + |
| 46 | +If you have tests that use annotations: |
| 47 | + |
| 48 | +```bash |
| 49 | +vendor/bin/rector process tests --config=vendor/ray/aura-sql-module/rector-migrate.php |
| 50 | +``` |
| 51 | + |
| 52 | +### Step 4: Manual Review |
| 53 | + |
| 54 | +Review the changes made by Rector and adjust if necessary. Pay special attention to: |
| 55 | + |
| 56 | +- Multi-line annotations with complex values |
| 57 | +- Annotations with custom parameters |
| 58 | +- Import statements (Rector should handle these automatically) |
| 59 | + |
| 60 | +### Step 5: Remove doctrine/annotations |
| 61 | + |
| 62 | +After migration, you can safely remove the doctrine/annotations dependency: |
| 63 | + |
| 64 | +```bash |
| 65 | +composer remove doctrine/annotations |
| 66 | +``` |
| 67 | + |
| 68 | +## Before and After Examples |
| 69 | + |
| 70 | +### Transactional Annotation |
| 71 | + |
| 72 | +**Before (Doctrine Annotation):** |
| 73 | +```php |
| 74 | +use Ray\AuraSqlModule\Annotation\Transactional; |
| 75 | +use Ray\AuraSqlModule\Annotation\WriteConnection; |
| 76 | + |
| 77 | +class UserRepository |
| 78 | +{ |
| 79 | + /** |
| 80 | + * @WriteConnection |
| 81 | + * @Transactional |
| 82 | + */ |
| 83 | + public function save(User $user): void |
| 84 | + { |
| 85 | + // ... |
| 86 | + } |
| 87 | +} |
| 88 | +``` |
| 89 | + |
| 90 | +**After (PHP 8 Attribute):** |
| 91 | +```php |
| 92 | +use Ray\AuraSqlModule\Annotation\Transactional; |
| 93 | +use Ray\AuraSqlModule\Annotation\WriteConnection; |
| 94 | + |
| 95 | +class UserRepository |
| 96 | +{ |
| 97 | + #[WriteConnection, Transactional] |
| 98 | + public function save(User $user): void |
| 99 | + { |
| 100 | + // ... |
| 101 | + } |
| 102 | +} |
| 103 | +``` |
| 104 | + |
| 105 | +### Read-Only Connection |
| 106 | + |
| 107 | +**Before (Doctrine Annotation):** |
| 108 | +```php |
| 109 | +use Aura\Sql\ExtendedPdoInterface; |
| 110 | +use Ray\AuraSqlModule\Annotation\ReadOnlyConnection; |
| 111 | + |
| 112 | +class UserFinder |
| 113 | +{ |
| 114 | + /** |
| 115 | + * @ReadOnlyConnection |
| 116 | + */ |
| 117 | + public function __construct( |
| 118 | + private ExtendedPdoInterface $pdo |
| 119 | + ) {} |
| 120 | +} |
| 121 | +``` |
| 122 | + |
| 123 | +**After (PHP 8 Attribute):** |
| 124 | +```php |
| 125 | +use Aura\Sql\ExtendedPdoInterface; |
| 126 | +use Ray\AuraSqlModule\Annotation\ReadOnlyConnection; |
| 127 | + |
| 128 | +class UserFinder |
| 129 | +{ |
| 130 | + #[ReadOnlyConnection] |
| 131 | + public function __construct( |
| 132 | + private ExtendedPdoInterface $pdo |
| 133 | + ) {} |
| 134 | +} |
| 135 | +``` |
| 136 | + |
| 137 | +## Supported Annotations |
| 138 | + |
| 139 | +The following Ray.AuraSqlModule annotations are automatically converted: |
| 140 | + |
| 141 | +- `@Transactional` → `#[Transactional]` |
| 142 | +- `@WriteConnection` → `#[WriteConnection]` |
| 143 | +- `@ReadOnlyConnection` → `#[ReadOnlyConnection]` |
| 144 | +- `@Read` → `#[Read]` |
| 145 | +- `@AuraSql` → `#[AuraSql]` |
| 146 | +- `@EnvAuth` → `#[EnvAuth]` |
| 147 | +- `@HttpMethod` → `#[HttpMethod]` |
| 148 | +- `@PagerViewOption` → `#[PagerViewOption]` |
| 149 | +- `@AuraSqlQueryConfig` → `#[AuraSqlQueryConfig]` |
| 150 | + |
| 151 | +**Note**: This migration tool only handles Ray.AuraSqlModule annotations. For Ray.Di annotations (such as `@Inject`, `@Named`, etc.), please refer to the [Ray.Di migration guide](https://github.com/ray-di/Ray.Di). |
| 152 | + |
| 153 | +## Troubleshooting |
| 154 | + |
| 155 | +### Rector doesn't find annotations |
| 156 | + |
| 157 | +Make sure your code is using fully qualified class names in `use` statements: |
| 158 | + |
| 159 | +```php |
| 160 | +// Correct |
| 161 | +use Ray\AuraSqlModule\Annotation\Transactional; |
| 162 | + |
| 163 | +// Incorrect - Rector won't recognize this |
| 164 | +use Ray\AuraSqlModule\Annotation as DB; |
| 165 | +``` |
| 166 | + |
| 167 | +### Import statements not updated |
| 168 | + |
| 169 | +Rector should automatically update import statements, but if it doesn't: |
| 170 | + |
| 171 | +1. Manually verify `use` statements are present |
| 172 | +2. Run your IDE's "Optimize Imports" feature |
| 173 | +3. Use tools like PHP-CS-Fixer to clean up unused imports |
| 174 | + |
| 175 | +### Complex annotation values |
| 176 | + |
| 177 | +For annotations with complex array values, you may need to manually adjust the syntax: |
| 178 | + |
| 179 | +**Before:** |
| 180 | +```php |
| 181 | +/** |
| 182 | + * @Transactional({"pdo1", "pdo2"}) |
| 183 | + */ |
| 184 | +``` |
| 185 | + |
| 186 | +**After:** |
| 187 | +```php |
| 188 | +#[Transactional(["pdo1", "pdo2"])] |
| 189 | +``` |
| 190 | + |
| 191 | +### Testing the migration |
| 192 | + |
| 193 | +After migration, ensure all tests pass: |
| 194 | + |
| 195 | +```bash |
| 196 | +vendor/bin/phpunit |
| 197 | +``` |
| 198 | + |
| 199 | +## Manual Migration |
| 200 | + |
| 201 | +If you prefer not to use Rector, you can manually convert annotations: |
| 202 | + |
| 203 | +1. Replace `/** @AnnotationName */` with `#[AnnotationName]` |
| 204 | +2. Move attributes from docblocks to the line before the method/property/class |
| 205 | ++3. For parameter annotations, place the attribute before the parameter declaration |
| 206 | +4. Ensure all necessary `use` statements are present |
| 207 | + |
| 208 | +## Need Help? |
| 209 | + |
| 210 | +If you encounter issues during migration: |
| 211 | + |
| 212 | +1. Check the [Ray.AuraSqlModule documentation](https://github.com/ray-di/Ray.AuraSqlModule) |
| 213 | +2. Review the [PHP 8 Attributes documentation](https://www.php.net/manual/en/language.attributes.php) |
| 214 | +3. [Open an issue](https://github.com/ray-di/Ray.AuraSqlModule/issues) on GitHub |
| 215 | + |
| 216 | +## References |
| 217 | + |
| 218 | +- [PHP 8 Attributes RFC](https://wiki.php.net/rfc/attributes_v2) |
| 219 | +- [Rector Documentation](https://github.com/rectorphp/rector) |
| 220 | +- [Ray.Di Documentation](https://ray-di.github.io/manuals/1.0/en/) |
0 commit comments