├── .github └── workflows │ ├── codeql-analysis.yml │ └── linter.yml ├── .gitignore ├── .php_cs.cache ├── .phpunit.result.cache ├── .travis.yml ├── CODE_OF_CONDUCT.md ├── CONTRIBUTING.md ├── LICENSE.md ├── README.md ├── composer.json ├── phpunit.xml ├── psalm.xml ├── src └── Preloader │ └── Preloader.php └── tests ├── Preloader └── PreloaderTest.php ├── resources1 ├── a.php ├── b.php └── nested │ └── c.php └── resources2 ├── a.php ├── b.php └── nested └── c.php /.github/workflows/codeql-analysis.yml: -------------------------------------------------------------------------------- 1 | name: "CodeQL" 2 | 3 | on: [pull_request] 4 | jobs: 5 | lint: 6 | name: CodeQL 7 | runs-on: ubuntu-latest 8 | 9 | steps: 10 | - name: Checkout repository 11 | uses: actions/checkout@v3 12 | with: 13 | fetch-depth: 2 14 | 15 | - run: git checkout HEAD^2 16 | 17 | - name: Run CodeQL 18 | run: | 19 | docker run --rm -v $PWD:/app composer sh -c \ 20 | "composer install --profile --ignore-platform-reqs && composer check" -------------------------------------------------------------------------------- /.github/workflows/linter.yml: -------------------------------------------------------------------------------- 1 | name: "Linter" 2 | 3 | on: [pull_request] 4 | jobs: 5 | lint: 6 | name: Linter 7 | runs-on: ubuntu-latest 8 | 9 | steps: 10 | - name: Checkout repository 11 | uses: actions/checkout@v3 12 | with: 13 | fetch-depth: 2 14 | 15 | - run: git checkout HEAD^2 16 | 17 | - name: Run Linter 18 | run: | 19 | docker run --rm -v $PWD:/app composer sh -c \ 20 | "composer install --profile --ignore-platform-reqs && composer lint" -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | composer.lock 2 | /vendor/ 3 | /.idea/ -------------------------------------------------------------------------------- /.php_cs.cache: -------------------------------------------------------------------------------- 1 | {"php":"7.3.11","version":"2.16.3:v2.16.3#83baf823a33a1cbd5416c8626935cf3f843c10b0","indent":" ","lineEnding":"\n","rules":{"braces":{"allow_single_line_closure":true}},"hashes":{"src\/Preloader\/Preloader.php":3043132934}} -------------------------------------------------------------------------------- /.phpunit.result.cache: -------------------------------------------------------------------------------- 1 | C:37:"PHPUnit\Runner\DefaultTestResultCache":144:{a:2:{s:7:"defects";a:1:{s:36:"Utopia\Tests\PreloaderTest::testTest";i:3;}s:5:"times";a:1:{s:36:"Utopia\Tests\PreloaderTest::testTest";d:0.011;}}} -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: php 2 | 3 | php: 4 | - 8.0 5 | - nightly 6 | 7 | notifications: 8 | email: 9 | - team@appwrite.io 10 | 11 | before_script: composer install --ignore-platform-reqs 12 | 13 | script: 14 | - vendor/bin/phpunit --configuration phpunit.xml 15 | - vendor/bin/psalm --show-info=true 16 | -------------------------------------------------------------------------------- /CODE_OF_CONDUCT.md: -------------------------------------------------------------------------------- 1 | # Contributor Covenant Code of Conduct 2 | 3 | ## Our Pledge 4 | 5 | In the interest of fostering an open and welcoming environment, we as 6 | contributors and maintainers pledge to make participation in our project and 7 | our community a harassment-free experience for everyone, regardless of age, body 8 | size, disability, ethnicity, sex characteristics, gender identity, expression, 9 | level of experience, education, socio-economic status, nationality, personal 10 | appearance, race, religion, or sexual identity and orientation. 11 | 12 | ## Our Standards 13 | 14 | Examples of behavior that contributes to creating a positive environment 15 | include: 16 | 17 | - Using welcoming and inclusive language 18 | - Being respectful of differing viewpoints and experiences 19 | - Gracefully accepting constructive criticism 20 | - Focusing on what is best for the community 21 | - Showing empathy towards other community members 22 | 23 | Examples of unacceptable behavior by participants include: 24 | 25 | - The use of sexualized language or imagery and unwelcome sexual attention or 26 | advances 27 | - Trolling, insulting/derogatory comments, and personal or political attacks 28 | - Public or private harassment 29 | - Publishing others' private information, such as a physical or electronic 30 | address, without explicit permission 31 | - Other conduct which could reasonably be considered inappropriate in a 32 | professional setting 33 | 34 | ## Our Responsibilities 35 | 36 | Project maintainers are responsible for clarifying the standards of acceptable 37 | behavior and are expected to take appropriate and fair corrective action in 38 | response to any instances of unacceptable behavior. 39 | 40 | Project maintainers have the right and responsibility to remove, edit, or 41 | reject comments, commits, code, wiki edits, issues, and other contributions 42 | that are not aligned to this Code of Conduct, or to ban temporarily or 43 | permanently any contributor for other behaviors that they deem inappropriate, 44 | threatening, offensive, or harmful. 45 | 46 | ## Scope 47 | 48 | This Code of Conduct applies both within project spaces and in public spaces 49 | when an individual is representing the project or its community. Examples of 50 | representing a project or community include using an official project e-mail 51 | address, posting via an official social media account, or acting as an appointed 52 | representative at an online or offline event. Representation of a project may be 53 | further defined and clarified by project maintainers. 54 | 55 | ## Enforcement 56 | 57 | Instances of abusive, harassing, or otherwise unacceptable behavior may be 58 | reported by contacting the project team at team@appwrite.io. All 59 | complaints will be reviewed and investigated and will result in a response that 60 | is deemed necessary and appropriate to the circumstances. The project team is 61 | obligated to maintain confidentiality with regard to the reporter of an incident. 62 | Further details of specific enforcement policies may be posted separately. 63 | 64 | Project maintainers who do not follow or enforce the Code of Conduct in good 65 | faith may face temporary or permanent repercussions as determined by other 66 | members of the project's leadership. 67 | 68 | ## Attribution 69 | 70 | This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4, 71 | available at https://www.contributor-covenant.org/version/1/4/code-of-conduct.html 72 | 73 | [homepage]: https://www.contributor-covenant.org 74 | 75 | For answers to common questions about this code of conduct, see 76 | https://www.contributor-covenant.org/faq -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | # Contributing 2 | 3 | We would ❤️ for you to contribute to Utopia-php and help make it better! We want contributing to Utopia-php to be fun, enjoyable, and educational for anyone and everyone. All contributions are welcome, including issues, new docs as well as updates and tweaks, blog posts, workshops, and more. 4 | 5 | ## How to Start? 6 | 7 | If you are worried or don’t know where to start, check out our next section explaining what kind of help we could use and where can you get involved. You can reach out with questions to [Eldad Fux (@eldadfux)](https://twitter.com/eldadfux) or anyone from the [Appwrite team on Discord](https://discord.gg/GSeTUeA). You can also submit an issue, and a maintainer can guide you! 8 | 9 | ## Code of Conduct 10 | 11 | Help us keep Utopia-php open and inclusive. Please read and follow our [Code of Conduct](/CODE_OF_CONDUCT.md). 12 | 13 | ## Submit a Pull Request 🚀 14 | 15 | Branch naming convention is as following 16 | 17 | `TYPE-ISSUE_ID-DESCRIPTION` 18 | 19 | example: 20 | 21 | ``` 22 | doc-548-submit-a-pull-request-section-to-contribution-guide 23 | ``` 24 | 25 | When `TYPE` can be: 26 | 27 | - **feat** - is a new feature 28 | - **doc** - documentation only changes 29 | - **cicd** - changes related to CI/CD system 30 | - **fix** - a bug fix 31 | - **refactor** - code change that neither fixes a bug nor adds a feature 32 | 33 | **All PRs must include a commit message with the changes description!** 34 | 35 | For the initial start, fork the project and use git clone command to download the repository to your computer. A standard procedure for working on an issue would be to: 36 | 37 | 1. `git pull`, before creating a new branch, pull the changes from upstream. Your master needs to be up to date. 38 | 39 | ``` 40 | $ git pull 41 | ``` 42 | 43 | 2. Create new branch from `master` like: `doc-548-submit-a-pull-request-section-to-contribution-guide`
44 | 45 | ``` 46 | $ git checkout -b [name_of_your_new_branch] 47 | ``` 48 | 49 | 3. Work - commit - repeat ( be sure to be in your branch ) 50 | 51 | 4. Push changes to GitHub 52 | 53 | ``` 54 | $ git push origin [name_of_your_new_branch] 55 | ``` 56 | 57 | 5. Submit your changes for review 58 | If you go to your repository on GitHub, you'll see a `Compare & pull request` button. Click on that button. 59 | 6. Start a Pull Request 60 | Now submit the pull request and click on `Create pull request`. 61 | 7. Get a code review approval/reject 62 | 8. After approval, merge your PR 63 | 9. GitHub will automatically delete the branch after the merge is done. (they can still be restored). 64 | 65 | ## Introducing New Features 66 | 67 | We would 💖 you to contribute to Utopia-php, but we would also like to make sure Utopia-php is as great as possible and loyal to its vision and mission statement 🙏. 68 | 69 | For us to find the right balance, please open an issue explaining your ideas before introducing a new pull request. 70 | 71 | This will allow the Utopia-php community to have sufficient discussion about the new feature value and how it fits in the product roadmap and vision. 72 | 73 | This is also important for the Utopia-php lead developers to be able to give technical input and different emphasis regarding the feature design and architecture. Some bigger features might need to go through our [RFC process](https://github.com/appwrite/rfc). 74 | 75 | ## Other Ways to Help 76 | 77 | Pull requests are great, but there are many other areas where you can help Utopia-php. 78 | 79 | ### Blogging & Speaking 80 | 81 | Blogging, speaking about, or creating tutorials about one of Utopia-php’s many features is great way to contribute and help our project grow. 82 | 83 | ### Presenting at Meetups 84 | 85 | Presenting at meetups and conferences about your Utopia-php projects. Your unique challenges and successes in building things with Utopia-php can provide great speaking material. We’d love to review your talk abstract/CFP, so get in touch with us if you’d like some help! 86 | 87 | ### Sending Feedbacks & Reporting Bugs 88 | 89 | Sending feedback is a great way for us to understand your different use cases of Utopia-php better. If you had any issues, bugs, or want to share about your experience, feel free to do so on our GitHub issues page or at our [Discord channel](https://discord.gg/GSeTUeA). 90 | 91 | ### Submitting New Ideas 92 | 93 | If you think Utopia-php could use a new feature, please open an issue on our GitHub repository, stating as much information as you can think about your new idea and it's implications. We would also use this issue to gather more information, get more feedback from the community, and have a proper discussion about the new feature. 94 | 95 | ### Improving Documentation 96 | 97 | Submitting documentation updates, enhancements, designs, or bug fixes. Spelling or grammar fixes will be very much appreciated. 98 | 99 | ### Helping Someone 100 | 101 | Searching for Utopia-php, GitHub or StackOverflow and helping someone else who needs help. You can also help by teaching others how to contribute to Utopia-php's repo! 102 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2013 Eldad Fux 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of 6 | this software and associated documentation files (the "Software"), to deal in 7 | the Software without restriction, including without limitation the rights to 8 | use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 9 | the Software, and to permit persons to whom the Software is furnished to do so, 10 | subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 17 | FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 18 | COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 19 | IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 20 | CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 21 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Utopia Preloader 2 | 3 | [![Build Status](https://travis-ci.org/utopia-php/preloader.svg?branch=master)](https://travis-ci.com/utopia-php/preloader) 4 | [![Discord](https://badgen.net/badge/discord/chat/green)](https://appwrite.io/discord) 5 | ![Total Downloads](https://img.shields.io/packagist/dt/utopia-php/preloader.svg) 6 | 7 | Utopia Preloader library is a simple and lite library for managing PHP preloading configuration. This library is aiming to be as simple as possible and easy to learn and use. This library is maintained by the [Appwrite team](https://appwrite.io). 8 | 9 | 10 | Although this library is a part of the [Utopia Framework](https://github.com/utopia-php/framework) project it is dependency free and can be used as standalone with any other PHP project or framework. 11 | 12 | ## Getting Started 13 | 14 | Install using composer: 15 | ```bash 16 | composer require utopia-php/preloader 17 | ``` 18 | 19 | ```php 20 | paths(realpath(__DIR__ . '/../app/config')) 30 | ->paths(realpath(__DIR__ . '/../src')) 31 | ->ignore(realpath(__DIR__ . '/../vendor/twig/twig')) 32 | ->ignore(realpath(__DIR__ . '/../vendor/guzzlehttp/guzzle')) 33 | ->ignore(realpath(__DIR__ . '/../vendor/geoip2')) 34 | ->ignore(realpath(__DIR__ . '/../vendor/maxmind')) 35 | ->ignore(realpath(__DIR__ . '/../vendor/maxmind-db')) 36 | ->ignore(realpath(__DIR__ . '/../vendor/piwik')) 37 | ->load(); 38 | 39 | ``` 40 | 41 | ## System Requirements 42 | 43 | Utopia Framework requires PHP 8.0 or later. Preloading is supported in PHP 7.4 or later. We recommend using the latest PHP version whenever possible. 44 | 45 | 46 | ## Copyright and license 47 | 48 | The MIT License (MIT) [http://www.opensource.org/licenses/mit-license.php](http://www.opensource.org/licenses/mit-license.php) 49 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "utopia-php/preloader", 3 | "description": "Utopia Preloader library is simple and lite library for managing PHP preloading configuration", 4 | "type": "library", 5 | "keywords": ["php","framework", "upf", "utopia", "preloader", "preload", "preloading"], 6 | "license": "MIT", 7 | "scripts": { 8 | "check": "./vendor/bin/phpstan analyse --level max src tests", 9 | "lint": "./vendor/bin/pint --test", 10 | "format": "./vendor/bin/pint" 11 | }, 12 | 13 | "autoload": { 14 | "psr-4": {"Utopia\\Preloader\\": "src/Preloader"} 15 | }, 16 | "require": { 17 | "php": ">=8.0" 18 | }, 19 | "require-dev": { 20 | "phpunit/phpunit": "^9.3", 21 | "vimeo/psalm": "4.0.1", 22 | "phpstan/phpstan": "1.9.x-dev", 23 | "laravel/pint": "1.2.*" 24 | }, 25 | "minimum-stability": "dev" 26 | } 27 | -------------------------------------------------------------------------------- /phpunit.xml: -------------------------------------------------------------------------------- 1 | 11 | 12 | 13 | ./tests/ 14 | 15 | 16 | -------------------------------------------------------------------------------- /psalm.xml: -------------------------------------------------------------------------------- 1 | 2 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | -------------------------------------------------------------------------------- /src/Preloader/Preloader.php: -------------------------------------------------------------------------------- 1 | 9 | */ 10 | protected $ignores = []; 11 | 12 | /** 13 | * @var array 14 | */ 15 | protected $paths = []; 16 | 17 | /** 18 | * @var array 19 | */ 20 | protected $loaded = []; 21 | 22 | /** 23 | * @var int 24 | */ 25 | protected $count = 0; 26 | 27 | /** 28 | * @var bool 29 | */ 30 | protected $debug = false; 31 | 32 | /** 33 | * @var array 34 | */ 35 | protected $included = []; 36 | 37 | /** 38 | * Constructor 39 | * 40 | * @param string $paths 41 | */ 42 | public function __construct(string ...$paths) 43 | { 44 | $this->paths = $paths; 45 | 46 | $require = __DIR__.'/../../../../composer/autoload_classmap.php'; 47 | $classMap = (file_exists($require)) ? require $require : []; 48 | 49 | $this->paths = \array_merge( 50 | $this->paths, 51 | \array_values($classMap) 52 | ); 53 | } 54 | 55 | /** 56 | * Paths 57 | * 58 | * Path to load 59 | * 60 | * @param string $paths 61 | * @return $this 62 | */ 63 | public function paths(string ...$paths): self 64 | { 65 | $this->paths = \array_merge( 66 | $this->paths, 67 | $paths 68 | ); 69 | 70 | return $this; 71 | } 72 | 73 | /** 74 | * Ignore 75 | * 76 | * Ignore a given path or file 77 | * 78 | * @param string $names 79 | * @return $this 80 | */ 81 | public function ignore(string ...$names): self 82 | { 83 | foreach ($names as $name) { 84 | if (is_readable($name)) { 85 | $this->ignores[] = $name; 86 | } else { 87 | if ($this->debug) { 88 | echo "[Preloader] Failed to ignore path `{$name}`".PHP_EOL; 89 | } 90 | } 91 | } 92 | 93 | return $this; 94 | } 95 | 96 | /** 97 | * Load 98 | * 99 | * Loads all preloader preconfigured paths and files 100 | */ 101 | public function load(): void 102 | { 103 | $this->loaded = get_included_files(); 104 | 105 | foreach ($this->paths as $path) { 106 | $this->loadPath(\rtrim($path, '/')); 107 | } 108 | 109 | if ($this->debug) { 110 | echo "[Preloader] Preloaded {$this->count} files.".PHP_EOL; 111 | } 112 | } 113 | 114 | /** 115 | * Get Count 116 | * 117 | * Get the total number of loaded files. 118 | * 119 | * @return int 120 | */ 121 | public function getCount(): int 122 | { 123 | return $this->count; 124 | } 125 | 126 | /** 127 | * Get List 128 | * 129 | * Get a list of all included paths. 130 | 131 | * 132 | * @return array 133 | */ 134 | public function getList(): array 135 | { 136 | return $this->included; 137 | } 138 | 139 | /** 140 | * Set Debug 141 | * 142 | * Set debug mode status. 143 | * 144 | * @param bool $status 145 | */ 146 | public function setDebug(bool $status = true): self 147 | { 148 | $this->debug = $status; 149 | 150 | return $this; 151 | } 152 | 153 | /** 154 | * Load Path 155 | * 156 | * Load a specific file or folder and nested folders. 157 | * 158 | * @param string $path 159 | * @return void 160 | */ 161 | private function loadPath(string $path): void 162 | { 163 | if (\is_dir($path)) { 164 | $this->loadDir($path); 165 | 166 | return; 167 | } 168 | 169 | $this->loadFile($path); 170 | } 171 | 172 | /** 173 | * Load Directory 174 | * 175 | * Load a specific folder and nested folders. 176 | * 177 | * @param string $path 178 | * @return void 179 | */ 180 | private function loadDir(string $path): void 181 | { 182 | $handle = \opendir($path); 183 | if ($handle) { 184 | while (($file = \readdir($handle)) !== false) { 185 | if (\in_array($file, ['.', '..'])) { 186 | continue; 187 | } 188 | 189 | $this->loadPath("{$path}/{$file}"); 190 | } 191 | 192 | \closedir($handle); 193 | } 194 | } 195 | 196 | /** 197 | * Load File 198 | * 199 | * Load a specific file. 200 | * 201 | * @param string $path 202 | * @return void 203 | */ 204 | private function loadFile(string $path): void 205 | { 206 | if ($this->shouldIgnore($path)) { 207 | return; 208 | } 209 | 210 | if (\in_array(\realpath($path), $this->included)) { 211 | if ($this->debug) { 212 | echo "[Preloader] Skiped `{$path}`".PHP_EOL; 213 | } 214 | 215 | return; 216 | } 217 | 218 | if (\in_array(\realpath($path), $this->loaded)) { 219 | if ($this->debug) { 220 | echo "[Preloader] Skiped `{$path}`".PHP_EOL; 221 | } 222 | 223 | return; 224 | } 225 | 226 | try { 227 | // opcache_compile_file($path); 228 | require $path; 229 | } catch (\Throwable $th) { 230 | if ($this->debug) { 231 | echo "[Preloader] Failed to load `{$path}`: ".$th->getMessage().PHP_EOL; 232 | } 233 | 234 | return; 235 | } 236 | 237 | $this->loaded = get_included_files(); 238 | $this->included[] = $path; 239 | $this->count++; 240 | } 241 | 242 | /** 243 | * Should Ignore 244 | * 245 | * Should a given path be ignored or not? 246 | * 247 | * @param string $path 248 | * @return bool 249 | */ 250 | private function shouldIgnore(?string $path): bool 251 | { 252 | if ($path === null) { 253 | return true; 254 | } 255 | 256 | if (! \in_array(\pathinfo($path, PATHINFO_EXTENSION), ['php'])) { 257 | return true; 258 | } 259 | 260 | foreach ($this->ignores as $ignore) { 261 | if (\strpos($path, $ignore) === 0) { 262 | return true; 263 | } 264 | } 265 | 266 | return false; 267 | } 268 | } 269 | -------------------------------------------------------------------------------- /tests/Preloader/PreloaderTest.php: -------------------------------------------------------------------------------- 1 | load(); 27 | 28 | $autoloaded = $preloader->getCount(); 29 | 30 | $this->assertEquals(0, $autoloaded); 31 | $this->assertCount(0, $preloader->getList()); 32 | 33 | $preloader = new Preloader(); 34 | 35 | $preloader 36 | 37 | ->paths(__DIR__.'/../resources1/') 38 | 39 | ->load(); 40 | 41 | $this->assertEquals(3, $preloader->getCount()); 42 | $this->assertCount(3, $preloader->getList()); 43 | 44 | $preloader = new Preloader(); 45 | 46 | $path = realpath(__DIR__.'/../resources2') ?: ''; 47 | $ignore = realpath(__DIR__.'/../resources2/nested') ?: ''; 48 | $preloader 49 | ->paths($path) 50 | ->ignore($ignore) 51 | ->load(); 52 | 53 | $this->assertEquals(2, $preloader->getCount()); 54 | $this->assertCount(2, $preloader->getList()); 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /tests/resources1/a.php: -------------------------------------------------------------------------------- 1 |