{"id":20713679,"url":"https://github.com/mcstreetguy/composerparser","last_synced_at":"2025-04-23T08:09:42.164Z","repository":{"id":62526204,"uuid":"135087456","full_name":"MCStreetguy/ComposerParser","owner":"MCStreetguy","description":"An oop composer file parser.","archived":false,"fork":false,"pushed_at":"2023-10-11T15:52:40.000Z","size":92,"stargazers_count":6,"open_issues_count":4,"forks_count":2,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-04-23T08:09:36.356Z","etag":null,"topics":["composer","composer-json","composer-lock","file-parser","json-parser","lockfile","package-management","parser","wrapper-library"],"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/MCStreetguy.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":"SECURITY.md","support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2018-05-27T22:56:38.000Z","updated_at":"2023-12-05T07:01:08.000Z","dependencies_parsed_at":"2024-06-19T09:58:48.188Z","dependency_job_id":"82c6effc-d4f3-42df-aa36-ab07e9d0e39d","html_url":"https://github.com/MCStreetguy/ComposerParser","commit_stats":{"total_commits":69,"total_committers":3,"mean_commits":23.0,"dds":"0.26086956521739135","last_synced_commit":"c4f15b82520a9a1780b3fdd0fde91cbc0f3a1add"},"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/MCStreetguy%2FComposerParser","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/MCStreetguy%2FComposerParser/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/MCStreetguy%2FComposerParser/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/MCStreetguy%2FComposerParser/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/MCStreetguy","download_url":"https://codeload.github.com/MCStreetguy/ComposerParser/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":250395287,"owners_count":21423400,"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":["composer","composer-json","composer-lock","file-parser","json-parser","lockfile","package-management","parser","wrapper-library"],"created_at":"2024-11-17T02:26:41.368Z","updated_at":"2025-04-23T08:09:42.145Z","avatar_url":"https://github.com/MCStreetguy.png","language":"PHP","readme":"# Composer Parser\n\n**A dependency-free parser library for composer.json and composer.lock files.**\n\n- [Composer Parser](#composer-parser)\n  - [Installation](#installation)\n  - [Usage](#usage)\n    - [Parsing](#parsing)\n      - [Exception Handling](#exception-handling)\n      - [Doing it the manual way](#doing-it-the-manual-way)\n        - [Sub-components](#sub-components)\n    - [Data Retrieval](#data-retrieval)\n      - [Special Classes](#special-classes)\n        - [PackageMap](#packagemap)\n        - [NamespaceMap](#namespacemap)\n    - [Global Configuration](#global-configuration)\n  - [Versioning](#versioning)\n  - [Testing](#testing)\n  - [Authors](#authors)\n    - [Acknowledgement](#acknowledgement)\n  - [License](#license)\n\n## Installation\n\n``` bash\ncomposer require mcstreetguy/composer-parser\n```\n\n## Usage\n\n### Parsing\n\nI recommend using the provided magic Factory method for parsing:\n\n``` php\nuse MCStreetguy\\ComposerParser\\Factory as ComposerParser;\n\n$composerJson = ComposerParser::parse('/path/to/composer.json');\n$lockfile = ComposerParser::parse('/path/to/composer.lock');\n```\n\nEven if this is the easiest way to retrieve an instance, it may be that you\nfor some reason cannot rely on these automations (e.g. if you got a differing filename).\nIn this case you can also call the parse methods directly:\n\n``` php\n$composerJson = ComposerParser::parseComposerJson('/some/file');\n$lockfile = ComposerParser::parseLockfile('/another/file');\n```\n\n_Please note_ that ComposerParser will not complain about any missing fields, instead\nit fills all missing values with default ones to ensure integrity.    \n\n#### Exception Handling\n\nDuring instatiation through the Factory, two exceptions may occur:\n\n``` php\ntry {\n    $composerJson = ComposerParser::parse('/path/to/composer.json');\n} catch (InvalidArgumentException $e) {\n    // The given file could not be found or is not readable\n} catch (RuntimeException $e) {\n    // The given file contained an invalid JSON string\n}\n```\n\n#### Doing it the manual way\n\nIf you can not rely on the Factory for any reason, you can also instantiate the class directly.  \n**This is however not recommended and may lead to unexpected behaviour.**\n\n``` php\nuse \\MCStreetguy\\ComposerParse\\ComposerJson;\n\n$rawData = file_get_contents('/path/to/composer.json');\n$parsedData = json_decode($rawData, true);\n\n$composerJson = new ComposerJson($parsedData);\n```\n\nAs you can see the constructor needs an array with the parsed json data.\nThis applies to all constructor methods throughout the library.\n\n##### Sub-components\n\nYou can also create sub-components directly.\nIn this case you have to keep in mind, that their constructors only accept the isolated data from the composer manifest\n(e.g. the `Author` class expects you to pass only the contents of one of the objects in the `author`-field).\n\n``` php\nuse \\MCStreetguy\\ComposerParse\\Json\\Author;\n\n$rawData = file_get_contents('/path/to/composer.json');\n$parsedData = json_decode($rawData, true);\n\n$author = new Author($parsedData['authors'][0]);\n```\n\n### Data Retrieval\n\nAll class instances provide getters for their properties.\nThe structure is directly adapted from [the composer.json schema](https://getcomposer.org/doc/04-schema.md).\nThe corresponding property names of wrapper classes have been converted to camelCase (see the [PSR-1 standard](https://www.php-fig.org/psr/psr-1/#4-class-constants-properties-and-method) for further information).\n\nFor any property you can use the provided getter methods.\n\n``` php\n$license = $composerJson-\u003egetLicense();\n$version = $composerJson-\u003egetVersion();\n```\n\nIt's also possible to directly access properties. _Please note_ that this is read-only!\n\n``` php\n$description = $composerJson-\u003edescription;\n$require = $composerJson-\u003erequire;\n```\n\nYou may also call `empty()` or `isset()` on the class properties.    \nTo check if the whole wrapper is empty (useful for nested classes) there is an `isEmpty()` method inherited:\n\n``` php\nif ($composerJson-\u003econfig-\u003eisEmpty()) {\n    // Do something\n}\n```\n\n#### Special Classes\n\nComposerParser uses some special classes for parts of the json schema.\n\n##### PackageMap\n\nThe PackageMap class is used for the fields `require`, `require-dev`, `conflict`, `replace`, `provide` and `suggest`.\nIt converts a json structure like this:\n\n``` json\n{\n    \"require\": {\n        \"vendor/package\": \"^2.3\",\n        \"foo/bar\": \"dev-master\"\n    }\n}\n```\n\ninto an array structure like this:\n\n``` php\n$require = [\n    [\n        \"package\" =\u003e \"vendor/package\",\n        \"version\" =\u003e \"^2.3\"\n    ],\n    [\n        \"package\" =\u003e \"foo/bar\",\n        \"version\" =\u003e \"dev-master\"\n    ]\n]\n```\n\nAdditionally it implements the Iterator and ArrayAccess interfaces, thus you may directly access it's contents or put it in a foreach loop:\n\n``` php\n$require = $composerJson-\u003egetRequire();\n\n$fooBar = $require[1];\n\nforeach ($require as $requirement) {\n    $package = $requirement['package'];\n    $version = $requirement['version'];\n\n    echo \"I need $package at version $version!\";\n}\n```\n\nIf you for some reason need the original mapped data you can retrieve it as following:\n\n``` php\n$map = $require-\u003egetData();\n```\n\n##### NamespaceMap\n\nThe NamespaceMap is used for the fields `autoload.psr-0` and `autoload.psr-4`.\nIn fact this class is identical to the [PackageMap](#packagemap). The only difference are the map keys:\n\n``` json\n{\n    \"psr-4\": {\n        \"MCStreetguy\\\\ComposerParser\\\\\": \"src/\"\n    }\n}\n```\n\n``` php\n$psr4 = [\n    [\n        \"namespace\" =\u003e \"MCStreetguy\\\\ComposerParser\\\\\",\n        \"source\" =\u003e \"src/\"\n    ]\n]\n```\n\n### Global Configuration\n\nBy default, the library tries to load your global configuration file, if there is any.\nIt follows the location rules for the composer home directory [as defined in the documentation](https://getcomposer.org/doc/03-cli.md#composer-home).\nIf there is no readable global configuration file present, this step is silently skipped.\n\nJust as composer itself, the following precedence rule applies to global configuration files:\n\u003e In case global configuration matches local configuration, the local configuration in the project's composer.json always wins.\n_([source: https://getcomposer.org/doc/03-cli.md#composer-home-config-json](https://getcomposer.org/doc/03-cli.md#composer-home-config-json))_\n\nYou may suppress this behaviour by passing `true` as second parameter to the `parse`, `parseComposerJson` and `parseLockfile` methods.\n\n## Versioning\n\nWe use [SemVer](http://semver.org/) for versioning. For the versions available, see the [tags on this repository](https://github.com/MCStreetguy/ComposerParser/tags). \n\n## Testing\n\nIf you contribute to this project, you have to ensure that your changes don't mess up the existing functionality.\nTherefore this repository comes with a PhpUnit testing configuration, that you can execute by running `make test`.\nSee [their documentation](https://phpunit.readthedocs.io/en/8.2/installation.html) on more information on how to install the tool.\n\n## Authors\n\n* **Maximilian Schmidt** - _Owner_ - [MCStreetguy](https://github.com/MCStreetguy/)\n\nSee also the list of [contributors](https://github.com/MCStreetguy/ComposerParser/contributors) who participated in this project.\n\n### Acknowledgement\n\nSpecial thanks go to [antonkomarev](https://github.com/antonkomarev) who helped discovering and fixing several deeply nested bugs in the libraries architecture.\n\n## License\n\nThis project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmcstreetguy%2Fcomposerparser","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmcstreetguy%2Fcomposerparser","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmcstreetguy%2Fcomposerparser/lists"}