Repository: coderello/laravel-shared-data Branch: 4.0 Commit: 5d5cc6e81581 Files: 23 Total size: 29.3 KB Directory structure: gitextract_3gqhbw41/ ├── .gitattributes ├── .github/ │ └── workflows/ │ ├── run-php-cs-fixer.yml │ └── run-tests.yml ├── .gitignore ├── .php_cs ├── CONTRIBUTING.md ├── LICENSE.md ├── README.md ├── composer.json ├── config/ │ └── shared-data.php ├── docs/ │ ├── contributing.md │ ├── installation.md │ ├── javascript-helper.md │ └── sharing-data.md ├── phpunit.xml.dist ├── src/ │ ├── Facades/ │ │ └── SharedData.php │ ├── Providers/ │ │ └── SharedDataServiceProvider.php │ ├── SharedData.php │ └── helpers.php └── tests/ ├── AbstractTestCase.php ├── SharedDataTest.php └── views/ ├── shared.blade.php └── shared_custom.blade.php ================================================ FILE CONTENTS ================================================ ================================================ FILE: .gitattributes ================================================ /.gitattributes export-ignore /.gitignore export-ignore /.styleci.yml export-ignore /phpunit.xml.dist export-ignore /tests export-ignore /.travis.yml export-ignore ================================================ FILE: .github/workflows/run-php-cs-fixer.yml ================================================ name: Fix PHP Code Style on: workflow_dispatch: push: paths: - '**.php' jobs: fix-php-code-style: name: Fix PHP Code Style runs-on: ubuntu-latest steps: - uses: actions/checkout@v2 with: fetch-depth: 2 token: ${{ secrets.GITHUB_TOKEN }} - name: Run PHP CS Fixer uses: docker://oskarstark/php-cs-fixer-ga:2.18.6 - name: Commit changes uses: stefanzweifel/git-auto-commit-action@v4 with: commit_message: Fix Backend Code Style skip_fetch: true ================================================ FILE: .github/workflows/run-tests.yml ================================================ name: Run Tests on: [push] jobs: test: runs-on: ubuntu-latest strategy: matrix: php: [8.1, 8.0] laravel: [9.*] dependency-version: [prefer-lowest, prefer-stable] include: - laravel: 9.* testbench: 7.* name: P${{ matrix.php }} - L${{ matrix.laravel }} - ${{ matrix.dependency-version }} steps: - name: Checkout code uses: actions/checkout@v1 - name: Cache dependencies uses: actions/cache@v1 with: path: ~/.composer/cache/files key: dependencies-php-${{ matrix.php }}-composer-${{ hashFiles('composer.json') }} - name: Setup PHP uses: shivammathur/setup-php@v2 with: php-version: ${{ matrix.php }} extensions: mbstring - name: Install dependencies run: | composer require "laravel/framework:${{ matrix.laravel }}" "orchestra/testbench:${{ matrix.testbench }}" --no-interaction --no-update composer update --${{ matrix.dependency-version }} --prefer-dist --no-interaction --no-suggest - name: Execute tests run: vendor/bin/phpunit ================================================ FILE: .gitignore ================================================ .idea vendor composer.lock /phpunit.xml .phpunit.result.cache .php_cs.cache ================================================ FILE: .php_cs ================================================ notPath('spark') ->notPath('bootstrap') ->notPath('storage') ->notPath('vendor') ->in(__DIR__) ->name('*.php') ->name('_ide_helper') ->notName('*.blade.php') ->ignoreDotFiles(true) ->ignoreVCS(true); return PhpCsFixer\Config::create() ->setRules([ '@PSR2' => true, 'array_syntax' => ['syntax' => 'short'], 'ordered_imports' => ['sortAlgorithm' => 'alpha'], 'no_unused_imports' => true, ]) ->setFinder($finder); ================================================ FILE: CONTRIBUTING.md ================================================ # Contributing Contributions are **welcome** and will be fully **credited**. Please read and understand the contribution guide before creating an issue or pull request. ## Requirements If the project maintainer has any additional requirements, you will find them listed here. - **[PSR-2 Coding Standard.](https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-2-coding-style-guide.md)** The easiest way to apply the conventions is to install [PHP CS Fixer](https://github.com/FriendsOfPHP/PHP-CS-Fixer). - **Add tests!** Your patch won't be accepted if it doesn't have tests. - **Document any change in behaviour.** Make sure the `README.md` and any other relevant documentation are kept up-to-date. - **Consider our release cycle.** We try to follow [SemVer v2.0.0](http://semver.org/). Randomly breaking public APIs is not an option. - **Create feature branches.** Don't ask us to pull from your master branch. - **One pull request per feature.** If you want to do more than one thing, send multiple pull requests. - **Send coherent history.** Make sure each individual commit in your pull request is meaningful. If you had to make multiple intermediate commits while developing, please [squash them](http://www.git-scm.com/book/en/v2/Git-Tools-Rewriting-History#Changing-Multiple-Commit-Messages) before submitting. **Happy coding!** ================================================ FILE: LICENSE.md ================================================ MIT License Copyright (c) 2018 Coderello Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ================================================ FILE: README.md ================================================ # Laravel Shared Data ## ✨ Introduction **Laravel Shared Data** provides an easy way to share the data from your backend to the JavaScript. ## 🚀 Quick start - Install the package: ```bash composer require coderello/laravel-shared-data ``` - Include the `@shared` directive into your blade layout before all scripts. - Share the data from within Laravel: ```bash share(['user' => $user, 'title' => $title]); ``` - Access the data from the JavaScript directly: ```bash const user = window.sharedData.user; const title = window.sharedData.title; ``` - Or using the built-it global helper: ```bash const user = shared('user'); const title = shared('title'); ``` ## 📖 License **Laravel Shared Data** is open-sourced software licensed under the [MIT license](LICENSE.md). ================================================ FILE: composer.json ================================================ { "name": "coderello/laravel-shared-data", "description": "Package for sharing data from Laravel to JavaScript.", "type": "library", "license": "MIT", "require": { "php": "^8.0", "ext-json": "*", "illuminate/support": "^9.0" }, "require-dev": { "orchestra/testbench": "^7.0" }, "autoload": { "psr-4": { "Coderello\\SharedData\\": "src/" }, "files": [ "src/helpers.php" ] }, "autoload-dev": { "psr-4": { "Coderello\\SharedData\\Tests\\": "tests/" } }, "scripts": { "test": "./vendor/bin/phpunit --colors=always" }, "extra": { "laravel": { "providers": [ "Coderello\\SharedData\\Providers\\SharedDataServiceProvider" ] } }, "config": { "sort-packages": true }, "minimum-stability": "dev", "prefer-stable": true } ================================================ FILE: config/shared-data.php ================================================ 'sharedData', /* * The settings for the JavaScript helper function that allows * retrieving the shared data on the front-end side easily. * * Example: `shared('user.email')` */ 'js_helper' => [ /* * Determines whether the JavaScript helper should be included * alongside with the shared data. */ 'enabled' => true, /* * The function name for the JavaScript helper. */ 'name' => 'shared', ], /* * The settings for the Blade directive. */ 'blade_directive' => [ /* * Blade directive name. * * By default the Blade directive named 'shared'. * * It means that the shared data rendering will be available in view files via `@shared`. */ 'name' => 'shared', ], ]; ================================================ FILE: docs/contributing.md ================================================ --- title: Contribution Guide section: Contributing weight: 100 featherIcon: github --- Contributions are **welcome** and will be fully **credited**. Please read and understand the contribution guide before creating an issue or pull request. ## Requirements If the project maintainer has any additional requirements, you will find them listed here. - **[PSR-2 Coding Standard.](https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-2-coding-style-guide.md)** The easiest way to apply the conventions is to install [PHP CS Fixer](https://github.com/FriendsOfPHP/PHP-CS-Fixer). - **Add tests!** Your patch won't be accepted if it doesn't have tests. - **Document any change in behaviour.** Make sure the `README.md` and any other relevant documentation are kept up-to-date. - **Consider our release cycle.** We try to follow [SemVer v2.0.0](http://semver.org/). Randomly breaking public APIs is not an option. - **Create feature branches.** Don't ask us to pull from your master branch. - **One pull request per feature.** If you want to do more than one thing, send multiple pull requests. - **Send coherent history.** Make sure each individual commit in your pull request is meaningful. If you had to make multiple intermediate commits while developing, please [squash them](http://www.git-scm.com/book/en/v2/Git-Tools-Rewriting-History#Changing-Multiple-Commit-Messages) before submitting. **Happy coding!** ================================================ FILE: docs/installation.md ================================================ --- title: Installation section: Getting Started weight: 5000 featherIcon: download --- You can install this package via composer using this command: ```bash composer require coderello/laravel-shared-data ``` After the installation you need to include `@shared` directive into your blade layout before all `'; } public function getJsHelper(): string { return 'window["'.$this->getJsHelperName().'"]=function(e){var n=void 0!==arguments[1]?arguments[1]:null;return[window.sharedDataNamespace].concat("string"==typeof e?e.split("."):[]).reduce(function(e,t){return e===n||"object"!=typeof e||void 0===e[t]?n:e[t]},window)}'; } public function __toString(): string { return $this->render(); } public function toArray(): array { return $this->get(); } public function jsonSerialize(): array { return $this->get(); } public function offsetExists($offset): bool { return Arr::has($this->data, $offset); } public function offsetGet($offset) { return $this->get($offset); } public function offsetSet($offset, $value): void { $this->put($offset, $value); } public function offsetUnset($offset): void { $this->forget($offset); } } ================================================ FILE: src/helpers.php ================================================ put(...$args); } } ================================================ FILE: tests/AbstractTestCase.php ================================================ addLocation(__DIR__.'/views'); } protected function getPackageProviders($app) { return [ SharedDataServiceProvider::class, ]; } } ================================================ FILE: tests/SharedDataTest.php ================================================ sharedData = new SharedData; $this->lazy = null; } public function testPut() { // scalar $this->sharedData->put('foo', 'bar'); $this->assertSame('bar', $this->sharedData->get('foo')); // iterable $this->sharedData->put([ 'scalar' => 'scalar-value', 'array' => ['nested' => 'value'], ]); $this->assertSame('scalar-value', $this->sharedData->get('scalar')); $this->assertSame(['nested' => 'value'], $this->sharedData->get('array')); // JsonSerializable $jsonSerializable = new class implements JsonSerializable { public function jsonSerialize() { return [ 'json-serializable-key' => 'json-serializable-value', ]; } }; $this->sharedData->put($jsonSerializable); $this->assertSame('json-serializable-value', $this->sharedData->get('json-serializable-key')); // Arrayable $arrayable = new class implements Arrayable { public function toArray() { return [ 'arrayable-key' => 'arrayable-value', ]; } }; $this->sharedData->put($arrayable); $this->assertSame('arrayable-value', $this->sharedData->get('arrayable-key')); // Closure (lazy) $this->lazy = 'foo'; $this->sharedData->put(function () { return [ 'lazy' => $this->lazy, ]; }); $this->lazy = 'bar'; $this->assertSame('bar', $this->sharedData->get('lazy')); // Closure (lazy) with key $this->lazy = 'foo'; $this->sharedData->put('lazy-with-key', function () { return $this->lazy; }); $this->lazy = 'bar'; $this->assertSame('bar', $this->sharedData->get('lazy-with-key')); // object $object = new stdClass; $object->objectScalar = 'object-scalar'; $object->objectArray = ['nested' => 'object-scalar']; $this->sharedData->put($object); $this->assertSame('object-scalar', $this->sharedData->get('objectScalar')); $this->assertSame(['nested' => 'object-scalar'], $this->sharedData->get('objectArray')); } public function testGet() { $values = [ 'scalar' => 'scalar-value', 'array' => ['nested' => 'value'], ]; $this->sharedData->put($values); $this->assertSame('scalar-value', $this->sharedData->get('scalar')); $this->assertSame($values, $this->sharedData->get()); } public function testToJson() { $this->sharedData->put([ 'scalar' => 'scalar-value', 'array' => ['nested' => 'value'], ]); $json = $this->sharedData->toJson(); $expectedJson = '{"scalar":"scalar-value","array":{"nested":"value"}}'; $this->assertSame($expectedJson, $json); } public function testRender() { $this->sharedData->put([ 'scalar' => 'scalar-value', 'array' => ['nested' => 'value'], ]); $this->sharedData->setJsNamespace('customShareDataNamespace'); $this->sharedData->setJsHelperName('customSharedFunctionName'); $this->sharedData->setJsHelperEnabled(true); $html = $this->sharedData->render(); $expectedHtml = ''; $this->assertSame($expectedHtml, $html); } public function testRenderWithAttributes() { $this->sharedData->put([ 'scalar' => 'scalar-value', 'array' => ['nested' => 'value'], ]); $this->sharedData->setJsNamespace('customShareDataNamespace'); $this->sharedData->setJsHelperName('customSharedFunctionName'); $this->sharedData->setJsHelperEnabled(true); $html = $this->sharedData->render(['attributes' => ['nonce' => 'HELLOWORLD">', 'data-hello' => 'world']]); $expectedHtml = ''; $this->assertSame($expectedHtml, $html); } public function testToString() { $this->sharedData->put('foo', 'bar'); $this->assertSame($this->sharedData->render(), (string) $this->sharedData); } public function testJsNamespace() { $this->assertSame('sharedData', $this->sharedData->getJsNamespace()); $this->sharedData->setJsNamespace('foo'); $this->assertSame('foo', $this->sharedData->getJsNamespace()); } public function testJsHelperName() { $this->assertSame('shared', $this->sharedData->getJsHelperName()); $this->sharedData->setJsHelperName('customShared'); $this->assertSame('customShared', $this->sharedData->getJsHelperName()); } public function testJsHelperEnabled() { $this->assertSame(true, $this->sharedData->getJsHelperEnabled()); $this->sharedData->setJsHelperEnabled(false); $this->assertSame(false, $this->sharedData->getJsHelperEnabled()); } public function testJsHelper() { $this->sharedData->setJsHelperName('customSharedFunctionName'); $this->assertSame('window["customSharedFunctionName"]=function(e){var n=void 0!==arguments[1]?arguments[1]:null;return[window.sharedDataNamespace].concat("string"==typeof e?e.split("."):[]).reduce(function(e,t){return e===n||"object"!=typeof e||void 0===e[t]?n:e[t]},window)}', $this->sharedData->getJsHelper()); } public function testToArray() { $this->sharedData->put('foo', ['bar' => 'baz']); $this->assertSame(['foo' => ['bar' => 'baz']], $this->sharedData->toArray()); } public function testJsonSerialize() { $this->sharedData->put('foo', ['bar' => 'baz']); $this->assertSame(['foo' => ['bar' => 'baz']], $this->sharedData->jsonSerialize()); } public function testOffsetExists() { $this->sharedData->put('foo.baz', 'bar'); $this->assertTrue(isset($this->sharedData['foo.baz'])); $this->assertFalse(isset($this->sharedData['baz.foo'])); } public function testOffsetGet() { $this->sharedData->put('foo.bar', 'baz'); $this->assertSame('baz', $this->sharedData['foo.bar']); } public function testOffsetSet() { $this->sharedData['foo.bar'] = 'baz'; $this->assertSame( [ 'foo' => [ 'bar' => 'baz', ], ], $this->sharedData->get() ); } public function testOffsetUnset() { $this->sharedData->put([ 'foo' => [ 'bar' => 'baz', 'baz' => 'bar', ], ]); unset($this->sharedData['foo.baz']); $this->assertSame( [ 'foo' => [ 'bar' => 'baz', ], ], $this->sharedData->get() ); } public function testForget() { $this->sharedData->put([ 'foo' => [ 'bar' => 'baz', 'baz' => 'bar', ], ]); $this->sharedData->forget('foo.baz'); $this->assertSame( [ 'foo' => [ 'bar' => 'baz', ], ], $this->sharedData->get() ); $this->sharedData->forget(); $this->assertSame([], $this->sharedData->get()); } public function testBladeDirective() { $this->assertEquals( shared()->render(), view('shared')->render() ); } /** * @depends testBladeDirective */ public function testBladeDirectiveWithCustomName() { $this->app['config']['shared-data.blade_directive.name'] = 'shared_custom'; $this->assertEquals( shared()->render(), view('shared_custom')->render() ); } } ================================================ FILE: tests/views/shared.blade.php ================================================ @shared ================================================ FILE: tests/views/shared_custom.blade.php ================================================ @shared_custom