承接 unclecheese/mock-dataobjects 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

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

unclecheese/mock-dataobjects

Composer 安装命令:

composer require unclecheese/mock-dataobjects

包简介

Allows DataObjects to self-populate intelligently using fake data

README 文档

README

This module provides intelligent content generation functionality to all DataObjects. The object introspects its fields and assigns an example value based on the field type and the name of the field. It also provides a command line utility for generating mock data programatically as well as various UI features in the CMS to support the creating and populating DataObjects.

Installation

Installation via Composer is highly recommended, as this module has external dependencies.

composer require unclecheese/mock-dataobjects:dev-master

Example

class StaffMember extends DataObject {

	private static $db = array (
		'FirstName' => 'Varchar(255)',
		'LastName' => 'Varchar(255)',
		'EmailAddress' => 'Varchar(255)',
		'Address' => 'Varchar(255)',
		'City' => 'Varchar(255)',
		'PostalCode' => 'Varchar(255)',
		'Country' => 'Varchar(255)',
		'Company' => 'Varchar(255)',
		'Website' => 'Varchar(255)',
		'PhoneNumber' => 'Varchar(255)',
	);


	private static $has_one = array (
		'Photo' => 'Image',
		'StaffHolder' => 'StaffHolder',
	);
}
$staff = new StaffMember();
$staff->fill();

Result:

Screenshot

Implementation

You can use the features of the MockDataObjects module in many ways, including executable code, a command-line interface, and from within the CMS.

From the CMS

Adding mock children to a parent page:

Right click on the parent page and choose "Add mock children."

Screenshot

Choose options, and create

Screenshot

Adding items to a grid

Just click on "add mock data" and set your options.

Screenshot

Populating existing records

Click on "fill with mock data"

Screenshot

In the execution pipeline

$myDataObject->fill();

As demonstrated above, the ->fill() method populates a DataObject with mock data. There are a few options you can pass to this method.

$myDataObject->fill(array(
	 'only_empty' => true, // only fill in empty fields
	 'include_relations' => true, // Include has_many and many_many relations
	 'relation_create_limit' => 5, // If no existing records for many_many or has_one relations, limit creation
	 'download_images' => false, // Don't download images from the web. Use existing.
));

From the command line

Create 50 new records. Use existing files for file relationships.

mockdata generate Product -count 50 --no-downloads

Populate existing records with new data.

mockdata populate Product

Add new records to the has_many relation on a given page.

mockdata generate StaffMember -parent-field StaffPageID -parent "our-staff"

Localisation

Mock data values are localised to the current locale as defined by i18n::get_locale().

i18n::set_locale('en_US');
$staff = new StaffMember();
$staff->fill();
echo $staff->PhoneNumber; // (102) 806-3915

i18n::set_locale('fr_FR');
$staff = new StaffMember();
$staff->fill();
echo $staff->PhoneNumber; // +33 8 17 54 64 62

Field name hooks

For generic database fields, such as Varchar, the mock data generator is informed by the field name in order to create more realistic data. These hooks are defined in the language file.

en:
  MockDataObject:
    FIRSTNAME: "Firstname, FirstName"
    LASTNAME: "Surname, LastName, Lastname"
    FULLNAME: "FullName"
    CITY: "City, Town"
    STATE: "State"
    ADDRESS: "Address, Address1, Address2"
    POSTCODE: "Zip, Zipcode, ZipCode"
    COUNTRYCODE: "Country, CountryCode"
    PHONENUMBER: "Phone, PhoneNumber, Fax, Cell, Mobile, Telephone, Phonenumber"
    EMAIL: "Email, EmailAddress"
    COMPANY: "Company, CompanyName, Organization"
    URL: "URL, Website, URI"
    LATITUDE: "Latitude, Lat"
    LONGITUDE: "Longitude, Lon"

A comma-separated list of possible field names are mapped to an entity, so that a field named "EmailAddress" or "Email" creates a fake email address, and "Phone" or "Telephone" creates a fake phone number.

An example language file for French might look like this:

fr:
  MockDataObject:
    FIRSTNAME: "Prenom"
    LASTNAME: "NomDeFamille, Nom"
    CITY: "Ville"

Model-independent data generation

Sometimes it is useful to generate mock data before the model has been created, such as when frontend development is happening before backend development. For that purpose, every DataObject comes with a $Fake method to access the fake data generator.

<h2>$Fake.Words</h2>
$Fake.Paragraphs(2,5) <!-- min, max -->
$Fake.Image.SetWidth(100)

<h3>What we can do for you</h3>
<ul>
  <% loop $Fake.Loop %>
    <li>$Fake.Number $Fake.Words</li>
  <% end_loop %>
</ul>

<h3>Contact Us</h3>
$Fake.Company<br />
$Fake.FullName<br />
$Fake.Address<br />
$Fake.Address<br />
$Fake.City, $Fake.State $Fake.PostalCode<br />
$Fake.PhoneNumber<br />
<a href="mailto:$Fake.Email">$Fake.Email</a>

<h3>Find us on a map!</h3>

$Fake.Latitude, $Fake.Longitude

Cleaning up

Records of all mockdata creation are stored in the MockDataLog table, which maps a record ID to a class name. You can clean this table using the task dev/tasks/MockDataTask cleanup <classname>. To clear all mock data, leave the class name argument null.

Be very careful about converting mock data records into authentic records, as this task will clean them up without knowing that you have populated them with valid data!

Troubleshooting

Just ring Uncle Cheese.

unclecheese/mock-dataobjects 适用场景与选型建议

unclecheese/mock-dataobjects 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 3.73k 次下载、GitHub Stars 达 18, 最近一次更新时间为 2013 年 09 月 29 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

围绕 unclecheese/mock-dataobjects 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 3.73k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 18
  • 点击次数: 16
  • 依赖项目数: 1
  • 推荐数: 0

GitHub 信息

  • Stars: 18
  • Watchers: 6
  • Forks: 8
  • 开发语言: PHP

其他信息

  • 授权协议: BSD-3-Clause
  • 更新时间: 2013-09-29