tobento/app-country 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

tobento/app-country

Composer 安装命令:

composer require tobento/app-country

包简介

App country support.

README 文档

README

Country support for the app.

Table of Contents

Getting Started

Add the latest version of the app country project running this command.

composer require tobento/app-country

Requirements

  • PHP 8.4 or greater

Documentation

App

Check out the App Skeleton if you are using the skeleton.

You may also check out the App to learn more about the app in general.

Country Boot

The country boot does the following:

  • installs country files
  • implements country interfaces
use Tobento\App\AppFactory;

// Create the app
$app = new AppFactory()->createApp();

// Add directories:
$app->dirs()
    ->dir(realpath(__DIR__.'/../'), 'root')
    ->dir(realpath(__DIR__.'/../app/'), 'app')
    ->dir($app->dir('app').'config', 'config', group: 'config')
    ->dir($app->dir('root').'public', 'public')
    ->dir($app->dir('root').'vendor', 'vendor');
    
// Adding boots
$app->boot(\Tobento\App\Country\Boot\Country::class);

// Run the app
$app->run();

Available Country Interfaces

The following interfaces are available after booting:

use Tobento\App\AppFactory;
use Tobento\Service\Country\CountryFactoryInterface;
use Tobento\Service\Country\CountriesFactoryInterface;
use Tobento\Service\Country\CountryRepositoryInterface;

// Create the app
$app = new AppFactory()->createApp();

// Add directories:
$app->dirs()
    ->dir(realpath(__DIR__.'/../'), 'root')
    ->dir(realpath(__DIR__.'/../app/'), 'app')
    ->dir($app->dir('app').'config', 'config', group: 'config')
    ->dir($app->dir('root').'public', 'public')
    ->dir($app->dir('root').'vendor', 'vendor');
    
// Adding boots
$app->boot(\Tobento\App\Country\Boot\Country::class);
$app->booting();

$countryFactory = $app->get(CountryFactoryInterface::class);
$countriesFactory = $app->get(CountriesFactoryInterface::class);
$countryRepository = $app->get(CountryRepositoryInterface::class);

// Run the app
$app->run();

Check out the Country Service to learn more about the interfaces.

Retrieve Countries

You can retrieve countries by using the CountryRepositoryInterface::class:

use Tobento\App\AppFactory;
use Tobento\Service\Country\CountryRepositoryInterface;

// Create the app
$app = new AppFactory()->createApp();

// Add directories:
$app->dirs()
    ->dir(realpath(__DIR__.'/../'), 'root')
    ->dir(realpath(__DIR__.'/../app/'), 'app')
    ->dir($app->dir('app').'config', 'config', group: 'config')
    ->dir($app->dir('root').'public', 'public')
    ->dir($app->dir('root').'vendor', 'vendor');
    
// Adding boots
$app->boot(\Tobento\App\Country\Boot\Country::class);
$app->booting();

$countryRepository = $app->get(CountryRepositoryInterface::class);

// Run the app
$app->run();

Check out the Country Repository Interface to learn more about it.

Add Or Customize Countries

Currently, countries are available in the locales en, de, fr and it.

You might add or customize/override new countries by the following way:

use Tobento\App\AppFactory;
use Tobento\Service\Country\CountryRepositoryInterface;

// Create the app
$app = new AppFactory()->createApp();

// Add directories:
$app->dirs()
    ->dir(realpath(__DIR__.'/../'), 'root')
    ->dir(realpath(__DIR__.'/../app/'), 'app')
    ->dir($app->dir('app').'config', 'config', group: 'config')
    ->dir($app->dir('root').'public', 'public')
    ->dir($app->dir('root').'vendor', 'vendor');
    
// Add country directory with higher priority:
$this->app->dirs()->dir(
    dir: $this->app->dir('app').'countries-custom/',
    
    // do not use 'countries' as name for migration purposes
    name: 'countries.custom',
    
    group: 'countries',
    
    // add higher priority as default countries dir:
    priority: 300,
);

// Adding boots
$app->boot(\Tobento\App\Country\Boot\Country::class);

// Run the app
$app->run();

Then just add the countries files you wish to override in the defined directory. If the file does not exist, the file from the default country directory is used.

Learn More

Using Countries In Forms

The following examples require the App View bundle with the following boots:

// ...

$app->boot(\Tobento\App\View\Boot\View::class);
$app->boot(\Tobento\App\View\Boot\Form::class);

// ...

Example using countries column

use Tobento\Service\Country\CountryRepositoryInterface;
use Tobento\Service\Country\CountryInterface;
use Tobento\Service\View\ViewInterface;

$countryRepository = $app->get(CountryRepositoryInterface::class);
$countries = $countryRepository->findCountries(locale: 'de');

// You may filter countries:
$countries = $countries->only(['CH', 'US']);

$view = $app->get(ViewInterface::class);

$content = $view->render(view: 'countries/select', data: [
    'countries' => $countries->column(column: 'name', index: 'code'),
]);

The views/countries/select.php view:

<?= $view->form()->select(
    name: 'countries[]',
    items: $countries,
    emptyOption: ['none', '---'],
) ?>
/*
<select name="countries[]" id="countries">
    <option value="none">---</option>
    <option value="CH">Schweiz</option>
    <option value="US">Vereinigte Staaten</option>
</select>
*/

You may check out the Select Form Element to learn more about it.

Example grouping countries by continents

use Tobento\Service\Country\CountryRepositoryInterface;
use Tobento\Service\Country\CountryInterface;
use Tobento\Service\View\ViewInterface;

$countryRepository = $app->get(CountryRepositoryInterface::class);
$countries = $countryRepository->findCountries(locale: 'de');

// Grouped column:
$groupedCountries = $countries->groupedColumn(group: 'continent', column: 'name', index: 'code');
ksort($groupedCountries);

$view = $app->get(ViewInterface::class);

$content = $view->render(view: 'countries/select', data: [
    'countries' => $groupedCountries,
]);

The views/countries/select.php view:

<?= $view->form()->select(
    name: 'countries[]',
    items: $countries,
    selectAttributes: [],
    optionAttributes: [],
    optgroupAttributes: [],
) ?>
/*
<select name="countries[]" id="countries">
    <optgroup label="Afrika">
        <option value="DZ">Algerien</option>
        ...
    </optgroup>
    <optgroup label="Antarktika">
        <option value="AQ">Antarktis</option>
        ...
    </optgroup>
    ...
</select>
*/

Example grouping countries by custom group

use Tobento\Service\Country\CountryRepositoryInterface;
use Tobento\Service\Country\CountryInterface;
use Tobento\Service\View\ViewInterface;

$countryRepository = $app->get(CountryRepositoryInterface::class);
$countries = $countryRepository->findCountries(locale: 'de');

// Handle grouping:
$countries = $countries->map(function(CountryInterface $c) {
    if (in_array($c->code(), ['CH', 'FR'])) {
        return $c->withGroup('Near By')->withPriority(100);
    }
    return $c->withGroup('All Others');
});

// You may sort it by priority:
$countries = $countries->sort(
    fn(CountryInterface $a, CountryInterface $b): int => $b->priority() <=> $a->priority()
);

// Grouped column:
$groupedCountries = $countries->groupedColumn(group: 'group', column: 'name', index: 'code');

$view = $app->get(ViewInterface::class);

$content = $view->render(view: 'countries/select', data: [
    'countries' => $groupedCountries,
]);

The views/countries/select.php view:

<?= $view->form()->select(
    name: 'countries[]',
    items: $countries,
    selectAttributes: [],
    optionAttributes: [],
    optgroupAttributes: [],
) ?>
/*
<select name="countries[]" id="countries">
    <optgroup label="Near By">
        <option value="FR">Frankreich</option>
        <option value="CH">Schweiz</option>
    </optgroup>
    <optgroup label="All Others">
        <option value="AF">Afghanistan</option>
        <option value="AX">Ålandinseln</option>
        <option value="AL">Albanien</option>
        ...
    </optgroup>
</select>
*/

Credits

tobento/app-country 适用场景与选型建议

tobento/app-country 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 95 次下载、GitHub Stars 达 0, 最近一次更新时间为 2023 年 07 月 13 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「package」 「app」 「country」 「tobento」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 tobento/app-country 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 tobento/app-country 我们能提供哪些服务?
定制开发 / 二次开发

基于 tobento/app-country 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 95
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 15
  • 依赖项目数: 1
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2023-07-13