Skip to content

Commit

Permalink
chore: readme and code style
Browse files Browse the repository at this point in the history
  • Loading branch information
gere-lajos committed May 9, 2024
1 parent e6e9a8d commit bfdbc84
Show file tree
Hide file tree
Showing 9 changed files with 97 additions and 53 deletions.
108 changes: 77 additions & 31 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,79 +5,125 @@
[![GitHub Code Style Action Status](https://img.shields.io/github/actions/workflow/status/gere-lajos/laravel-web-tinker/fix-php-code-style-issues.yml?branch=main&label=code%20style&style=flat-square)](https://github.com/gere-lajos/laravel-web-tinker/actions?query=workflow%3A"Fix+PHP+code+style+issues"+branch%3Amain)
[![Total Downloads](https://img.shields.io/packagist/dt/gere-lajos/laravel-web-tinker.svg?style=flat-square)](https://packagist.org/packages/gere-lajos/laravel-web-tinker)

This is where your description should go. Limit it to a paragraph or two. Consider adding a small example.
This package allows you to use Tinker in your browser. Wildly inspired by Spatie's [Laravel Web Tinker](https://github.com/spatie/laravel-web-tinker), but with added functionality, and React frontend.

## Support us
## 🚨 A word to the wise 🚨

[<img src="https://github-ads.s3.eu-central-1.amazonaws.com/laravel-web-tinker.jpg?t=1" width="419px" />](https://spatie.be/github-ad-click/laravel-web-tinker)
This package can run arbitrary code. Unless you know what you are doing, you should never install or use this in a production environment, or any environment where you handle real world data.

We invest a lot of resources into creating [best in class open source packages](https://spatie.be/open-source). You can support us by [buying one of our paid products](https://spatie.be/open-source/support-us).

We highly appreciate you sending us a postcard from your hometown, mentioning which of our package(s) you are using. You'll find our address on [our contact page](https://spatie.be/about-us). We publish all received postcards on [our virtual postcard wall](https://spatie.be/open-source/postcards).

## Installation

You can install the package via composer:

```bash
composer require gere-lajos/laravel-web-tinker
composer require gere-lajos/laravel-web-tinker --dev
```

You can publish and run the migrations with:
Next, you must publish the assets from this package by running this command.

```bash
php artisan vendor:publish --tag="laravel-web-tinker-migrations"
php artisan migrate
php artisan web-tinker:install
```

You can publish the config file with:
Optionally, you can publish the config file of the package.

```bash
php artisan vendor:publish --tag="laravel-web-tinker-config"
php artisan vendor:publish --provider="Spatie\WebTinker\WebTinkerServiceProvider" --tag="config"
```

This is the contents of the published config file:
This is the content that will be published to `config/web-tinker.php`

```php
return [

/*
* The web tinker page will be available on this path.
*/
'path' => '/tinker',

/*
* By default this package will only run in local development.
* Do not change this, unless you know what your are doing.
*/
'enabled' => env('APP_ENV') === 'local',

/*
* This class can modify the output returned by Tinker. You can replace this with
* any class that implements \Spatie\WebTinker\OutputModifiers\OutputModifier.
*/
'output_modifier' => \GereLajos\LaravelWebTinker\OutputModifiers\PrefixDateTime::class,

/*
* These middleware will be assigned to every WebTinker route, giving you the chance
* to add your own middlewares to this list or change any of the existing middleware.
*/
'middleware' => [
Illuminate\Cookie\Middleware\EncryptCookies::class,
Illuminate\Session\Middleware\StartSession::class,
GereLajos\LaravelWebTinker\Http\Middleware\Authorize::class,
],

/*
* If you want to fine-tune PsySH configuration specify
* configuration file name, relative to the root of your
* application directory.
*/
'config_file' => env('PSYSH_CONFIG', null),
];
```

Optionally, you can publish the views using
## Usage

```bash
php artisan vendor:publish --tag="laravel-web-tinker-views"
```
By default this package will only run in a local environment.

## Usage
Visit `/tinker` in your local environment of your app to view the tinker page.

## Authorization

Should you want to run this in another environment (we do not recommend this), there are two steps you must perform.

1. You must register a `viewWebTinker` ability. A good place to do this is in the `AuthServiceProvider` that ships with Laravel.

```php
$laravelWebTinker = new Gere Lajos\LaravelWebTinker();
echo $laravelWebTinker->echoPhrase('Hello, Gere Lajos!');
public function boot()
{
$this->registerPolicies();

Gate::define('viewWebTinker', function ($user = null) {
// return true if access to web tinker is allowed
});
}
```

## Testing
2. You must set the `enabled` variable in the `web-tinker` config file to `true`.

```bash
composer test
```
## Modifying the output

## Changelog
You can modify the output of tinker by specifying an output modifier in the `output_modifier` key of the `web-tinker` config file. An output modifier is any class that implements `\GereLajos\LaravelWebTinker\OutputModifiers\OutputModifier`.

Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently.
This is how that interface looks like.

## Contributing
```php
namespace GereLajos\LaravelWebTinker\OutputModifiers;

Please see [CONTRIBUTING](CONTRIBUTING.md) for details.
interface OutputModifier
{
public function modify(string $output = ''): string;
}
```

## Security Vulnerabilities
The default install of this package will use the `PrefixDataTime` output modifier which prefixes the output from Tinker with the current date time.

Please review [our security policy](../../security/policy) on how to report security vulnerabilities.
## Testing

``` bash
composer test
```

## Credits

- [Gere Lajos](https://github.com/gere-lajos)
- [All Contributors](../../contributors)

## License

Expand Down
5 changes: 0 additions & 5 deletions config/web-tinker.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,6 @@
*/
'path' => '/tinker',

/*
* Possible values are 'auto', 'light' and 'dark'.
*/
'theme' => 'auto',

/*
* By default this package will only run in local development.
* Do not change this, unless you know what you are doing.
Expand Down
12 changes: 7 additions & 5 deletions resources/js/Editor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,14 @@ import { useState } from "react";
import parse from "html-react-parser";

const stateFields = { history: historyField };
const editorStateKey = "editorState";
const editorValueKey = "editorValue";

export default function Editor({ path }: { path: string }) {
const [output, setOutput] = useState("");

const serializedState = localStorage.getItem("editorState");
const value = localStorage.getItem("editorValue") || "";
const serializedState = localStorage.getItem(editorStateKey);
const value = localStorage.getItem(editorValueKey) || "";

return (
<div className="grid min-h-screen w-full grid-cols-[1fr_1fr]">
Expand Down Expand Up @@ -49,7 +51,7 @@ export default function Editor({ path }: { path: string }) {
event.preventDefault();

const code = localStorage
.getItem("editorValue")
.getItem(editorValueKey)
?.trim();

if (code === "") {
Expand Down Expand Up @@ -91,11 +93,11 @@ export default function Editor({ path }: { path: string }) {
: undefined
}
onChange={(value, viewUpdate) => {
localStorage.setItem("editorValue", value);
localStorage.setItem(editorValueKey, value);

const state = viewUpdate.state.toJSON(stateFields);
localStorage.setItem(
"editorState",
editorStateKey,
JSON.stringify(state),
);
}}
Expand Down
7 changes: 5 additions & 2 deletions resources/js/main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@ import ReactDOM from "react-dom/client";
import App from "./App.tsx";
import "../css/index.css";

ReactDOM.createRoot(document.getElementById("root")!).render(
const root = document.getElementById("root");
const path = root?.dataset.path || "";

ReactDOM.createRoot(root!).render(
<React.StrictMode>
<App path={document.getElementById("root")?.dataset.path} />
<App path={path} />
</React.StrictMode>,
);
1 change: 0 additions & 1 deletion src/Commands/LaravelWebTinkerInstallCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
class LaravelWebTinkerInstallCommand extends Command
{
public $signature = 'laravel-web-tinker:install';

public $description = 'Install the Laravel Web Tinker resources';

public function handle()
Expand Down
3 changes: 2 additions & 1 deletion src/Controllers/WebTinkerController.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@

class WebTinkerController
{
public function index(){
public function index()
{
return view('web-tinker::web-tinker');
}

Expand Down
2 changes: 1 addition & 1 deletion src/Http/Middleware/Authorize.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public function handle($request, $next)

protected function allowedToUseTinker(): bool
{
if (! config('web-tinker.enabled')) {
if (!config('web-tinker.enabled')) {
return false;
}

Expand Down
10 changes: 4 additions & 6 deletions src/LaravelWebTinker.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,8 @@ class LaravelWebTinker
{
/** @var \Symfony\Component\Console\Output\BufferedOutput */
protected $output;

/** @var \Psy\Shell */
protected $shell;

/** @var \GereLajos\LaravelWebTinker\OutputModifiers\OutputModifier */
protected $outputModifier;

Expand Down Expand Up @@ -51,7 +49,7 @@ protected function createShell(BufferedOutput $output): Shell
{
$config = new Configuration([
'updateCheck' => 'never',
'configFile' => config('web-tinker.config_file') !== null ? base_path().'/'.config('web-tinker.config_file') : null,
'configFile' => config('web-tinker.config_file') !== null ? base_path() . '/' . config('web-tinker.config_file') : null,
]);

$config->setHistoryFile(defined('PHP_WINDOWS_VERSION_BUILD') ? 'null' : '/dev/null');
Expand All @@ -77,16 +75,16 @@ protected function createShell(BufferedOutput $output): Shell

public function removeComments(string $code): string
{
$tokens = collect(token_get_all("<?php\n".$code.'?>'));
$tokens = collect(token_get_all("<?php\n" . $code . '?>'));

return $tokens->reduce(function ($carry, $token) {
if (is_string($token)) {
return $carry.$token;
return $carry . $token;
}

$text = $this->ignoreCommentsAndPhpTags($token);

return $carry.$text;
return $carry . $text;
}, '');
}

Expand Down
2 changes: 1 addition & 1 deletion src/OutputModifiers/PrefixDateTime.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ class PrefixDateTime implements OutputModifier
{
public function modify(string $output = ''): string
{
return '<span style="color:rgba(255,255,255,0.2);font-style:italic">'.now()->format('Y-m-d H:i:s').'</span><br>'.$output;
return '<span style="color:rgba(255,255,255,0.2);font-style:italic">' . now()->format('Y-m-d H:i:s') . '</span><br>' . $output;
}
}

0 comments on commit bfdbc84

Please sign in to comment.