├── .github ├── FUNDING.yml └── workflows │ └── ci.yml ├── .gitignore ├── .php-cs-fixer.php ├── README.md ├── check ├── composer.json ├── phpunit.xml ├── src ├── ConsoleLog.php └── ConsoleLogServiceProvider.php ├── tests ├── EnvironmentTest.php ├── LivewireTest.php ├── Pest.php ├── TestCase.php └── views │ ├── default.blade.php │ ├── livewire-component.blade.php │ ├── local-and-staging.blade.php │ └── production.blade.php └── views └── components └── console-log.blade.php /.github/FUNDING.yml: -------------------------------------------------------------------------------- 1 | # These are supported funding model platforms 2 | 3 | github: stancl 4 | -------------------------------------------------------------------------------- /.github/workflows/ci.yml: -------------------------------------------------------------------------------- 1 | name: CI 2 | 3 | on: 4 | push: 5 | pull_request: 6 | branches: [ master ] 7 | 8 | jobs: 9 | pest: 10 | name: Tests (Pest) L${{ matrix.laravel }} 11 | runs-on: ubuntu-latest 12 | strategy: 13 | matrix: 14 | laravel: [9, 10] 15 | 16 | steps: 17 | - name: Checkout code 18 | uses: actions/checkout@v2 19 | 20 | - name: Install dependencies 21 | run: composer require "laravel/framework:^${{matrix.laravel}}.0" 22 | - name: Run tests 23 | run: vendor/bin/pest 24 | 25 | php-cs-fixer: 26 | name: Code style (php-cs-fixer) 27 | runs-on: ubuntu-latest 28 | steps: 29 | - uses: actions/checkout@v2 30 | - name: Install php-cs-fixer 31 | run: composer global require friendsofphp/php-cs-fixer 32 | - name: Run php-cs-fixer 33 | run: $HOME/.composer/vendor/bin/php-cs-fixer fix --config=.php-cs-fixer.php 34 | - name: Commit changes from php-cs-fixer 35 | uses: EndBug/add-and-commit@v5 36 | with: 37 | author_name: Samuel Štancl 38 | author_email: samuel.stancl@gmail.com 39 | message: Fix code style (php-cs-fixer) 40 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | vendor/ 2 | composer.lock 3 | .phpunit.result.cache 4 | .php-cs-fixer.cache 5 | -------------------------------------------------------------------------------- /.php-cs-fixer.php: -------------------------------------------------------------------------------- 1 | ['syntax' => 'short'], 8 | 'binary_operator_spaces' => [ 9 | 'default' => 'single_space', 10 | 'operators' => [ 11 | '=>' => null, 12 | '|' => 'no_space', 13 | ], 14 | ], 15 | 'blank_line_after_namespace' => true, 16 | 'blank_line_after_opening_tag' => true, 17 | 'no_superfluous_phpdoc_tags' => true, 18 | 'blank_line_before_statement' => [ 19 | 'statements' => ['return'], 20 | ], 21 | 'braces' => true, 22 | 'cast_spaces' => true, 23 | 'class_definition' => true, 24 | 'concat_space' => [ 25 | 'spacing' => 'one', 26 | ], 27 | 'declare_equal_normalize' => true, 28 | 'elseif' => true, 29 | 'encoding' => true, 30 | 'full_opening_tag' => true, 31 | 'declare_strict_types' => true, 32 | 'fully_qualified_strict_types' => true, // added by Shift 33 | 'function_declaration' => true, 34 | 'function_typehint_space' => true, 35 | 'heredoc_to_nowdoc' => true, 36 | 'include' => true, 37 | 'increment_style' => ['style' => 'post'], 38 | 'indentation_type' => true, 39 | 'linebreak_after_opening_tag' => true, 40 | 'line_ending' => true, 41 | 'lowercase_cast' => true, 42 | 'constant_case' => true, 43 | 'lowercase_keywords' => true, 44 | 'lowercase_static_reference' => true, // added from Symfony 45 | 'magic_method_casing' => true, // added from Symfony 46 | 'magic_constant_casing' => true, 47 | 'method_argument_space' => true, 48 | 'native_function_casing' => true, 49 | 'no_alias_functions' => true, 50 | 'no_extra_blank_lines' => [ 51 | 'tokens' => [ 52 | 'extra', 53 | 'throw', 54 | 'use', 55 | 'use_trait', 56 | ], 57 | ], 58 | 'no_blank_lines_after_class_opening' => true, 59 | 'no_blank_lines_after_phpdoc' => true, 60 | 'no_closing_tag' => true, 61 | 'no_empty_phpdoc' => true, 62 | 'no_empty_statement' => true, 63 | 'no_leading_import_slash' => true, 64 | 'no_leading_namespace_whitespace' => true, 65 | 'no_mixed_echo_print' => [ 66 | 'use' => 'echo', 67 | ], 68 | 'no_multiline_whitespace_around_double_arrow' => true, 69 | 'multiline_whitespace_before_semicolons' => [ 70 | 'strategy' => 'no_multi_line', 71 | ], 72 | 'no_short_bool_cast' => true, 73 | 'no_singleline_whitespace_before_semicolons' => true, 74 | 'no_spaces_after_function_name' => true, 75 | 'no_spaces_around_offset' => true, 76 | 'no_spaces_inside_parenthesis' => true, 77 | 'no_trailing_comma_in_list_call' => true, 78 | 'no_trailing_comma_in_singleline_array' => true, 79 | 'no_trailing_whitespace' => true, 80 | 'no_trailing_whitespace_in_comment' => true, 81 | 'no_unneeded_control_parentheses' => true, 82 | 'no_unreachable_default_argument_value' => true, 83 | 'no_useless_return' => true, 84 | 'no_whitespace_before_comma_in_array' => true, 85 | 'no_whitespace_in_blank_line' => true, 86 | 'normalize_index_brace' => true, 87 | 'not_operator_with_successor_space' => true, 88 | 'object_operator_without_whitespace' => true, 89 | 'ordered_imports' => ['sort_algorithm' => 'alpha'], 90 | 'phpdoc_indent' => true, 91 | 'general_phpdoc_tag_rename' => true, 92 | 'phpdoc_no_access' => true, 93 | 'phpdoc_no_package' => true, 94 | 'phpdoc_no_useless_inheritdoc' => true, 95 | 'phpdoc_scalar' => true, 96 | 'phpdoc_single_line_var_spacing' => true, 97 | 'phpdoc_summary' => true, 98 | 'phpdoc_to_comment' => false, 99 | 'phpdoc_trim' => true, 100 | 'phpdoc_types' => true, 101 | 'phpdoc_var_without_name' => true, 102 | 'psr_autoloading' => true, 103 | 'self_accessor' => true, 104 | 'short_scalar_cast' => true, 105 | 'simplified_null_return' => false, // disabled by Shift 106 | 'single_blank_line_at_eof' => true, 107 | 'single_blank_line_before_namespace' => true, 108 | 'single_class_element_per_statement' => true, 109 | 'single_import_per_statement' => false, 110 | 'single_line_after_imports' => true, 111 | 'no_unused_imports' => true, 112 | 'single_line_comment_style' => [ 113 | 'comment_types' => ['hash'], 114 | ], 115 | 'single_quote' => true, 116 | 'space_after_semicolon' => true, 117 | 'standardize_not_equals' => true, 118 | 'switch_case_semicolon_to_colon' => true, 119 | 'switch_case_space' => true, 120 | 'ternary_operator_spaces' => true, 121 | 'trailing_comma_in_multiline' => true, 122 | 'trim_array_spaces' => true, 123 | 'unary_operator_spaces' => true, 124 | 'whitespace_after_comma_in_array' => true, 125 | ]; 126 | 127 | $project_path = getcwd(); 128 | $finder = Finder::create() 129 | ->in([ 130 | $project_path . '/src', 131 | ]) 132 | ->name('*.php') 133 | ->notName('*.blade.php') 134 | ->ignoreDotFiles(true) 135 | ->ignoreVCS(true); 136 | 137 | return (new Config()) 138 | ->setFinder($finder) 139 | ->setRules($rules) 140 | ->setRiskyAllowed(true) 141 | ->setUsingCache(true); 142 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # console-log 2 | 3 | A tiny package that adds a `consoleLog()` method to Livewire. This method sends any data to the browser developer console. 4 | 5 | ## Installation 6 | 7 | Require the package via composer: 8 | 9 | ```bash 10 | composer require leanadmin/console-log 11 | ``` 12 | 13 | Add this to your base layout: 14 | 15 | ```html 16 | 17 | ``` 18 | 19 | By default, events will only show up if your application is in the `local` environment. If you wish to change that, pass an `environment` attribute to the component: 20 | 21 | ```html 22 | 23 | 24 | 25 | ``` 26 | 27 | ## Usage 28 | 29 | In any Livewire component, you can use the `consoleLog()` method to log a value (or values) to the browser console: 30 | 31 | ```php 32 | $this->consoleLog('foo'); 33 | $this->consoleLog($value); 34 | 35 | $this->consoleLog('foo', 'bar'); 36 | $this->consoleLog($values); 37 | ``` 38 | 39 | ## IDE support 40 | 41 | Since the package adds a macro, you will not have IDE autosuggest for the `consoleLog()` method by default. 42 | 43 | However, if you wish to add it, simply use the `ConsoleLog` trait: 44 | 45 | ```php 46 | use Lean\ConsoleLog\ConsoleLog; 47 | 48 | class MyComponent extends Component 49 | { 50 | use ConsoleLog; 51 | } 52 | ``` 53 | 54 | This trait has a `@method` annotation which lets your IDE understand the method. 55 | -------------------------------------------------------------------------------- /check: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | set -e 3 | 4 | offer_run() { 5 | read -p "For more output, run $1. Run it now (Y/n)? " run 6 | 7 | case ${run:0:1} in 8 | n|N ) 9 | exit 1 10 | ;; 11 | * ) 12 | $1 13 | ;; 14 | esac 15 | 16 | exit 1 17 | } 18 | 19 | if (php-cs-fixer fix --dry-run --config=.php-cs-fixer.php > /dev/null 2>/dev/null); then 20 | echo '✅ php-cs-fixer OK' 21 | else 22 | read -p "⚠️ php-cs-fixer found issues. Fix (Y/n)? " fix 23 | case ${fix:0:1} in 24 | n|N ) 25 | echo '❌ php-cs-fixer FAIL' 26 | offer_run 'php-cs-fixer fix --config=.php-cs-fixer.php' 27 | ;; 28 | * ) 29 | if (php-cs-fixer fix --config=.php-cs-fixer.php > /dev/null 2>/dev/null); then 30 | echo '✅ php-cs-fixer OK' 31 | else 32 | echo '❌ php-cs-fixer FAIL' 33 | offer_run 'php-cs-fixer fix --config=.php-cs-fixer.php' 34 | fi 35 | ;; 36 | esac 37 | fi 38 | 39 | if (./vendor/bin/pest > /dev/null 2>/dev/null); then 40 | echo '✅ Pest OK' 41 | else 42 | echo '❌ Pest FAIL' 43 | offer_run './vendor/bin/pest' 44 | fi 45 | 46 | echo '==================' 47 | echo '✅ Everything OK' 48 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "leanadmin/console-log", 3 | "description": "consoleLog() for Livewire components", 4 | "type": "library", 5 | "license": "MIT", 6 | "autoload": { 7 | "psr-4": { 8 | "Lean\\ConsoleLog\\": "src/" 9 | } 10 | }, 11 | "autoload-dev": { 12 | "psr-4": { 13 | "Lean\\ConsoleLog\\Tests\\": "tests/" 14 | } 15 | }, 16 | "authors": [ 17 | { 18 | "name": "Samuel Štancl", 19 | "email": "samuel.stancl@gmail.com" 20 | } 21 | ], 22 | "require": { 23 | "livewire/livewire": "^2.3", 24 | "illuminate/view": "^9.0|^10.0" 25 | }, 26 | "require-dev": { 27 | "pestphp/pest": "^1.0|^2.0", 28 | "illuminate/support": "^9.0|^10.0", 29 | "pestphp/pest-plugin-laravel": "^1.0|^2.0", 30 | "illuminate/testing": "^9.0|^10.0", 31 | "orchestra/testbench": "^7.0|^8.0", 32 | "pestphp/pest-plugin-livewire": "^1.1|^2.0" 33 | }, 34 | "extra": { 35 | "laravel": { 36 | "providers": [ 37 | "Lean\\ConsoleLog\\ConsoleLogServiceProvider" 38 | ] 39 | } 40 | }, 41 | "minimum-stability": "dev", 42 | "prefer-stable": true, 43 | "config": { 44 | "allow-plugins": { 45 | "pestphp/pest-plugin": true 46 | } 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /phpunit.xml: -------------------------------------------------------------------------------- 1 | 2 | 7 | 8 | 9 | ./tests 10 | 11 | 12 | 13 | 14 | ./app 15 | ./src 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | -------------------------------------------------------------------------------- /src/ConsoleLog.php: -------------------------------------------------------------------------------- 1 | loadViewsFrom(__DIR__ . '/../views', 'lean'); 19 | 20 | Component::macro('consoleLog', function (...$data) { 21 | /** @var Component $this */ 22 | if (count($data) === 1) { 23 | $data = $data[0]; 24 | } 25 | 26 | $this->dispatchBrowserEvent('lean-debug', $data); 27 | }); 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /tests/EnvironmentTest.php: -------------------------------------------------------------------------------- 1 | addNamespace('lean-test', __DIR__ . '/views'); 9 | }); 10 | 11 | it('only works in local environment by default', function () { 12 | $this->testView('default'); 13 | 14 | environment('local'); 15 | $this->assertListenerRendered(); 16 | 17 | environment('staging'); 18 | $this->assertListenerNotRendered(); 19 | 20 | environment('production'); 21 | $this->assertListenerNotRendered(); 22 | }); 23 | 24 | it('can be configured to only work in one specific environment', function () { 25 | $this->testView('production'); 26 | 27 | environment('local'); 28 | $this->assertListenerNotRendered(); 29 | 30 | environment('staging'); 31 | $this->assertListenerNotRendered(); 32 | 33 | environment('production'); 34 | $this->assertListenerRendered(); 35 | }); 36 | 37 | it('can be configured to only in multiple specific environments', function () { 38 | $this->testView('local-and-staging'); 39 | 40 | environment('local'); 41 | $this->assertListenerRendered(); 42 | 43 | environment('staging'); 44 | $this->assertListenerRendered(); 45 | 46 | environment('production'); 47 | $this->assertListenerNotRendered(); 48 | }); 49 | -------------------------------------------------------------------------------- /tests/LivewireTest.php: -------------------------------------------------------------------------------- 1 | addNamespace('lean-test', __DIR__ . '/views'); 13 | }); 14 | 15 | it('sends the event to the browser', function () { 16 | livewire(TestComponent::class) 17 | ->call('dumpString') 18 | ->assertDispatchedBrowserEvent('lean-debug'); 19 | }); 20 | 21 | it('can accept multiple values', function () { 22 | livewire(TestComponent::class) 23 | ->call('dumpArray') 24 | ->assertDispatchedBrowserEvent('lean-debug', ['foo', 'bar']); 25 | }); 26 | 27 | it('displays a single value as a single value and not an arary', function () { 28 | livewire(TestComponent::class) 29 | ->call('dumpString') 30 | ->assertDispatchedBrowserEvent('lean-debug', 'foo'); 31 | }); 32 | 33 | ////////////////////////////////////////////////////////////////// 34 | 35 | class TestComponent extends Component 36 | { 37 | public function dumpString() 38 | { 39 | $this->consoleLog('foo'); 40 | } 41 | 42 | public function dumpArray() 43 | { 44 | $this->consoleLog(['foo', 'bar']); 45 | } 46 | 47 | public function render() 48 | { 49 | return view('lean-test::livewire-component'); 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /tests/Pest.php: -------------------------------------------------------------------------------- 1 | 'lean-test::default', 11 | 'local-and-staging' => 'lean-test::local-and-staging', 12 | 'production' => 'lean-test::production', 13 | ][$component] ?? $component; 14 | 15 | return view($component, [ 16 | 'attributes' => new ComponentAttributeBag([]), 17 | ])->toHtml(); 18 | } 19 | 20 | function environment(string $env) 21 | { 22 | app()['env'] = $env; 23 | } 24 | -------------------------------------------------------------------------------- /tests/TestCase.php: -------------------------------------------------------------------------------- 1 | assertTrue( 25 | Str::contains(renderComponent($this->testView), 'window.addEventListener') 26 | ); 27 | } 28 | 29 | protected function assertListenerNotRendered() 30 | { 31 | $this->assertFalse( 32 | Str::contains(renderComponent($this->testView), 'window.addEventListener') 33 | ); 34 | } 35 | 36 | protected function testView(string $view): void 37 | { 38 | $this->testView = $view; 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /tests/views/default.blade.php: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /tests/views/livewire-component.blade.php: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/archtechx/console-log/729c11cf07115823134a8151f9e28c7cfb9de0cb/tests/views/livewire-component.blade.php -------------------------------------------------------------------------------- /tests/views/local-and-staging.blade.php: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /tests/views/production.blade.php: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /views/components/console-log.blade.php: -------------------------------------------------------------------------------- 1 | @props([ 2 | 'environment' => 'local', // The environment(s) in which we display the values 3 | ]) 4 | 5 | {{-- We make sure we're working with an array --}} 6 | @php($environments = is_array($environment) ? $environment : [$environment]) 7 | 8 | @env($environments) 9 | 12 | @endenv 13 | --------------------------------------------------------------------------------