{"id":18285499,"url":"https://github.com/bemit/orbiter-annotations-util","last_synced_at":"2025-07-07T06:08:26.516Z","repository":{"id":57032765,"uuid":"223274951","full_name":"bemit/orbiter-annotations-util","owner":"bemit","description":"Helper for Doctrine\\Annotations, with cached Reflection ⚡","archived":false,"fork":false,"pushed_at":"2022-10-20T18:26:09.000Z","size":61,"stargazers_count":2,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-03-21T00:33:04.347Z","etag":null,"topics":["annotation","cache","doctrine","php","reflection"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/bemit.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2019-11-21T22:13:28.000Z","updated_at":"2023-01-23T10:07:47.000Z","dependencies_parsed_at":"2022-08-24T05:00:45.337Z","dependency_job_id":null,"html_url":"https://github.com/bemit/orbiter-annotations-util","commit_stats":null,"previous_names":[],"tags_count":10,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bemit%2Forbiter-annotations-util","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bemit%2Forbiter-annotations-util/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bemit%2Forbiter-annotations-util/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bemit%2Forbiter-annotations-util/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/bemit","download_url":"https://codeload.github.com/bemit/orbiter-annotations-util/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247305879,"owners_count":20917201,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["annotation","cache","doctrine","php","reflection"],"created_at":"2024-11-05T13:16:52.645Z","updated_at":"2025-04-05T07:32:17.456Z","avatar_url":"https://github.com/bemit.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Orbiter\\AnnotationUtil\n\nHelper utility for Doctrine\\Annotations, uses cached reflection by [scaleupstack/reflection](https://packagist.org/packages/scaleupstack/reflection).\n\nIncludes CodeInfo for static code analyzes, for easier, automatic parsing of Annotations and e.g. setup of DI.\n\n- [AnnotationsUtil](#annotationsutil)\n- [Example Annotation](#example-annotation)\n- [CodeInfo](#code-info---a-file-content-parsing-helper)\n- [License](#license)\n\nInstall with composer:\n\n    composer require orbiter/annotations-util\n\nSee [doctrine docs](https://www.doctrine-project.org/projects/annotations.html) for details on what Annotations are and complexer Examples.\n\n## AnnotationsUtil\n\n```php\n\u003c?php\nuse Orbiter\\AnnotationsUtil\\AnnotationUtil;\nuse Orbiter\\AnnotationsUtil\\CachedReflection;\n\n// Add PSR-4 Annotation Namespaces\nAnnotationUtil::registerPsr4Namespace('Lib', __DIR__ . '/lib');\n\n// uses only cached Reflection, without annotations\nCachedReflection::getClass(App\\Foo::class): ReflectionClass;\nCachedReflection::getProperty(App\\Foo::class, 'bar'): ReflectionProperty;\nCachedReflection::getMethod(App\\Foo::class, 'foo'): ReflectionMethod;\n\nCachedReflection::setPropertyValue(App\\Foo::class, 'bar');\nCachedReflection::setStaticPropertyValue(App\\Foo::class, 'bar_tastic');\n\nCachedReflection::invokeMethod(App\\Foo::class, 'fooTastic');\nCachedReflection::invokeStaticMethod(App\\Foo::class, 'foo');\n\n// Use normal doctrine where needed\nDoctrine\\Common\\Annotations\\AnnotationReader::addGlobalIgnoredName('dummy');\n\n// setup reader with cached reflections\n$annotation_reader_cached = new Orbiter\\AnnotationsUtil\\AnnotationReader(\n    new Doctrine\\Common\\Annotations\\IndexedReader(\n        new Doctrine\\Common\\Annotations\\AnnotationReader()\n    )\n);\n\n// Get Annotations\n$annotation_reader_cached-\u003egetClassAnnotations(App\\Foo::class): array;// of annotations\n$annotation_reader_cached-\u003egetClassAnnotation(App\\Foo::class, Lib\\MyAnnotation::class): Lib\\MyAnnotation;\n\n$annotation_reader_cached-\u003egetPropertyAnnotations(App\\Foo::class, 'bar'): array;// of annotations\n$annotation_reader_cached-\u003egetPropertyAnnotation(App\\Foo::class, 'bar', Lib\\MyAnnotation::class): Lib\\MyAnnotation;\necho $annotation_reader_cached-\u003egetPropertyAnnotation(App\\Foo::class, 'bar', Lib\\MyAnnotation::class)-\u003emyProperty;\n\n$annotation_reader_cached-\u003egetMethodAnnotations(App\\Foo::class, 'foo'): array;// of annotations\n$annotation_reader_cached-\u003egetMethodAnnotation(App\\Foo::class, 'foo', Lib\\MyAnnotation::class): Lib\\MyAnnotation;\necho $annotation_reader_cached-\u003egetMethodAnnotation(App\\Foo::class, 'foo', Lib\\MyAnnotation::class)-\u003emyProperty;\n\n\n// Setup code inspection / class discovery:\n\n$code_info = new Orbiter\\AnnotationsUtil\\CodeInfo();\nif(getenv('env') === 'prod') {\n    // absolute file to cache, if cache exists, it will not be re-freshed.\n    // Delete the file for a new cache\n    $code_info-\u003eenableFileCache(__DIR__ . '/tmp/codeinfo.cache');\n}\n\n// Add a dirs to scan, use `flag` for grouping different folders in the result\n$code_info-\u003edefineSource(\n    new CodeInfoSource(\n        __DIR__ . '/app',\n        ['FLAG_APP'],\n        ['php'],\n    ),\n);\n\n// parse defined folders\n$code_info-\u003eprocess();\n\n// Get the discovery for annotations:\n$discovery = new Orbiter\\AnnotationsUtil\\AnnotationDiscovery($annotation_reader_cached);\n\n$discovery-\u003ediscoverByAnnotation(\n    $code_info-\u003egetClassNames('FLAG_APP'),\n);\n\n$results = $discovery-\u003egetDiscovered(Satellite\\KernelRoute\\Annotations\\Route::class);\nforeach($results as $result) {\n    $result-\u003egetAnnotation();\n}\n```\n\n### Example Annotation\n\nDefine your annotation, remember to specify it in the Annotation loaders - normal **autoloading doesn't work** for classes using `@Annotation`!\n\n```php\n\u003c?php\nnamespace Lib;\n\nuse Doctrine\\Common\\Annotations\\Annotation;\n\n/**\n * @Annotation\n */\nfinal class MyAnnotation {\n    public $myProperty;\n}\n```\n\nUsing an annotation in a class:\n\n```php\n\u003c?php\nnamespace App;\n\nuse Lib\\MyAnnotation;\n\nclass Foo {\n    /**\n     * @MyAnnotation(myProperty=\"demo-value\")\n     */\n    private $bar;\n}\n```\n\nGet value of the Annotation:\n\n```php\n\u003c?php\n\n// this prints \"demo-value\"\necho $annotation_reader_cached-\u003egetMethodAnnotation(App\\Foo::class, 'bar', Lib\\MyAnnotation::class)-\u003emyProperty;\n/**\n * @var Lib\\MyAnnotation $annotation this variable is the Annotation instance and contains also it's data\n */\n$annotation = $annotation_reader_cached-\u003egetMethodAnnotation(App\\Foo::class, 'bar', Lib\\MyAnnotation::class);\n// this is the recommended way to use the properties\necho $annotation-\u003emyProperty;\n```\n\nThis example is a summarized version, using this utility to read in the end, from [Doctrine\\Annotations: Create Annotations](https://www.doctrine-project.org/projects/doctrine-annotations/en/1.6/index.html#introduction).\n\n## Take a Look\n\nWant to build console apps with dependency injection and annotations? Use this app skeleton: [elbakerino/console](https://github.com/elbakerino/console-di-annotations), powered by PHP-DI, uses Doctrine\\Annotations with this package.\n\nBuild event and middleware based apps with [Satellite](https://github.com/bemit/satellite-app), DI enabled and with annotations for cli commands and routings..\n\n## License\n\nThis project is free software distributed under the **MIT License**.\n\nSee: [LICENSE](LICENSE).\n\n### Contributors\n\nBy committing your code to the code repository you agree to release the code under the MIT License attached to the repository without the expectation of consideration.\n\n***\n\nAuthor: [Michael Becker](https://i-am-digital.eu)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbemit%2Forbiter-annotations-util","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbemit%2Forbiter-annotations-util","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbemit%2Forbiter-annotations-util/lists"}