draw/core
Core utilities for Draw components/bundles. Provides reusable shared code and automatically registers missing Draw-namespaced Doctrine annotations as ignored to prevent annotation reader errors when optional annotation libraries aren’t installed.
Install the Package Require the package via Composer in your Laravel project:
composer require draw/core
Register the Service Provider
Add the DrawCoreServiceProvider to your config/app.php under the providers array:
'providers' => [
// ...
Draw\Core\Providers\DrawCoreServiceProvider::class,
],
Configure Ignored Annotations
Create or update config/draw.php to specify annotations to ignore:
return [
'ignored_annotations' => [
'Some\NonDraw\Annotation\Route',
'Another\Namespace\ParamConverter',
],
];
(Alternatively, use the provided ignore_annotations.php example as a reference.)
First Use Case: Resolve Annotation Errors
If your Laravel app uses Doctrine’s AnnotationReader and encounters errors like:
Doctrine\Common\Annotations\AnnotationException: [Semantical Error] The annotation "@Route" in class ...
The package will automatically ignore draw-namespaced or configured annotations, eliminating the error.
Annotation-Heavy Projects
draw/core to centralize ignored annotations in config/draw.php.// app/Providers/AppServiceProvider.php
use Draw\Core\AnnotationIgnorer;
public function boot() {
AnnotationIgnorer::ignore(config('draw.ignored_annotations'));
}
Modular Component Libraries
draw/core across multiple Laravel packages to avoid duplicate annotation configurations.// In a shared package's composer.json
"require": {
"draw/core": "^1.0"
}
Dynamic Annotation Handling
$ignored = cache()->remember('draw.ignored_annotations', now()->addHours(1), function() {
return config('draw.ignored_annotations');
});
AnnotationIgnorer::ignore($ignored);
Doctrine Integration
Bind the AnnotationReader in Laravel’s service container to use the ignorer:
// app/Providers/DrawCoreServiceProvider.php
use Doctrine\Common\Annotations\AnnotationReader;
public function register() {
$this->app->singleton(AnnotationReader::class, function($app) {
$reader = new AnnotationReader();
$reader->addIgnoredNamespace(config('draw.ignored_annotations'));
return $reader;
});
}
Laravel Artisan Commands Use the package to validate annotations during command execution:
use Draw\Core\AnnotationValidator;
public function handle() {
if (!AnnotationValidator::validate($this->getAnnotations())) {
$this->error('Invalid annotations detected.');
}
}
Testing
Mock the AnnotationIgnorer in PHPUnit tests to simulate ignored annotations:
$ignorer = $this->createMock(AnnotationIgnorer::class);
$ignorer->method('ignore')->willReturn(true);
$this->app->instance(AnnotationIgnorer::class, $ignorer);
False Ignores
*) may suppress valid annotations.config/draw.php.Doctrine Version Conflicts
draw/core may not support newer doctrine/annotations versions.composer.json:
"require": {
"doctrine/annotations": "^1.13"
}
Circular Dependencies
draw/core is used in a package that also depends on Doctrine, circular requires may occur.composer.json replace or conflict directives.Annotation Reader Initialization
AnnotationReader is instantiated.DrawCoreServiceProvider is loaded early in Laravel’s boot process.Enable Logging Add debug logs for ignored annotations:
AnnotationIgnorer::setLogger(function($message) {
\Log::debug('[Draw/Core]', [$message]);
});
Validate Config Use Artisan commands to verify ignored annotations:
// In a custom Artisan command
public function handle() {
$ignored = config('draw.ignored_annotations');
foreach ($ignored as $annotation) {
if (!class_exists($annotation)) {
$this->error("Annotation {$annotation} does not exist.");
}
}
}
Custom Ignore Logic
Extend the AnnotationIgnorer to add dynamic rules:
class CustomAnnotationIgnorer extends \Draw\Core\AnnotationIgnorer {
public function ignore($annotations) {
// Add custom logic (e.g., ignore based on environment)
parent::ignore($annotations);
}
}
Annotation Validation Implement a validator to check annotations against business rules:
use Draw\Core\AnnotationValidator;
class MyValidator extends AnnotationValidator {
protected function rules() {
return [
'Some\Annotation' => ['method' => 'validateMethod'],
];
}
}
Integration with Laravel Events Trigger events when annotations are ignored:
AnnotationIgnorer::onIgnore(function($annotation) {
event(new AnnotationIgnored($annotation));
});
Environment-Specific Ignores
Override config/draw.php per environment (e.g., config/draw-local.php):
'ignored_annotations' => env('DRAW_IGNORED_ANNOTATIONS', []),
Namespacing
Ensure ignored annotations use fully qualified class names (e.g., \App\Annotations\Route instead of Route).
Performance
Avoid ignoring too many annotations, as each ignored annotation adds a small overhead to the AnnotationReader.
How can I help you explore Laravel packages today?