Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Initital commit. #1

Merged
merged 4 commits into from
Nov 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 47 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<p align="center">
<a href="https://github.com/yii2-extensions/template" target="_blank">
<a href="https://github.com/yii2-extensions/asset-bootstrap5" target="_blank">
<img src="https://www.yiiframework.com/image/yii_logo_light.svg" height="100px;">
</a>
<h1 align="center">Yii2-Template.</h1>
<h1 align="center">Asset for Twitter Bootstrap5.</h1>
<br>
</p>

Expand All @@ -13,32 +13,62 @@
<a href="https://github.com/yiisoft/yii2/tree/2.2" target="_blank">
<img src="https://img.shields.io/badge/Yii2%20version-2.2-blue" alt="yii2-version">
</a>
<a href="https://github.com/yii2-extensions/template/actions/workflows/build.yml" target="_blank">
<img src="https://github.com/yii2-extensions/template/actions/workflows/build.yml/badge.svg" alt="PHPUnit">
<a href="https://github.com/yii2-extensions/asset-bootstrap5/actions/workflows/build.yml" target="_blank">
<img src="https://github.com/yii2-extensions/asset-bootstrap5/actions/workflows/build.yml/badge.svg" alt="PHPUnit">
</a>
<a href="https://codecov.io/gh/yii2-extensions/template" target="_blank">
<img src="https://codecov.io/gh/yii2-extensions/template/branch/main/graph/badge.svg?token=MF0XUGVLYC" alt="Codecov">
<a href="https://codecov.io/gh/yii2-extensions/asset-bootstrap5" target="_blank">
<img src="https://codecov.io/gh/yii2-extensions/asset-bootstrap5/branch/main/graph/badge.svg?token=MF0XUGVLYC" alt="Codecov">
</a>
<a href="https://github.com/yii2-extensions/template/actions/workflows/static.yml" target="_blank">
<a href="https://github.com/yii2-extensions/asset-bootstrap5/actions/workflows/static.yml" target="_blank">
<img src="https://github.com/yii2-extensions/gii/actions/workflows/static.yml/badge.svg" alt="PHPStan">
</a>
<a href="https://github.com/yii2-extensions/template/actions/workflows/static.yml" target="_blank">
<img src="https://img.shields.io/badge/PHPStan%20level-2-blue" alt="PHPStan level">
</a>
<a href="https://github.styleci.io/repos/698621511?branch=main" target="_blank">
<img src="https://github.styleci.io/repos/698621511/shield?branch=main" alt="Code style">
</a>
<a href="https://github.com/yii2-extensions/asset-bootstrap5/actions/workflows/static.yml" target="_blank">
<img src="https://img.shields.io/badge/PHPStan%20level-5-blue" alt="PHPStan level">
</a>
<a href="https://github.styleci.io/repos/719651888?branch=main" target="_blank">
<img src="https://github.styleci.io/repos/719651888/shield?branch=main" alt="Code style">
</a>
</p>

## Requirements
## Installation

The preferred way to install this extension is through [composer](https://getcomposer.org/download/).

Either run

The minimun version of `PHP` required by this package is `PHP 8.1`.
```
composer require --dev --prefer-dist yii2-extensions/asset-bootstrap5
```

For install this package, you need [composer](https://getcomposer.org/).
or add

```
"yii2-extensions/asset-bootstrap5": "dev-main"
```

to the require-dev section of your `composer.json` file.

## Usage

[Check the documentation docs](/docs/README.md) to learn about usage.
```php
<?php

declare(strict_types=1);

use Yii2\Asset\BootstrapAsset;

BootstrapAsset::register($this);
```

```php
<?php

declare(strict_types=1);

use Yii2\Asset\BootstrapPluginAsset;

BootstrapPluginAsset::register($this);
```

## Testing

Expand Down
5 changes: 5 additions & 0 deletions composer-require-checker.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"symbol-whitelist": [
"YII_ENV"
]
}
29 changes: 23 additions & 6 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,41 +1,58 @@
{
"name": "yii2/template",
"name": "yii2-extensions/asset-bootstrap5",
"type": "library",
"description": "_____",
"description": "Asset for Bootstrap5 for Yii Framework",
"keywords": [
"_____"
"yii2",
"asset",
"bootstrap5"
],
"license": "mit",
"minimum-stability": "dev",
"prefer-stable": true,
"require": {
"php": ">=8.1",
"npm-asset/bootstrap": "^5.3",
"oomphinc/composer-installers-extender": "^2.0",
"yiisoft/yii2": "^2.2"
},
"require-dev": {
"maglnet/composer-require-checker": "^4.6",
"php-forge/support": "dev-main",
"phpunit/phpunit": "^10.2",
"yii2-extensions/phpstan": "dev-main"
},
"autoload": {
"psr-4": {
"yii\\template\\": "src"
"Yii2\\Asset\\": "src"
}
},
"autoload-dev": {
"psr-4": {
"yii\\template\\tests\\": "tests"
"Yii2\\Asset\\Tests\\": "tests"
}
},
"extra": {
"branch-alias": {
"dev-main": "1.0.x-dev"
},
"installer-types": [
"bower-asset",
"npm-asset"
],
"installer-paths": {
"./node_modules/{$name}": [
"type:bower-asset",
"type:npm-asset"
]
}
},
"config": {
"sort-packages": true,
"allow-plugins": {
"yiisoft/yii2-composer": true
"yiisoft/yii2-composer": true,
"composer/installers": true,
"oomphinc/composer-installers-extender": true
}
},
"scripts": {
Expand Down
9 changes: 4 additions & 5 deletions phpstan.neon
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
includes:
- vendor/yii2-extensions/phpstan/extension.neon
parameters:
bootstrapFiles:
- tests/bootstrap.php

dynamicConstantNames:
- YII_DEBUG
- YII_ENV
- YII_ENV_DEV
- YII_ENV_PROD
- YII_ENV_TEST

level: 2
level: 5

paths:
- src
Expand Down
4 changes: 2 additions & 2 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<phpunit
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.2/phpunit.xsd"
bootstrap="vendor/autoload.php"
bootstrap="tests/bootstrap.php"
cacheDirectory=".phpunit.cache"
colors="true"
executionOrder="depends,defects"
Expand All @@ -11,7 +11,7 @@
stopOnFailure="false"
>
<testsuites>
<testsuite name="Template">
<testsuite name="Yii2-Asset-Bootstrap5">
<directory>tests</directory>
</testsuite>
</testsuites>
Expand Down
28 changes: 28 additions & 0 deletions src/BootstrapAsset.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

declare(strict_types=1);

namespace Yii2\Asset;

use yii\web\AssetBundle;

/**
* Twitter Bootstrap 5 CSS bundle.
*/
class BootstrapAsset extends AssetBundle
{
/**
* @inheritDoc
*/
public $sourcePath = '@npm/bootstrap/dist/css';

public function init(): void
{
parent::init();

$assetBootstrap = YII_ENV === 'prod' ? 'bootstrap.min.css' : 'bootstrap.css';

$this->js[] = $assetBootstrap;
$this->publishOptions['only'] = [$assetBootstrap];
}
}
35 changes: 35 additions & 0 deletions src/BootstrapPluginAsset.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

declare(strict_types=1);

namespace Yii2\Asset;

use yii\web\AssetBundle;

/**
* Twitter Bootstrap 5 JavaScript bundle.
*/
class BootstrapPluginAsset extends AssetBundle
{
/**
* @inheritDoc
*/
public $sourcePath = '@npm/bootstrap/dist/js';

/**
* @inheritDoc
*/
public $depends = [
BootstrapAsset::class,
];

public function init(): void
{
parent::init();

$assetBootstrapPlugin = YII_ENV === 'prod' ? 'bootstrap.bundle.min.js' : 'bootstrap.bundle.js';

$this->js[] = $assetBootstrapPlugin;
$this->publishOptions['only'] = [$assetBootstrapPlugin];
}
}
13 changes: 0 additions & 13 deletions src/Example.php

This file was deleted.

104 changes: 104 additions & 0 deletions tests/AssetTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
<?php

declare(strict_types=1);

namespace Yii2\Asset\Tests;

use Yii;
use Yii2\Asset\BootstrapAsset;
use Yii2\Asset\BootstrapPluginAsset;
use yii\web\AssetBundle;
use yii\web\View;

final class AssetTest extends TestCase
{
public function testBootstrapAssetSimpleDependency(): void
{
$view = Yii::$app->getView();

$this->assertEmpty($view->assetBundles);

BootstrapAsset::register($view);

$this->assertCount(1, $view->assetBundles);
$this->assertArrayHasKey(BootstrapAsset::class, $view->assetBundles);
}

public function testBootstrapAssetSourcesPublish(): void
{
$view = new View();
$bundle = BootstrapAsset::register($view);

$this->assertDirectoryExists($bundle->basePath);
$this->sourcesPublishVerifyFiles('js', $bundle);
}

public function testBootstrapAssetRegister(): void
{
$view = new View();

$this->assertEmpty($view->assetBundles);

BootstrapAsset::register($view);

$this->assertCount(1, $view->assetBundles);
$this->assertInstanceOf(AssetBundle::class, $view->assetBundles[BootstrapAsset::class]);

$result = $view->renderFile(__DIR__ . '/support/main.php');

$this->assertMatchesRegularExpression('/bootstrap.css/', $result);
}

public function testBootstrapPluginAssetSimpleDependency(): void
{
$view = Yii::$app->getView();

$this->assertEmpty($view->assetBundles);

BootstrapPluginAsset::register($view);

$this->assertCount(2, $view->assetBundles);
$this->assertArrayHasKey(BootstrapAsset::class, $view->assetBundles);
$this->assertArrayHasKey(BootstrapPluginAsset::class, $view->assetBundles);
$this->assertInstanceOf(AssetBundle::class, $view->assetBundles[BootstrapAsset::class]);
}

public function testBootstrapPluginAssetSourcesPublish(): void
{
$view = new View();
$bundle = BootstrapPluginAsset::register($view);

$this->assertDirectoryExists($bundle->basePath);
$this->sourcesPublishVerifyFiles('js', $bundle);
}

public function testBootstrapPluginAssetRegister(): void
{
$view = new View();

$this->assertEmpty($view->assetBundles);

BootstrapPluginAsset::register($view);

$this->assertCount(2, $view->assetBundles);
$this->assertInstanceOf(AssetBundle::class, $view->assetBundles[BootstrapPluginAsset::class]);
$this->assertInstanceOf(AssetBundle::class, $view->assetBundles[BootstrapAsset::class]);

$result = $view->renderFile(__DIR__ . '/support/main.php');

$this->assertMatchesRegularExpression('/bootstrap.css/', $result);
$this->assertMatchesRegularExpression('/bootstrap.bundle.js/', $result);
}

private function sourcesPublishVerifyFiles(string $type, object $bundle): void
{
foreach ($bundle->$type as $filename) {
$publishedFile = $bundle->basePath . DIRECTORY_SEPARATOR . $filename;
$sourceFile = $bundle->sourcePath . DIRECTORY_SEPARATOR . $filename;
$this->assertFileExists($publishedFile);
$this->assertFileEquals($publishedFile, $sourceFile);
}

$this->assertDirectoryExists($bundle->basePath);
}
}
18 changes: 0 additions & 18 deletions tests/ExampleTest.php

This file was deleted.

Loading