Skip to content

Commit

Permalink
Sf4 improvements and privatize all services (#3)
Browse files Browse the repository at this point in the history
* autoregister commands so they work in sf4 and remove forms classes

* the decorating and aliasing doesn't play nice with sf4, make it all private and enforce di instead of container->get(...)

* update readme about controllers since NcrAware* is now gone
  • Loading branch information
gdbrown authored Dec 4, 2017
1 parent 6283c0f commit fdbcc02
Show file tree
Hide file tree
Showing 17 changed files with 173 additions and 169 deletions.
14 changes: 14 additions & 0 deletions CHANGELOG-0.x.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,24 @@
This changelog references the relevant changes done in 0.x versions.


## v0.2.0
__BREAKING CHANGES__

* Register all commands in `Command` namespace using new Symfony 4 convention.
* Require `"gdbots/pbjx-bundle": "~0.2"` which requires `"symfony/framework-bundle": "^4.0"`.
* Remove `Gdbots\Bundle\NcrBundle\Form` as symfony forms is no longer apart of `gdbots/pbjx-bundle`.
* Register interfaces and classes for autowiring:
* `Gdbots\Ncr\Ncr`
* `Gdbots\Ncr\NcrCache`
* `Gdbots\Ncr\NcrLazyLoader`
* `Gdbots\Ncr\NcrSearch`


## v0.1.1
* Add support for Symfony 4.
* Add `ncr:get-node` symfony console command.



## v0.1.0
* Initial version.
30 changes: 19 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,17 @@ Symfony bundle that integrates [gdbots/ncr](https://github.com/gdbots/ncr-php) l


# Configuration
Follow the standard [bundle install](http://symfony.com/doc/current/bundles/installation.html) using __gdbots/ncr-bundle__ as the composer package name.
Follow the standard [bundle install](http://symfony.com/doc/current/bundles/installation.html)
using __gdbots/ncr-bundle__ as the composer package name.

> The examples below assume you're running the DynamoDb Ncr and Elastica NcrSearch.
__config.yml:__
__config/packages/ncr.yml:__

```yaml
# many of the configurations below are defaults so you can remove them
# from your configuration, added here for reference

# these parameters would likely be in your parameters.yml file, not here
parameters:
cloud_region: 'us-west-2'
es_clusters:
Expand All @@ -35,7 +35,7 @@ gdbots_ncr:
ncr:
provider: dynamodb
memoizing:
enable: true # default
enabled: true # default
#
# IMPORTANT read_through notes for the memoizer
#
Expand Down Expand Up @@ -125,7 +125,7 @@ services:
# to optimize batch requests.
acme_ncr.ncr_lazy_loading_handler:
class: Acme\Ncr\NcrLazyLoadingHandler
public: true
public: false
arguments: ['@ncr_lazy_loader']
tags:
- {name: pbjx.event_subscriber}
Expand All @@ -134,17 +134,24 @@ services:


# Controllers
It is recommended to have data retrieval be the responsibility of Pbjx requests, however, that strategy doesn't work for all uses cases. A `NcrAwareControllerTrait` is provided which gives you methods to fetch the key Ncr services from the Symfony container.
It is recommended to have data retrieval be the responsibility of Pbjx requests, however,
that strategy doesn't work for all uses cases. Use Symfony autowiring and typehint the
interface in your constructor or setter methods to get key Ncr services.

Autowiring supported for these interfaces:

# Symfony Form Types (deprecated)
We have found that many node operations have a common requirement to hide/ignore certain fields that are generally only populated by the server. Extend the `Gdbots\Bundle\NcrBundle\Form\AbstractNodeType` for your `Node` types.
* `Gdbots\Ncr\Ncr`
* `Gdbots\Ncr\NcrCache`
* `Gdbots\Ncr\NcrLazyLoader`
* `Gdbots\Ncr\NcrSearch`


# Twig Extension
The `NcrExtension` provides one function called `ncr_get_node`. It is important to note that this does __NOT__ make a query to get a node, instead it pulls from `NcrCache`.
The `NcrExtension` provides one function called `ncr_get_node`. It is important to note
that this does __NOT__ make a query to get a node, instead it pulls from `NcrCache`.

> This might change in the future, but this strategy eliminates horribly performing twig templates that make Ncr queries.
> This might change in the future, but this strategy eliminates horribly performing
> twig templates that make Ncr queries.
The function will accept a `MessageRef`, `NodeRef` or a string version of a NodeRef.

Expand All @@ -158,7 +165,8 @@ Here is the creator:


# Console Commands
This library provides the basics for creating and extracting data from the Ncr services. Run the Symfony console and look for __ncr__ commands.
This library provides the basics for creating and extracting data from the Ncr services.
Run the Symfony console and look for __ncr__ commands.

```txt
ncr:create-search-storage Creates the NcrSearch storage.
Expand Down
6 changes: 3 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
"license": "Apache-2.0",
"require": {
"php": ">=7.1",
"gdbots/ncr": "^0.1.2",
"gdbots/pbjx-bundle": "^0.1.2"
"gdbots/ncr": "~0.1",
"gdbots/pbjx-bundle": "~0.2"
},
"require-dev": {
"phpunit/phpunit": "^6.4",
Expand All @@ -29,7 +29,7 @@
},
"extra": {
"branch-alias": {
"dev-master": "0.1.x-dev"
"dev-master": "0.2.x-dev"
}
}
}
19 changes: 14 additions & 5 deletions src/Command/CreateSearchStorageCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

namespace Gdbots\Bundle\NcrBundle\Command;

use Gdbots\Ncr\NcrSearch;
use Gdbots\Schemas\Ncr\Mixin\Indexed\IndexedV1Mixin;
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Symfony\Component\Console\Input\InputArgument;
Expand All @@ -13,7 +14,16 @@

class CreateSearchStorageCommand extends ContainerAwareCommand
{
use NcrAwareCommandTrait;
use NcrCommandTrait;

/**
* @param NcrSearch $ncrSearch
*/
public function __construct(NcrSearch $ncrSearch)
{
parent::__construct(null);
$this->ncrSearch = $ncrSearch;
}

/**
* {@inheritdoc}
Expand Down Expand Up @@ -62,7 +72,7 @@ protected function configure()
*
* @return null
*
* @throws \Exception
* @throws \Throwable
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
Expand All @@ -72,15 +82,14 @@ protected function execute(InputInterface $input, OutputInterface $output)

$io = new SymfonyStyle($input, $output);
$io->title('NcrSearch Storage Creator');
$ncrSearch = $this->getNcrSearch();

foreach ($this->getSchemasUsingMixin(IndexedV1Mixin::create(), $input->getArgument('qname')) as $schema) {
$qname = $schema->getQName();

try {
$ncrSearch->createStorage($qname, $context);
$this->ncrSearch->createStorage($qname, $context);
$io->success(sprintf('Created NcrSearch storage for "%s".', $qname));
} catch (\Exception $e) {
} catch (\Throwable $e) {
if (!$skipErrors) {
throw $e;
}
Expand Down
19 changes: 14 additions & 5 deletions src/Command/CreateStorageCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

namespace Gdbots\Bundle\NcrBundle\Command;

use Gdbots\Ncr\Ncr;
use Gdbots\Schemas\Ncr\Mixin\Node\NodeV1Mixin;
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Symfony\Component\Console\Input\InputArgument;
Expand All @@ -13,7 +14,16 @@

class CreateStorageCommand extends ContainerAwareCommand
{
use NcrAwareCommandTrait;
use NcrCommandTrait;

/**
* @param Ncr $ncr
*/
public function __construct(Ncr $ncr)
{
parent::__construct(null);
$this->ncr = $ncr;
}

/**
* {@inheritdoc}
Expand Down Expand Up @@ -62,7 +72,7 @@ protected function configure()
*
* @return null
*
* @throws \Exception
* @throws \Throwable
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
Expand All @@ -72,15 +82,14 @@ protected function execute(InputInterface $input, OutputInterface $output)

$io = new SymfonyStyle($input, $output);
$io->title('Ncr Storage Creator');
$ncr = $this->getNcr();

foreach ($this->getSchemasUsingMixin(NodeV1Mixin::create(), $input->getArgument('qname')) as $schema) {
$qname = $schema->getQName();

try {
$ncr->createStorage($qname, $context);
$this->ncr->createStorage($qname, $context);
$io->success(sprintf('Created Ncr storage for "%s".', $qname));
} catch (\Exception $e) {
} catch (\Throwable $e) {
if (!$skipErrors) {
throw $e;
}
Expand Down
19 changes: 14 additions & 5 deletions src/Command/DescribeSearchStorageCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

namespace Gdbots\Bundle\NcrBundle\Command;

use Gdbots\Ncr\NcrSearch;
use Gdbots\Schemas\Ncr\Mixin\Indexed\IndexedV1Mixin;
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Symfony\Component\Console\Input\InputArgument;
Expand All @@ -13,7 +14,16 @@

class DescribeSearchStorageCommand extends ContainerAwareCommand
{
use NcrAwareCommandTrait;
use NcrCommandTrait;

/**
* @param NcrSearch $ncrSearch
*/
public function __construct(NcrSearch $ncrSearch)
{
parent::__construct(null);
$this->ncrSearch = $ncrSearch;
}

/**
* {@inheritdoc}
Expand Down Expand Up @@ -56,7 +66,7 @@ protected function configure()
*
* @return null
*
* @throws \Exception
* @throws \Throwable
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
Expand All @@ -65,18 +75,17 @@ protected function execute(InputInterface $input, OutputInterface $output)

$io = new SymfonyStyle($input, $output);
$io->title('NcrSearch Storage Describer');
$ncrSearch = $this->getNcrSearch();

foreach ($this->getSchemasUsingMixin(IndexedV1Mixin::create(), $input->getArgument('qname')) as $schema) {
$qname = $schema->getQName();

try {
$details = $ncrSearch->describeStorage($qname, $context);
$details = $this->ncrSearch->describeStorage($qname, $context);
$io->success(sprintf('Describing NcrSearch storage for "%s".', $qname));
$io->comment(sprintf('context: %s', json_encode($context)));
$io->text($details);
$io->newLine();
} catch (\Exception $e) {
} catch (\Throwable $e) {
$io->error(sprintf('Failed to describe NcrSearch storage for "%s".', $qname));
$io->text($e->getMessage());
if ($e->getPrevious()) {
Expand Down
19 changes: 14 additions & 5 deletions src/Command/DescribeStorageCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

namespace Gdbots\Bundle\NcrBundle\Command;

use Gdbots\Ncr\Ncr;
use Gdbots\Schemas\Ncr\Mixin\Node\NodeV1Mixin;
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Symfony\Component\Console\Input\InputArgument;
Expand All @@ -13,7 +14,16 @@

class DescribeStorageCommand extends ContainerAwareCommand
{
use NcrAwareCommandTrait;
use NcrCommandTrait;

/**
* @param Ncr $ncr
*/
public function __construct(Ncr $ncr)
{
parent::__construct(null);
$this->ncr = $ncr;
}

/**
* {@inheritdoc}
Expand Down Expand Up @@ -56,7 +66,7 @@ protected function configure()
*
* @return null
*
* @throws \Exception
* @throws \Throwable
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
Expand All @@ -65,18 +75,17 @@ protected function execute(InputInterface $input, OutputInterface $output)

$io = new SymfonyStyle($input, $output);
$io->title('Ncr Storage Describer');
$ncr = $this->getNcr();

foreach ($this->getSchemasUsingMixin(NodeV1Mixin::create(), $input->getArgument('qname')) as $schema) {
$qname = $schema->getQName();

try {
$details = $ncr->describeStorage($qname, $context);
$details = $this->ncr->describeStorage($qname, $context);
$io->success(sprintf('Describing Ncr storage for "%s".', $qname));
$io->comment(sprintf('context: %s', json_encode($context)));
$io->text($details);
$io->newLine();
} catch (\Exception $e) {
} catch (\Throwable $e) {
$io->error(sprintf('Failed to describe Ncr storage for "%s".', $qname));
$io->text($e->getMessage());
if ($e->getPrevious()) {
Expand Down
17 changes: 14 additions & 3 deletions src/Command/ExportNodesCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
namespace Gdbots\Bundle\NcrBundle\Command;

use Gdbots\Common\Util\NumberUtils;
use Gdbots\Ncr\Ncr;
use Gdbots\Schemas\Ncr\Mixin\Node\Node;
use Gdbots\Schemas\Ncr\Mixin\Node\NodeV1Mixin;
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
Expand All @@ -15,7 +16,16 @@

class ExportNodesCommand extends ContainerAwareCommand
{
use NcrAwareCommandTrait;
use NcrCommandTrait;

/**
* @param Ncr $ncr
*/
public function __construct(Ncr $ncr)
{
parent::__construct(null);
$this->ncr = $ncr;
}

/**
* {@inheritdoc}
Expand Down Expand Up @@ -72,6 +82,8 @@ protected function configure()
* @param OutputInterface $output
*
* @return null
*
* @throws \Throwable
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
Expand All @@ -84,7 +96,6 @@ protected function execute(InputInterface $input, OutputInterface $output)
$context = json_decode($input->getOption('context') ?: '{}', true);
$context['tenant_id'] = (string)$input->getOption('tenant-id');

$ncr = $this->getNcr();
$i = 0;

$receiver = function (Node $node) use ($errOutput, $batchSize, $batchDelay, &$i) {
Expand All @@ -104,7 +115,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
};

foreach ($this->getSchemasUsingMixin(NodeV1Mixin::create(), $input->getArgument('qname')) as $schema) {
$ncr->pipeNodes($schema->getQName(), $receiver, $context);
$this->ncr->pipeNodes($schema->getQName(), $receiver, $context);
}
}
}
Loading

0 comments on commit fdbcc02

Please sign in to comment.