Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
a3020 committed Nov 2, 2017
0 parents commit 55e215a
Show file tree
Hide file tree
Showing 7 changed files with 197 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
@@ -0,0 +1,3 @@
vendor
.idea
composer.lock
2 changes: 2 additions & 0 deletions CHANGELOG
@@ -0,0 +1,2 @@
0.9.0
- Create logs command
34 changes: 34 additions & 0 deletions README.md
@@ -0,0 +1,34 @@
# concrete5 fake data

[![Latest Version on Packagist][ico-version]][link-packagist]
[![Software License][ico-license]](LICENSE.txt)
[![Total Downloads][ico-downloads]][link-downloads]

## Currently supported commands
You can create fake data via the CLI. Start the CLI environment via `concrete/bin/concrete5`. Supported commands are:
- `fake-data:create:logs --amount 100`

## Installation Composer based environment

To install this package on a composer based concrete5 site, make sure you already have composer/installers then run:

```sh
$ composer require a3020/fake_data
```

Then install the package:

```sh
$ ./vendor/bin/concrete5 c5:package-install fake_data
```

## Installation normal environment
In a normal concrete5 environment, download the ZIP file, extract its
contents to `packages` and run `composer install` in the `packages/fake_data` directory.

[ico-version]: https://img.shields.io/packagist/v/a3020/fake_data.svg?style=flat-square
[ico-license]: https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square
[ico-downloads]: https://img.shields.io/packagist/dt/a3020/fake_data.svg?style=flat-square

[link-packagist]: https://packagist.org/packages/a3020/fake_data
[link-downloads]: https://packagist.org/packages/a3020/fake_data
10 changes: 10 additions & 0 deletions composer.json
@@ -0,0 +1,10 @@
{
"name": "a3020/fake_data",
"description": "Use fake data in your concrete5 installation",
"type": "concrete5-package",
"license": "MIT",
"minimum-stability": "stable",
"require": {
"fzaninotto/faker": "^1.7"
}
}
43 changes: 43 additions & 0 deletions controller.php
@@ -0,0 +1,43 @@
<?php

namespace Concrete\Package\FakeData;

use A3020\FakeData\Provider\FakeDataServiceProvider;
use Concrete\Core\Package\Package;

final class Controller extends Package
{
protected $pkgHandle = 'fake_data';
protected $appVersionRequired = '8.2.1';
protected $pkgVersion = '0.9.0';
protected $pkgAutoloaderRegistries = [
'src/FakeData' => '\A3020\FakeData',
];

public function getPackageName()
{
return t('Fake Data');
}

public function getPackageDescription()
{
return t('Create fake data in your C5 installation');
}

/**
* Load Composer files.
*
* If the C5 installation is Composer based, the vendor directory will
* be in the root directory. Therefore we first check if the autoloader exists.
*/
public function on_start()
{
$autoloadFile = $this->getPackagePath() . '/vendor/autoload.php';
if (file_exists($autoloadFile)) {
require_once $autoloadFile;
}

$provider = $this->app->make(FakeDataServiceProvider::class);
$provider->register();
}
}
75 changes: 75 additions & 0 deletions src/FakeData/Console/Command/CreateLogsCommand.php
@@ -0,0 +1,75 @@
<?php

namespace A3020\FakeData\Console\Command;

use Concrete\Core\Database\Connection\Connection;
use Concrete\Core\Logging\Logger;
use Concrete\Core\Support\Facade\Application;
use Faker\Factory;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;

class CreateLogsCommand extends Command
{
protected $appInstance;
protected $connection;

const NUMBER_OF_LOGS = 50;

protected function configure()
{
$this
->setName('fake-data:create:logs')
->setDescription('Create fake log entries')
->addOption('amount', null, InputOption::VALUE_OPTIONAL, 'Number of logs to be created');
}

protected function execute(InputInterface $input, OutputInterface $output)
{
$this->appInstance = Application::getFacadeApplication();
$this->connection = $this->appInstance->make(Connection::class);

$numberOfLogs = (int) ($input->getOption('amount') ?? self::NUMBER_OF_LOGS);
$this->createLogs($numberOfLogs);

$output->writeln('<info>Number of logs created: '. $numberOfLogs .'</info>');
}

/**
* @param int $numberOfLogs
*/
private function createLogs($numberOfLogs)
{
$logger = $this->appInstance->make(Logger::class);
$faker = Factory::create();

$allUserIds = $this->getAllUserIds();

$statement = $this->connection->prepare('
INSERT INTO Logs (channel, level, message, time, uID)
VALUES (:channel, :level, :message, :time, :uID)
');

for ($i = 0; $i < $numberOfLogs; $i++) {
$statement->execute(
[
'channel' => $faker->randomElement($logger->getChannels()),
'level' => $faker->randomElement($logger->getLevels()),
'message' => $faker->text,
'time' => $faker->dateTimeThisYear()->getTimestamp(),
'uID' => (int) $faker->randomElement($allUserIds),
]
);
}
}

/**
* @return array
*/
private function getAllUserIds()
{
return (array) $this->connection->fetchColumn('SELECT uID FROM Users');
}
}
30 changes: 30 additions & 0 deletions src/FakeData/Provider/FakeDataServiceProvider.php
@@ -0,0 +1,30 @@
<?php

namespace A3020\FakeData\Provider;

use A3020\FakeData\Console\Command\CreateLogsCommand;
use Concrete\Core\Application\Application;

class FakeDataServiceProvider
{
/** @var Application */
private $app;

public function __construct(Application $app)
{
$this->app = $app;
}

public function register()
{
if ($this->app->isRunThroughCommandLineInterface()) {
$this->registerConsoleCommands();
}
}

private function registerConsoleCommands()
{
$console = $this->app->make('console');
$console->add(new CreateLogsCommand());
}
}

0 comments on commit 55e215a

Please sign in to comment.