-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Ilya Sakovich
committed
Mar 18, 2019
0 parents
commit 298c3eb
Showing
14 changed files
with
316 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
/.gitattributes export-ignore | ||
/.gitignore export-ignore | ||
/.styleci.yml export-ignore | ||
/phpunit.xml.dist export-ignore | ||
/tests export-ignore |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
vendor | ||
composer.lock | ||
.idea |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
preset: laravel |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
# 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!** |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
# Laravel Shared Data | ||
|
||
**Laravel Shared Data** provides an easy way to share the data from your backend to the JavaScript. | ||
|
||
## Installation | ||
|
||
You can install this package via composer using this command: | ||
|
||
```bash | ||
composer require coderello/laravel-shared-data | ||
``` | ||
|
||
The package will automatically register itself. | ||
|
||
## Testing | ||
|
||
You can run the tests with: | ||
|
||
```bash | ||
composer test | ||
``` | ||
|
||
## Changelog | ||
|
||
Please see [CHANGELOG](CHANGELOG.md) for more information what has changed recently. | ||
|
||
## Contributing | ||
|
||
Please see [CONTRIBUTING](CONTRIBUTING.md) for details. | ||
|
||
## License | ||
|
||
The MIT License (MIT). Please see [License File](LICENSE.md) for more information. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
{ | ||
"name": "coderello/laravel-shared-data", | ||
"description": "Package for sharing data from Laravel to JavaScript.", | ||
"type": "library", | ||
"license": "MIT", | ||
"require": { | ||
"php": ">=7.1", | ||
"laravel/framework": "5.5.*|5.6.*|5.7.*|5.8.*", | ||
"ext-json": "*" | ||
}, | ||
"require-dev": { | ||
"orchestra/testbench": "~3.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" | ||
] | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<?php | ||
|
||
return [ | ||
|
||
/* | ||
* JavaScript namespace. | ||
* | ||
* By default the namespace is equal to 'sharedData'. | ||
* | ||
* It means that the shared data will be accessible from window.sharedData | ||
*/ | ||
'js_namespace' => 'sharedData', | ||
|
||
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<phpunit backupGlobals="false" | ||
backupStaticAttributes="false" | ||
beStrictAboutTestsThatDoNotTestAnything="false" | ||
bootstrap="vendor/autoload.php" | ||
colors="true" | ||
convertErrorsToExceptions="true" | ||
convertNoticesToExceptions="true" | ||
convertWarningsToExceptions="true" | ||
processIsolation="false" | ||
stopOnFailure="false" | ||
> | ||
<testsuites> | ||
<testsuite name="Laravel Shared Data Test Suite"> | ||
<directory suffix=".php">./tests/</directory> | ||
</testsuite> | ||
</testsuites> | ||
<filter> | ||
<whitelist processUncoveredFilesFromWhitelist="true"> | ||
<directory suffix=".php">./src/</directory> | ||
</whitelist> | ||
</filter> | ||
</phpunit> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?php | ||
|
||
namespace Coderello\SharedData\Facades; | ||
|
||
use Illuminate\Support\Facades\Facade; | ||
|
||
class SharedData extends Facade | ||
{ | ||
/** | ||
* Get the registered name of the component. | ||
* | ||
* @return string | ||
*/ | ||
protected static function getFacadeAccessor() | ||
{ | ||
return \Coderello\SharedData\SharedData::class; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
<?php | ||
|
||
namespace Coderello\SharedData\Providers; | ||
|
||
use Coderello\SharedData\SharedData; | ||
use Illuminate\Support\ServiceProvider; | ||
|
||
class SharedDataServiceProvider extends ServiceProvider | ||
{ | ||
/** | ||
* Bootstrap any application services. | ||
* | ||
* @return void | ||
*/ | ||
public function boot() | ||
{ | ||
$this->publishes([ | ||
__DIR__.'/../../config/shared_data.php' => config_path('shared_data.php'), | ||
], 'shared-data-config'); | ||
} | ||
|
||
/** | ||
* Register any application services. | ||
* | ||
* @return void | ||
*/ | ||
public function register() | ||
{ | ||
$this->mergeConfigFrom( | ||
__DIR__.'/../../config/shared_data.php', | ||
'shared_data' | ||
); | ||
|
||
$this->app->singleton(SharedData::class, function () { | ||
return new SharedData($this->app['config']['shared_data']); | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
<?php | ||
|
||
namespace Coderello\SharedData; | ||
|
||
use Illuminate\Contracts\Support\Jsonable; | ||
use Illuminate\Contracts\Support\Renderable; | ||
use Illuminate\Support\Arr; | ||
use LogicException; | ||
|
||
class SharedData implements Renderable, Jsonable | ||
{ | ||
protected $data = []; | ||
|
||
protected $config = []; | ||
|
||
public function __construct(array $config) | ||
{ | ||
$this->config = $config; | ||
} | ||
|
||
public function put($key, $value = null) | ||
{ | ||
if (is_scalar($key)) { | ||
Arr::set($this->data, $key, $value); | ||
} elseif (is_iterable($key)) { | ||
foreach ($key as $nestedKey => $nestedValue) { | ||
$this->put($nestedKey, $nestedValue); | ||
} | ||
} elseif (is_object($key)) { | ||
$this->put(get_object_vars($key)); | ||
} else { | ||
throw new \InvalidArgumentException(); | ||
} | ||
|
||
return $this; | ||
} | ||
|
||
public function get($key = null) | ||
{ | ||
if (is_null($key)) { | ||
return $this->data; | ||
} else { | ||
return Arr::get($this->data, $key); | ||
} | ||
} | ||
|
||
protected function getNamespace() | ||
{ | ||
if (! ($namespace = Arr::get($this->config, 'js_namespace'))) { | ||
throw new LogicException('JavaScript namespace should be specified in the config file.'); | ||
} | ||
|
||
return $namespace; | ||
} | ||
|
||
public function toJson($options = 0) | ||
{ | ||
return json_encode($this->get(), $options); | ||
} | ||
|
||
public function render() | ||
{ | ||
return '<script>window[\''.$this->getNamespace().'\'] = '.$this->toJson().';</script>'; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<?php | ||
|
||
if (! function_exists('shared')) { | ||
/** | ||
* @return \Coderello\SharedData\SharedData | ||
*/ | ||
function shared() | ||
{ | ||
return app(\Coderello\SharedData\SharedData::class); | ||
} | ||
} | ||
|
||
if (! function_exists('share')) { | ||
/** | ||
* @param array $args | ||
* @return \Coderello\SharedData\SharedData | ||
*/ | ||
function share(...$args) | ||
{ | ||
return app(\Coderello\SharedData\SharedData::class)->put(...$args); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<?php | ||
|
||
namespace Coderello\SharedData\Tests; | ||
|
||
use Coderello\SharedData\Providers\SharedDataServiceProvider; | ||
use Orchestra\Testbench\TestCase as OrchestraTestCase; | ||
|
||
abstract class AbstractTestCase extends OrchestraTestCase | ||
{ | ||
protected function getPackageProviders($app) | ||
{ | ||
return [ | ||
SharedDataServiceProvider::class, | ||
]; | ||
} | ||
} |