{"id":19250247,"url":"https://github.com/charcoalphp/factory","last_synced_at":"2026-06-19T15:01:27.885Z","repository":{"id":37492319,"uuid":"505906176","full_name":"charcoalphp/factory","owner":"charcoalphp","description":"[READ-ONLY] Dynamic object creation (Factory/ AbstractFactory / Builder / Class Resolver)","archived":false,"fork":false,"pushed_at":"2024-03-13T15:05:12.000Z","size":898,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-10-05T07:48:55.269Z","etag":null,"topics":["builder-pattern","factory","factory-pattern","php","read-only-repository"],"latest_commit_sha":null,"homepage":"https://github.com/charcoalphp/charcoal","language":"PHP","has_issues":false,"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/charcoalphp.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,"governance":null,"roadmap":null,"authors":null,"dei":null}},"created_at":"2022-06-21T15:44:06.000Z","updated_at":"2022-06-21T19:18:45.000Z","dependencies_parsed_at":"2024-03-13T16:29:37.164Z","dependency_job_id":"056e7391-9262-4ad6-9e9b-0879223893f6","html_url":"https://github.com/charcoalphp/factory","commit_stats":{"total_commits":61,"total_committers":5,"mean_commits":12.2,"dds":"0.39344262295081966","last_synced_commit":"e2fdc3a8c292d42d505b134d6abe5221b4665fc8"},"previous_names":[],"tags_count":32,"template":false,"template_full_name":null,"purl":"pkg:github/charcoalphp/factory","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/charcoalphp%2Ffactory","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/charcoalphp%2Ffactory/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/charcoalphp%2Ffactory/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/charcoalphp%2Ffactory/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/charcoalphp","download_url":"https://codeload.github.com/charcoalphp/factory/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/charcoalphp%2Ffactory/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":34536283,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-05-26T15:22:16.424Z","status":"online","status_checked_at":"2026-06-19T02:00:06.005Z","response_time":61,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"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":["builder-pattern","factory","factory-pattern","php","read-only-repository"],"created_at":"2024-11-09T18:16:36.671Z","updated_at":"2026-06-19T15:01:27.853Z","avatar_url":"https://github.com/charcoalphp.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"Charcoal Factory\n================\n\nThe Factory package provides abstract object factories to create objects.\n\n## Installation\n\n```shell\ncomposer require charcoal/factory\n```\n\n## Usage\n\nFactories can resolve a _type_ to a FQN and create instance of this class with an optional given set of arguments, while ensuring a default base class.\n\nFactory options should be set directly from the constructor:\n\n```php\n$factory = new Charcoal\\Factory\\GenericFactory([\n    // Ensure the created object is a Charcoal Model\n    'base_class' =\u003e '\\Charcoal\\Model\\ModelInterface',\n\n    // An associative array of class map (aliases)\n    'map' =\u003e [\n        'foo' =\u003e '\\My\\Foo',\n        'bar' =\u003e '\\My\\Bar',\n    ],\n\n    // Constructor arguments\n    'arguments' =\u003e [\n        $dep1,\n        $dep2,\n    ],\n\n    // Object callback\n    'callback' =\u003e function ($obj) {\n        $obj-\u003edo('foo');\n    },\n]);\n\n// Create a \"\\My\\Custom\\Baz\" object with the given arguments + callbck\n$factory-\u003ecreate('\\My\\Custom\\Baz');\n\n// Create a \"\\My\\Foo\" object (using the map of aliases)\n$factory-\u003ecreate('foo');\n\n// Create a \"\\My\\Custom\\FooBar\" object with the default resolver\n$factory-\u003ecreate('my/custom/foo-bar');\n```\n\nConstructor options (class dependencies) are:\n\n| Name               | Type       | Default              | Description |\n| ------------------ | ---------- | -------------------- | ----------- |\n| `base_class`       | _string_   | `''`                 | Optional. A base class (or interface) to ensure a type of object.\n| `default_class`    | _string_   | `''`                 | Optional. A default class, as fallback when the requested object is not resolvable.\n| `arguments`        | _array_    | `[]`                 | Optional. Constructor arguments that will be passed along to created instances.\n| `callback`         | _callable_ | `null`               | Optional. A callback function that will be called upon object creation.\n| `resolver`         | _callable_ | `null`\u003csup\u003e[1]\u003c/sup\u003e | Optional. A class resolver. If none is provided, a default will be used.\n| `resolver_options` | _array_    | `null`               | Optional. Resolver options (prefix, suffix, capitals and replacements). This is ignored / unused if `resolver` is provided.\n\nNotes:\n\n* \u003csup\u003e[1]\u003c/sup\u003e If no resolver is provided, a default `\\Charcoal\\Factory\\GenericResolver` will be used.\n\n### Class resolver\n\nThe _type_ (class identifier) sent to the `create()` method can be parsed / resolved with a custom `Callable` resolver.\n\nIf no `resolver` is passed to the constructor, a default `\\Charcoal\\Factory\\GenericResolver` is used. This default resolver transforms, for example, `my/custom/foo-bar` into `\\My\\Custom\\FooBar`.\n\n### Class map and aliases\n\nClass _aliases_ can be added by setting them in the Factory constructor:\n\n```php\n$factory = new GenericFactory([\n    'map' =\u003e [\n        'foo' =\u003e '\\My\\Foo',\n        'bar' =\u003e '\\My\\Bar',\n    ],\n]);\n\n// Create a `\\My\\Foo` instance\n$obj = $factory-\u003ecreate('foo');\n```\n\n### Ensuring a type of object\n\nEnsuring a type of object can be done by setting the `base_class` property.\n\nThe recommended way of setting the base class is by setting it in the constructor:\n\n```php\n$factory = new GenericFactory([\n    'base_class' =\u003e '\\My\\Foo\\BaseClassInterface',\n]);\n```\n\n\u003e 👉 Note that _Interfaces_ can also be used as a factory's base class.\n\n### Setting a default type of object\n\nIt is possible to set a default type of object (default class) by setting the `default_class` property.\n\nThe recommended way of setting the default class is by setting it in the constructor:\n\n```php\n$factory = new GenericFactory([\n    'default_class' =\u003e '\\My\\Foo\\DefaultClassInterface',\n]);\n```\n\n\u003e ⚠️ Setting a default class name changes the standard Factory behavior. When an invalid class name is used, instead of throwing an `Exception`, an object of the default class type will **always** be returned.\n\n### Constructor arguments\n\nIt is possible to set \"automatic\" constructor arguments that will be passed to every created object.\n\nThe recommended way of setting constructor arguments is by passing an array of arguments to the constructor:\n\n```php\n$factory = new GenericFactory([\n    'arguments' =\u003e [\n        [\n            'logger' =\u003e $container['logger'],\n        ],\n        $secondArgument,\n    ],\n]);\n```\n\n### Executing an object callback\n\nIt is possible to execute an object callback upon object instanciation. A callback is any `Callable` that takes the newly created object by reference as its function parameter.\n\n```php\n// $obj is the newly created object\nfunction callback($obj): void;\n```\n\nThe recommended way of adding an object callback is by passing a `Callable` to the constructor:\n\n```php\n$factory = new GenericFactory([\n    'arguments' =\u003e [[\n        'logger' =\u003e $container['logger']\n    ]],\n    'callback' =\u003e function ($obj) {\n        $obj-\u003efoo('bar');\n        $obj-\u003elogger-\u003edebug('Objet instanciated from factory.');\n    }\n]);\n```\n\n## Resources\n\n* [Contributing](https://github.com/charcoalphp/.github/blob/main/CONTRIBUTING.md)\n* [Report issues](https://github.com/charcoalphp/charcoal/issues) and\n  [send pull requests](https://github.com/charcoalphp/charcoal/pulls)\n  in the [main Charcoal repository](https://github.com/charcoalphp/charcoal)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcharcoalphp%2Ffactory","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcharcoalphp%2Ffactory","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcharcoalphp%2Ffactory/lists"}