From e50e22434f96309a9a3b9a315679574468dca540 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Samuel=20=C5=A0tancl?= Date: Wed, 17 Mar 2021 17:28:17 +0100 Subject: [PATCH] initial --- .github/workflows/ci.yml | 18 ----------- README.md | 41 +++++++++++++++++++++---- check | 16 ---------- composer.json | 20 ++++--------- docker-compose.yml | 12 -------- phpstan.neon | 25 ---------------- src/FrontendAccess.php | 12 ++++++++ src/PackageServiceProvider.php | 32 -------------------- src/WithExplicitAccess.php | 23 ++++++++++++++ tests/LivewireAccessTest.php | 55 ++++++++++++++++++++++++++++++++++ tests/TestCase.php | 16 ---------- tests/TestComponent.php | 27 +++++++++++++++++ 12 files changed, 159 insertions(+), 138 deletions(-) delete mode 100644 docker-compose.yml delete mode 100644 phpstan.neon create mode 100644 src/FrontendAccess.php delete mode 100644 src/PackageServiceProvider.php create mode 100644 src/WithExplicitAccess.php create mode 100644 tests/LivewireAccessTest.php delete mode 100644 tests/TestCase.php create mode 100644 tests/TestComponent.php diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 5440ff4..9f71337 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -3,7 +3,6 @@ name: CI env: COMPOSE_INTERACTIVE_NO_CLI: 1 PHP_CS_FIXER_IGNORE_ENV: 1 - MYSQL_PORT: 3307 GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} on: @@ -18,17 +17,11 @@ jobs: steps: - uses: actions/checkout@v2 - - name: Start docker containers - run: docker-compose up -d - name: Install composer dependencies run: composer install - name: Run tests run: vendor/bin/phpunit - psalm: - name: Static analysis (Psalm) - runs-on: ubuntu-latest - steps: - uses: actions/checkout@v2 - name: Install composer dependencies @@ -36,17 +29,6 @@ jobs: - name: Run psalm run: vendor/bin/psalm - phpstan: - name: Static analysis (PHPStan) - runs-on: ubuntu-latest - - steps: - - uses: actions/checkout@v2 - - name: Install composer dependencies - run: composer install - - name: Run phpstan - run: vendor/bin/phpstan analyse - php-cs-fixer: name: Code style (php-cs-fixer) runs-on: ubuntu-latest diff --git a/README.md b/README.md index 18e74e0..2d49df2 100644 --- a/README.md +++ b/README.md @@ -1,17 +1,50 @@ -# Package +# Livewire Access + +This package adds PHP 8.0 attribute support to Livewire. In specific, the attributes are used for flagging component properties and methods as *frontend-accessible*. + +Components which implement the trait provided by this package will implicitly deny access to all properties and methods if they don't have the `#[FrontendAccess]` attribute, regardless of their visibility. ## Installation ```sh -composer require stancl/package +composer require leanadmin/livewire-access ``` ## Usage ```php -// ... +use Livewire\Component; +use Lean\LivewireAccess\WithExplicitAccess; +use Lean\LivewireAccess\FrontendAccess; + +class MyComponent extends Component +{ + // Use the trait on your component to enable this functionality + use WithExplicitAccess; + + // Accessing this from the frontend will throw an exception + public string $inaccessible; + + #[FrontendAccess] + public string $accessible; // This property allows frontend access + + public function secretMethod() + { + // Calling this from the frontend will throw an exception + } + + #[FrontendAccess] + public function secretMethod() + { + // This method allows frontend access + } +} ``` +The properties still need to be `public` to be accessible. + +The thrown exceptions are identical to those that Livewire would throw if the properties/methods were not public. + ## Development Running all checks locally: @@ -23,8 +56,6 @@ Running all checks locally: Running tests: ```sh -MYSQL_PORT=3307 docker-compose up -d - phpunit ``` diff --git a/check b/check index 60ec827..16a81b1 100755 --- a/check +++ b/check @@ -36,22 +36,6 @@ else esac fi -if (./vendor/bin/psalm > /dev/null 2>/dev/null); then - echo '✅ Psalm OK' -else - echo '❌ Psalm FAIL' - offer_run './vendor/bin/psalm' -fi - -if (./vendor/bin/phpstan analyse > /dev/null 2>/dev/null); then - echo '✅ PHPStan OK' -else - echo '❌ PHPStan FAIL' - offer_run './vendor/bin/phpstan analyse' -fi - -(MYSQL_PORT=3307 docker-compose up -d > /dev/null 2>/dev/null) || true - if (./vendor/bin/phpunit > /dev/null 2>/dev/null); then echo '✅ PHPUnit OK' else diff --git a/composer.json b/composer.json index 62b2546..3e2d91d 100644 --- a/composer.json +++ b/composer.json @@ -1,5 +1,5 @@ { - "name": "stancl/package", + "name": "leanadmin/livewire-access", "description": "", "type": "library", "license": "MIT", @@ -11,27 +11,19 @@ ], "autoload": { "psr-4": { - "Stancl\\Package\\": "src/" + "Lean\\LivewireAccess\\": "src/" } }, "autoload-dev": { "psr-4": { - "Stancl\\Package\\Tests\\": "tests/" + "Lean\\LivewireAccess\\Tests\\": "tests/" } }, "require": { - "illuminate/support": "^8.24" + "php": "^8.0", + "livewire/livewire": "^2.4" }, "require-dev": { - "orchestra/testbench": "^6.9", - "vimeo/psalm": "^4.2", - "nunomaduro/larastan": "^0.6.10" - }, - "extra": { - "laravel": { - "providers": [ - "Stancl\\Package\\PackageServiceProvider" - ] - } + "orchestra/testbench": "^6.9" } } diff --git a/docker-compose.yml b/docker-compose.yml deleted file mode 100644 index 45edba6..0000000 --- a/docker-compose.yml +++ /dev/null @@ -1,12 +0,0 @@ -version: '3' -services: - mysql: - image: mysql:5.7 - environment: - MYSQL_ROOT_PASSWORD: password - MYSQL_DATABASE: main - MYSQL_USER: user - MYSQL_PASSWORD: password - MYSQL_TCP_PORT: ${MYSQL_PORT} - ports: - - "${MYSQL_PORT}:${MYSQL_PORT}" diff --git a/phpstan.neon b/phpstan.neon deleted file mode 100644 index 228f911..0000000 --- a/phpstan.neon +++ /dev/null @@ -1,25 +0,0 @@ -includes: - - ./vendor/nunomaduro/larastan/extension.neon - -parameters: - paths: - - src - - tests - - level: 8 - - universalObjectCratesClasses: - - Illuminate\Routing\Route - - ignoreErrors: - # - - # message: '#Offset (.*?) does not exist on array\|null#' - # paths: - # - tests/* - # - - # message: '#expects resource, resource\|false given#' - # paths: - # - tests/* - # - '#should return \$this#' - - checkMissingIterableValueType: false diff --git a/src/FrontendAccess.php b/src/FrontendAccess.php new file mode 100644 index 0000000..1eaf540 --- /dev/null +++ b/src/FrontendAccess.php @@ -0,0 +1,12 @@ +loadViewsFrom(__DIR__ . '/../assets/views', 'package'); - - // $this->publishes([ - // __DIR__ . '/../assets/views' => resource_path('views/vendor/package'), - // ], 'package-views'); - - // $this->mergeConfigFrom( - // __DIR__ . '/../assets/package.php', - // 'package' - // ); - - // $this->publishes([ - // __DIR__ . '/../assets/package.php' => config_path('package.php'), - // ], 'package-config'); - } -} diff --git a/src/WithExplicitAccess.php b/src/WithExplicitAccess.php new file mode 100644 index 0000000..b6f5a28 --- /dev/null +++ b/src/WithExplicitAccess.php @@ -0,0 +1,23 @@ +getAttributes(FrontendAccess::class)) > 0; + } + + public function propertyIsPublicAndNotDefinedOnBaseClass($propertyName) + { + return parent::propertyIsPublicAndNotDefinedOnBaseClass($propertyName) + && count((new ReflectionProperty($this, $propertyName))->getAttributes(FrontendAccess::class)) > 0; + } +} diff --git a/tests/LivewireAccessTest.php b/tests/LivewireAccessTest.php new file mode 100644 index 0000000..caf917e --- /dev/null +++ b/tests/LivewireAccessTest.php @@ -0,0 +1,55 @@ +expectException(PublicPropertyNotFoundException::class); + + Livewire::test(TestComponent::class) + ->call('$set', 'foo', 'xxx'); + } + + /** @test */ + public function public_properties_can_be_explicitly_accessible() + { + Livewire::test(TestComponent::class) + ->call('$set', 'bar', 'xxx'); + + // No exception + } + + /** @test */ + public function public_methods_are_not_acccessible_by_default() + { + $this->expectException(NonPublicComponentMethodCall::class); + + Livewire::test(TestComponent::class) + ->call('abc'); + } + + /** @test */ + public function public_methods_can_be_explicitly_accessible() + { + Livewire::test(TestComponent::class) + ->call('def'); + + // No exception + } +} diff --git a/tests/TestCase.php b/tests/TestCase.php deleted file mode 100644 index a5df534..0000000 --- a/tests/TestCase.php +++ /dev/null @@ -1,16 +0,0 @@ -