Skip to content

Commit

Permalink
fix conflicts
Browse files Browse the repository at this point in the history
  • Loading branch information
williamespindola committed Sep 9, 2015
2 parents ebd6827 + addc4d9 commit 4f65171
Show file tree
Hide file tree
Showing 25 changed files with 589 additions and 252 deletions.
24 changes: 22 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,28 @@
[![Quality Score][ico-code-quality]][link-code-quality]
[![Total Downloads][ico-downloads]][link-downloads]

A simple back-end life circle for fields inspired on [cockpit](http://getcockpit.com/).
Via command line you can create fields for your partials and consume in your project using he Repository, Services or Extensions
Field is a blueprint to work as a CMS inpired on [cockpit](http://getcockpit.com/).
Via command line you can create fields for your partials and consume in your project using Repository, Services or Extensions.

### Example

Create some field, running this in your terminal
```bash
php vendor/bin/field create Collection about "About page"
php vendor/bin/field create Field textfield "About text" text about
php vendor/bin/field create Field imagefield "About image" text about
```

With this code you will create a collection called "About page" and two field for him "About text" and "About image". So you can get this using the Repository:
```php
use WilliamEspindola\Field\Storage\ORM\Doctrine;
use WilliamEspindola\Field\Repository\CollectionFieldRepository;

$doctrineStorage = new Doctrine(/** doctrine setup */); // you can uss orther ORM as you want
$repository = new CollectionFieldRepository($doctrineStorage);

$repository->findAll()
```

## Install

Expand Down
13 changes: 6 additions & 7 deletions behat.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
default:
paths:
features: %behat.paths.base%/tests/functional
extensions:
Behat\MinkExtension\Extension:
goutte: ~
selenium2: ~
base_url: http://localhost:8080
autoload:
'': %paths.base%/tests/functional
suites:
cli:
paths: [ %paths.base%/tests/functional/cli ]
contexts: [ FeatureContext ]
Empty file modified bin/field
100644 → 100755
Empty file.
9 changes: 7 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"license": "BDS-3-Clause",
"keywords": [
"field",
"cms",
"console"
],
"authors": [
Expand All @@ -18,12 +19,16 @@
],
"require": {
"php" : ">=5.4",
"respect/relational": "^0.8",
"ocramius/generated-hydrator": "1.1.0",
"symfony/console": "~2.7"
},
"require-dev": {
"phpunit/phpunit" : "4.*"
"phpunit/phpunit" : "4.*",
"behat/behat": "~3.0"
},
"suggest": {
"doctrine/orm": "orm 2.5.0, if you want to work with doctrine",
"respect/relational": "relational ^0.8, if you want to work with relational"
},
"autoload": {
"psr-4": {
Expand Down
15 changes: 15 additions & 0 deletions config/xml/WilliamEspindola.Field.Entity.Collection.dcm.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<!-- config/xml/Collection.dcm.xml -->
<doctrine-mapping
xmlns="http://doctrine-project.org/schemas/orm/doctrine-mapping"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://doctrine-project.org/schemas/orm/doctrine-mapping http://raw.github.com/doctrine/doctrine2/master/doctrine-mapping.xsd">

<entity name="WilliamEspindola\Field\Entity\Collection">
<id name="id" type="integer">
<generator strategy="AUTO" />
</id>

<field name="name" type="string" />
<field name="label" type="string" />
</entity>
</doctrine-mapping>
15 changes: 15 additions & 0 deletions config/xml/WilliamEspindola.Field.Entity.CollectionField.dcm.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<!-- config/xml/CollectionField.dcm.xml -->
<doctrine-mapping
xmlns="http://doctrine-project.org/schemas/orm/doctrine-mapping"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://doctrine-project.org/schemas/orm/doctrine-mapping http://raw.github.com/doctrine/doctrine2/master/doctrine-mapping.xsd">

<entity name="WilliamEspindola\Field\Entity\CollectionField">
<id name="id" type="integer">
<generator strategy="AUTO" />
</id>

<many-to-one field="field" target-entity="WilliamEspindola\Field\Entity\Field" />
<many-to-one field="collection" target-entity="WilliamEspindola\Field\Entity\Collection" />
</entity>
</doctrine-mapping>
17 changes: 17 additions & 0 deletions config/xml/WilliamEspindola.Field.Entity.Field.dcm.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<!-- config/xml/Field.dcm.xml -->
<doctrine-mapping
xmlns="http://doctrine-project.org/schemas/orm/doctrine-mapping"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://doctrine-project.org/schemas/orm/doctrine-mapping http://raw.github.com/doctrine/doctrine2/master/doctrine-mapping.xsd">

<entity name="WilliamEspindola\Field\Entity\Field">
<id name="id" type="integer">
<generator strategy="AUTO" />
</id>

<field name="name" type="string" />
<field name="type" type="string" />
<field name="value" type="text" />
<field name="label" type="string" />
</entity>
</doctrine-mapping>
16 changes: 16 additions & 0 deletions config/xml/option.dcm.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<!-- config/xml/Option.dcm.xml -->
<doctrine-mapping
xmlns="http://doctrine-project.org/schemas/orm/doctrine-mapping"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://doctrine-project.org/schemas/orm/doctrine-mapping http://raw.github.com/doctrine/doctrine2/master/doctrine-mapping.xsd">

<entity name="option">
<id name="id" type="integer">
<generator strategy="AUTO" />
</id>

<field name="option" type="string" />

<many-to-one target-entity="WilliamEspindola\Field\Entity\Field" field="field" />
</entity>
</doctrine-mapping>
2 changes: 1 addition & 1 deletion data/mysql-schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ CREATE TABLE IF NOT EXISTS `collection` (
PRIMARY KEY (`id`))
ENGINE = InnoDB;

CREATE TABLE IF NOT EXISTS `collection_field` (
CREATE TABLE IF NOT EXISTS `collectionfield` (
`collection_id` INT NOT NULL,
`field_id` INT NOT NULL,
`id` INT NOT NULL AUTO_INCREMENT,
Expand Down
42 changes: 11 additions & 31 deletions src/Console/Command/AbstractCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Respect\Relational\Mapper;
use Respect\Relational\Db;
use WilliamEspindola\Field\Console\Command\Database\DoctrineStorage;
use WilliamEspindola\Field\Console\Command\Database\RelationalStorage;

abstract class AbstractCommand extends Command
{
Expand All @@ -23,7 +23,7 @@ abstract class AbstractCommand extends Command
public function bootstrap(InputInterface $input, OutputInterface $output)
{
if (!$this->getConfig())
$this->loadConfig($input, $output);
$this->locateConfigFile($input);
}

protected function configure()
Expand Down Expand Up @@ -65,37 +65,17 @@ protected function instanceConfig($configFilePath)
$this->config = $configArray;
}

/**
* @return Mapper
*/
public function getMapper()
{
$dsn = "{$this->config['driver']}:host={$this->config['host']};dbname={$this->config['dbname']}";
$mapper = new Mapper(new \PDO($dsn, $this->config['user'], $this->config['password']));
$mapper->entityNamespace = '\\WilliamEspindola\\Field\\Entity\\';

return $mapper;
}

/**
* @return Db
*/
public function getDb()
{
$dsn = "{$this->config['driver']}:host={$this->config['host']};dbname={$this->config['dbname']}";
$db = new Db(new \PDO($dsn, $this->config['user'], $this->config['password']));

return $db;
}

protected function loadConfig(InputInterface $input, OutputInterface $output)
public function getConfig()
{
$configFilePath = $this->locateConfigFile($input);
$output->writeln('<info>using config file</info> .' . str_replace(getcwd(), '', realpath($configFilePath)));
return $this->config;
}

public function getConfig()
public function getStorage()
{
return $this->config;
if (class_exists('Doctrine\ORM\Tools\Setup')) {
return new DoctrineStorage($this->getConfig());
} else {
return new RelationalStorage($this->getConfig());
}
}
}
110 changes: 75 additions & 35 deletions src/Console/Command/Create.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,9 @@
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Output\OutputInterface;
use WilliamEspindola\Field\Entity\Field;
use WilliamEspindola\Field\Entity\Collection;
use WilliamEspindola\Field\Entity\CollectionField;
use WilliamEspindola\Field\Repository\CollectionFieldRepository;
use WilliamEspindola\Field\Repository\CollectionRepository;
use WilliamEspindola\Field\Repository\FieldRepository;
use WilliamEspindola\Field\Storage\ORM\RespectRelational;

class Create extends AbstractCommand
{
Expand All @@ -37,47 +33,91 @@ protected function configure()
* @param InputInterface $input
* @param OutputInterface $output
* @return int|null|void
*
* TODO Remove the if/else
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$this->bootstrap($input, $output);

$mapper = $this->getMapper();
$relational = new RespectRelational($mapper);
$fieldRepository = new FieldRepository($relational);
$collectionRepository = new CollectionRepository($relational);
$collectionFieldRepository = new CollectionFieldRepository($relational);
$storage = $this->getStorage()->getMapperInstance();

$entity = $input->getArgument('Field|Collection');
if ($input->getArgument('Field|Collection') === 'Collection') {
$this->createCollection($input, $storage);
$output->writeln('<info>Collection ' . $input->getArgument('name') . ' was been created.</info>');
return;
}

if ($entity === 'Field') {
$entity = new Field();
$entity->setType($input->getArgument('type'));
$entity->setName($input->getArgument('name'));
$entity->setLabel($input->getArgument('label'));
$fieldRepository->save($entity);
if ($input->getArgument('collection') != null) {
$collection = $this->findACollection($input, $storage);
if (!$collection) {
$output->writeln('<error>The Collection ' . $input->getArgument('collection') . ' not found.</error>');
return;
}

if ($input->getArgument('collection') != null) {
$field = $this->createField($input, $storage);
$this->createTheFieldCollectionRelationship($field, $collection[0], $storage);
$output->writeln(
'<info>Field '
. $input->getArgument('name')
. ' was been created with to collection '
. $input->getArgument('collection')
. '</info>');
return;
}

$collection = $collectionRepository->findOne(['name' => $input->getArgument('collection')]);
$this->createField($input, $storage);
$output->writeln('<info>Field ' . $input->getArgument('name') . ' was been created.</info>');
}

if (!$collection) {
$output->writeln('<error>The Collection ' . $input->getArgument('collection') . ' not found.</error>');
return;
}
private function createCollection(InputInterface $input, $storage)
{
$collectionRepository = new CollectionRepository($storage);

$collectionField = new CollectionField();
$collectionField->setFieldId($entity);
$collectionField->setCollectionId($collection);
$collectionFieldRepository->save($collectionField);
}
} else {
$entity = new Collection();
$entity->setName($input->getArgument('name'));
$entity->setLabel($input->getArgument('label'));
$collectionRepository->save($entity);
}
$collection = (object)([
'name' => $input->getArgument('name'),
'label' => $input->getArgument('label')
]);

$collectionRepository->save($collection);

return $collection;
}

private function createField(InputInterface $input, $storage)
{
$fieldRepository = new FieldRepository($storage);

$field = (object)([
'type' => $input->getArgument('type'),
'name' => $input->getArgument('name'),
'label' => $input->getArgument('label')
]);

$fieldRepository->save($field);

return $field;
}

private function findACollection(InputInterface $input, $storage)
{
$collectionRepository = new CollectionRepository($storage);

return $collectionRepository->findBy(
['name' => $input->getArgument('collection')],
['order by name']
);
}

private function createTheFieldCollectionRelationship($entity, $collection, $storage)
{
$collectionFieldRepository = new CollectionFieldRepository($storage);

$collectionField = (object)([
'field_id' => $entity,
'collection_id' => $collection
]);

$collectionFieldRepository->save($collectionField);

return $collectionField;
}
}
30 changes: 30 additions & 0 deletions src/Console/Command/Database/DoctrineStorage.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

namespace WilliamEspindola\Field\Console\Command\Database;

use WilliamEspindola\Field\Storage\ORM\Doctrine;
use Doctrine\ORM\Tools\Setup;

class DoctrineStorage
{
protected $mapper;

public function __construct($config)
{
$setUp = Setup::createXMLMetadataConfiguration(array(__DIR__."/config/xml"), true);

$this->mapper = new Doctrine($config, $setUp);
}

public function getMapperInstance()
{
return $this->mapper->getMapper();
}

public function getExecuteQuery($query)
{
$stmt = $this->mapper->getMapper()->getConnection()->prepare($query);

return $stmt->execute();
}
}
Loading

0 comments on commit 4f65171

Please sign in to comment.