{"id":15022101,"url":"https://github.com/ayesh/composer-preload","last_synced_at":"2025-04-04T22:06:12.358Z","repository":{"id":33436471,"uuid":"156724942","full_name":"Ayesh/Composer-Preload","owner":"Ayesh","description":"Preload your sweet sweet code to opcache with a composer command, making your code faster to run.","archived":false,"fork":false,"pushed_at":"2022-05-16T11:25:07.000Z","size":92,"stargazers_count":205,"open_issues_count":4,"forks_count":12,"subscribers_count":10,"default_branch":"master","last_synced_at":"2025-04-04T22:06:06.520Z","etag":null,"topics":["composer","composer-commands","composer-plugin","composer-preload","opcache","php"],"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/Ayesh.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":"2018-11-08T15:09:25.000Z","updated_at":"2024-12-04T09:53:55.000Z","dependencies_parsed_at":"2022-07-27T15:32:03.444Z","dependency_job_id":null,"html_url":"https://github.com/Ayesh/Composer-Preload","commit_stats":null,"previous_names":[],"tags_count":13,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Ayesh%2FComposer-Preload","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Ayesh%2FComposer-Preload/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Ayesh%2FComposer-Preload/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Ayesh%2FComposer-Preload/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Ayesh","download_url":"https://codeload.github.com/Ayesh/Composer-Preload/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247256112,"owners_count":20909240,"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-commands","composer-plugin","composer-preload","opcache","php"],"created_at":"2024-09-24T19:57:27.587Z","updated_at":"2025-04-04T22:06:12.331Z","avatar_url":"https://github.com/Ayesh.png","language":"PHP","readme":"# Composer Preload\n\nPreload your sweet sweet code to opcache with a composer command, making your code run faster.\n\nComposer Preload is a composer plugin aiming to provide and complement PHP opcache warming.\nThis plugin introduces a new `composer preload` command that can generate a `vendor/preload.php` file (following \n`vendor/autoload.php` pattern) that contains calls to warm up the opcache cache.\n\nPlease note that this plugin is currently in a very rudimentary state, and it is highly recommend to not use this in \nany production system. Any contributions are warmly welcome!\n\n# How it works\n\nAt the moment, this plugin scans for `.php` files in the given paths recursively, and create a file that calls \n`opcache_compile_file` function.\n\nWhen you want to warm up the cache, you can either call `php vendor/preload.php` in command line, or when PHP 7.4 hits \nthe shelves, configure PHP to automatically load this file.\n\n# Installation\n\nJust the way you'd install a normal composer package, you can install this plugin aswell:\n```\ncomposer require ayesh/composer-preload\n```\nIf you would rather install this globally:\n```\ncomposer g require ayesh/composer-preload\n```\n\n# Configuration\n\n1: Modify your `composer.json` file, and create a section called `extra` if it's not there already. Following is an \nexample:\n\n```\n{\n    \"extra\": {\n        \"preload\": {\n            \"paths\": [\n                \"web\"\n            ],\n            \"exclude\": [\n                \"web/core/tests\",\n                \"web/core/lib/Drupal/Component/Assertion\",\n                \"web/core/modules/simpletest\",\n                \"web/core/modules/editor/src/Tests\"\n            ],\n            \"extensions\": [\"php\", \"module\", \"inc\", \"install\"],\n            \"exclude-regex\": \"/[A-Za-z0-9_]test\\\\.php$/i\",\n            \"no-status-check\": false,\n            \"files\": [\n                \"somefile.php\"\n            ]\n        }\n    }\n}\n```\nThe `extra.preload` directive contains all the configuration options for this plugin. The `paths` directive must be an \narray of directories relative to the `composer.json` file. These directories will be scanned recursively for `.php` \nfiles, converted to absolute paths, and appended to the `vendor/preload.php` file.\n\n2: Run the `composer preload` command.\n\n3: Execute the generated `vendor/preload.php` file. You can either run `php vendor/preload.php` or use your web server \nto execute it. See the Preloading section below for more information.\n\n\n## Configuration options\n\n - `extra.preload.paths` : _Required_\n\nAn array of directory paths to look for `.php` files in. This setting is required as of now. The directories must exist\n at the time `composer preload` command is run.\n\n - `extra.preload.exclude` : _Optional_\n\nAn array of directory paths to exclude from the `preload.php`. This list must be relative to the composer.json file, \nsimilar to the `paths` directive. The ideal use case limiting the scope of the `paths` directive.\n\n - `extra.preload.extensions` : _Optional_, Default: `[\"php\"]`\n\nAn array of file extensions to search for. If not entered, it will search for all `.php` files.\nDo not enter the proceeding period (`.`) character. The example above is suitable for Drupal. For Symfony/Laravel projects,\nyou can leave the default option `[\"php\"]` or just not use this option so it defaults to just `.php`.\n\n - `extra.preload.exclude-regex` : _Optional_\n\nSet a PCRE compatible full regular expression (with delimiters and modifiers included) that will be matched against\nthe full path, and if matched, will be excluded from the preload list. This can help you exclude tests from the preload\nlist.\n\nFor example, to exclude all PHPUnit-akin tests, you can use the regular expression `/[A-Za-z0-9_]test\\\\.php$/i`.\nThis will make sure the file name ends with \"test.php\", but also has an alphanumeric or underscore prefix. This is \na common pattern of PHPUnit tests. The `/i` modifier makes the match case insensitive. \n\nFor directory separators, always use Unix-style forward slashes (`/`) even if you are on a Windows system that uses \nbackwards slashes (`\\`). Don't forget to properly escape the regex pattern to work within JSON syntax; e.g escape\nslashes (`\\` and `/`) with a backwards slash (`\\` -\u003e `\\\\` and `/` -\u003e `\\/`). This will make the regular expression\nhard to read, but ¯\\\\_(ツ)_/¯.\n\n - `extra.preload.no-status-check`: _Optional_, Default: _`false`_\n\nIf this setting is set to `true` (you can also pass command line option `--no-status-check`), make the generated \n`preload.php` file not contain additional checks to make sure the opcache is enabled. This setting is disabled by \ndefault, and the generated `preload.php` file will contain a small snippet on the top that makes it quit if opcache is \nnot enabled.\n\n - `extra.preload.files` : _Optional_\n\nAn array of single files to be included. This setting is optional. The files must exist\n at the time `composer preload` command is run.\n\n# Preloading\n\nTo do the actual preloading, execute `vendor/preload.php`. \n\nIf you have enabled opcache for CLI applications, you can directly call `php vendor/preload.php` to execute the \ngenerated PHP file and warm up the cache right away. \n\nFuture versions of this plugin will have a feature to generate the file _and_ immediately run it.\n\nIn a webserver context, or when you cannot run the PHP file with the CLI `php` binary. this probably means you'll \nwant to link `vendor/preload.php` into your docroot somwhere and curl it. For example, \n`ln -s vendor/preload.php path/to/docroot/preload.php` and then `curl localhost/preload.php` on webserver startup.\n\n\n# FAQ\n\n### What does this plugin even do?\n\nThis plugin can create a new file at `vendor/preload.php` that follows the pattern of Composer's autoloader at \n`vendor/autoload.php`. This new `preload.php` file contains several function calls that compiles PHP files and cache\nthem into PHP's opcache. PHP Opcache is a shared memory (with optional file storage option) feature in PHP that can\nhold compiled PHP files, so the same file doesn't need to be compiled again and again when its called. This is a\npersistent memory until PHP is restarted or the cache is eventually cleared. \n\nCaching files in opcache has siginificant performance benefits for the cost of memory.\n\n### So all the files are loaded all the time?\n\nAll the files are loaded to the _Opcache_. This is **not** same as you `include()` or `require()` a class, which makes\nPHP actually execute the code. When you cache code to Opcache, those classes are not executed - just their compiled code\nis cached to the memory. \n\nFor example, if you declare a variable, this plugin's preload functionality will not make the variables available inside\nyour PHP code. You still have to include the file to make them available. \n\n### I have the `vendor/preload.php` file. What now?\n\nAfter generating the file, you might need to actaully run it effectively load the files to Opcache. Ideally, you should\ndo this every time you restart your web server or PHP server, depending on how you serve PHP within your web server. \n\nPHP 7.4 has a `php.ini` option `opcache.preload` that you can specify this generated file, or a separate file that calls\nall `vendor/preload.php` files you have across your server to actively warm up the cache. \n\n### I have multiple Composer projects running on same server. \n\nYou can generate the preload file for each project, and include all of them in a separate PHP file you create by\nyourself. Then, call all of the generated `vendor/preload.php` files. \n\nBy default, the preload file will contain a small snippet at the top that will quit the script immediately if Opcache\nis not available. If you plan to include this `vendor/preload.php` file from another script, you can use the special\ncommand line option `composer preload --no-status-check` that will make the `vendor/preload.php` file not contain these\nchecks, so you can incude multiple `vendor/preload.php` files across all your projects without running the same snippet\nover and over. It is recommended that you make sure Opcache is enabled before doing so. Feel free to copypasta the\nsnippet from one of your generated preload files. \n\n### Can I generate the preload file in one server and use it in another server?\n\nYes. Similar to the `vendor/autoload.php` file, `vendor/preload.php` file this plugin generates also uses relative paths. \nFrom version `v0.1.0` and forward, you can generate the preload file at one server and reuse it in other servers (directory hierarchies).\n\nYou will still need to run the `vendor/preload.php` file in all servers that you want to preload opcache. \n\n# Roadmap\n\n - ☐ Extend `extras.preload` section to configure the packages that  should be preloaded instead of setting the individual paths.\n - ✓ Feature to set an exclude pattern (v0.0.3)\n - ☐ Progress bar to show the file generation progress\n - ☐ Flag to generate the file _and_ run it, so the cache is immediately warmed up.\n - ☐ Fancier progress bar.\n - ⭕ Full test coverage.\n - ☐ Even more fancier progress bar with opcache memory usage display, etc.\n - ⭕ Get many Github stars\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fayesh%2Fcomposer-preload","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fayesh%2Fcomposer-preload","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fayesh%2Fcomposer-preload/lists"}