Skip to content

Commit

Permalink
refactor: autoinstall for assetmapper & stimulus
Browse files Browse the repository at this point in the history
  • Loading branch information
siganushka committed Oct 19, 2024
1 parent 46b8ef6 commit 3ddcc79
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 15 deletions.
15 changes: 11 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,14 @@
[![Latest Unstable Version](https://poser.pugx.org/siganushka/region-bundle/v/unstable)](https://packagist.org/packages/siganushka/region-bundle)
[![License](https://poser.pugx.org/siganushka/region-bundle/license)](https://packagist.org/packages/siganushka/region-bundle)

国内行政区划(省、市、区、乡/街道联动) [Bundle](https://symfony.com/doc/current/bundles.html),数据来源 [Administrative-divisions-of-China](https://github.com/modood/Administrative-divisions-of-China)
国内行政区划四级联动 [Bundle](https://symfony.com/doc/current/bundles.html),数据来源 [Administrative-divisions-of-China](https://github.com/modood/Administrative-divisions-of-China)

### 说明

- 一级:省、自治区、直辖市
- 二级:市、县、自治州、自治县
- 三级:区、县
- 四级:乡镇/街道

### 安装

Expand All @@ -21,13 +28,13 @@ $ composer require siganushka/region-bundle
$ php bin/console doctrine:schema:update --force
```

更新行政区划数据源:
更新行政区划数据源(默认为三级)

```bash
$ php bin/console siganushka:region:update
```

> 默认仅导入省、市、区三级,可使用 `--with-street` 参数导入乡/街道四级
> 使用 `php bin/console siganushka:region:update --with-street` 参数导入四级
导入路由:

Expand Down Expand Up @@ -59,7 +66,7 @@ $ php bin/console assets:install

### 示例

例如用户地址实体,包含省、市、区、/街道四级联动:
例如用户地址实体,包含省、市、区、乡镇/街道四级联动:

```php
// src/Entity/UserAddress.php
Expand Down
Original file line number Diff line number Diff line change
@@ -1,25 +1,21 @@
import { Controller } from '@hotwired/stimulus';

/*
* The following line makes this controller "lazy": it won't be downloaded until needed
* See https://github.com/symfony/stimulus-bridge#lazy-controllers
*/
/* stimulusFetch: 'lazy' */
export default class extends Controller {
static values = {
url: String,
cascader: String,
url: String,
}

connect() {
this.element.addEventListener('change', this.handleChange.bind(this))
this.element.addEventListener('change', this.change.bind(this))
}

disconnect() {
this.element.removeEventListener('change', this.handleChange.bind(this))
this.element.removeEventListener('change', this.change.bind(this))
}

handleChange(event) {
change(event) {
const cascaderEl = document.getElementById(this.cascaderValue)
if (!cascaderEl) return

Expand Down
17 changes: 17 additions & 0 deletions assets/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"name": "@siganushka/region-bundle",
"description": "Region bundle integration for Symfony",
"license": "MIT",
"version": "0.1.0",
"symfony": {
"controllers": {
"region": {
"main": "dist/controller.js",
"name": "region",
"webpackMode": "eager",
"fetch": "eager",
"enabled": true
}
}
}
}
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "siganushka/region-bundle",
"description": "Region bundle for symfony.",
"keywords": ["region-bundle"],
"keywords": ["region-bundle", "symfony-ux"],
"homepage": "https://github.com/siganushka/region-bundle",
"type": "symfony-bundle",
"license": "MIT",
Expand Down
3 changes: 1 addition & 2 deletions public/main.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
document.addEventListener('DOMContentLoaded', () => {
const change = async (event) => {
console.log('change...', event.target.dataset)
const change = (event) => {
const { regionCascaderValue, regionUrlValue } = event.target.dataset
const cascaderEl = document.getElementById(regionCascaderValue)
if (!cascaderEl) return
Expand Down
26 changes: 26 additions & 0 deletions src/DependencyInjection/SiganushkaRegionExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Siganushka\RegionBundle\DependencyInjection;

use Symfony\Component\AssetMapper\AssetMapperInterface;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Extension\Extension;
Expand Down Expand Up @@ -43,5 +44,30 @@ public function prepend(ContainerBuilder $container): void
$container->prependExtensionConfig('siganushka_generic', [
'doctrine' => ['mapping_override' => $mappingOverride],
]);

if ($this->isAssetMapperAvailable($container)) {
$container->prependExtensionConfig('framework', [
'asset_mapper' => [
'paths' => [
__DIR__.'/../../assets/dist' => '@siganushka/region-bundle',
],
],
]);
}
}

private function isAssetMapperAvailable(ContainerBuilder $container): bool
{
if (!interface_exists(AssetMapperInterface::class)) {
return false;
}

/** @var array */
$bundlesMetadata = $container->getParameter('kernel.bundles_metadata');
if (!isset($bundlesMetadata['FrameworkBundle'])) {
return false;
}

return is_file($bundlesMetadata['FrameworkBundle']['path'].'/Resources/config/asset_mapper.php');
}
}

0 comments on commit 3ddcc79

Please sign in to comment.