PHP code example of cosmologist / symfony-common-bundle

1. Go to this page and download the library: Download cosmologist/symfony-common-bundle library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

cosmologist / symfony-common-bundle example snippets


$connection->fetchAllIndexed('SELECT id, name FROM users');
// 1  => [id: 1, name: Ivan]
// 7  => [id: 7, name: Vasiliy]
// ...

$utils = $container->get(Cosmologist\Bundle\SymfonyCommonBundle\Doctrine\DoctrineUtils::class);
// or
$utils = $container->get('symfony_common.doctrine.utils');

$entity = $entityManager->find(App\Entity\Foo::class, $identifier);
DoctrineUtils::getRealClass($entity); \\ "App\Entity\Foo"
DoctrineUtils::getRealClass(App\Entity\Foo::class); \\ "App\Entity\Foo"

$utils->getClassMetadata($entity);
$utils->getClassMetadata(Entity::class);

$doctrineUtils->getAssociationTargetClassRecursive('AppBundle/Entity/Company', 'contact.user'); // 'AppBundle/Entity/User'

$utils->getEntitySingleIdentifierField($entity);
$utils->getEntitySingleIdentifierField(Entity::class);

$utils->getEntitySingleIdentifierValue($entity);

$utils->isEntity($entity);

$this->getEntityAlias(FooBundle\Entity\Bar\Baz::class); // 'foo.bar.baz'
$this->decodeEntityAlias('foo.bar.baz'); // 'FooBundle\Entity\Bar\Baz'

$qb = $entityManager->getRepository(Company::class)->createQueryBuilder('company');

# Recursive joins
DoctrineUtils::joinRecursive($qb, 'contact.user.type');
// equivalent to
$qb
  ->join('company.contact', 'contact')
  ->join('contact.user', 'user')
  ->join('user.type', 'type');

// Adds join and returns an alias of added join
DoctrineUtils::joinOnce($qb, 'contact.user', 'u1'); // "u1"

// If a join with specified parameters exists then only returns an alias of existed join 
DoctrineUtils::joinOnce($qb, 'contact.user', 'u2'); // "u1"

$resultCriteria = DoctrineUtils::mergeCriteria($firstCriteria, $secondCriteria); 

$utils = $container->get('symfony_common.routing.utils');

$utils->forwardToUri('/products/programmers-t-shirts');
// or
$utils->forwardToUri('https://myshop.com/products/programmers-t-shirts');

$crypto = $container->get(Cosmologist\Bundle\SymfonyCommonBundle\Security\Crypto::class);
$crypto->decrypt($crypto->encrypt('The sensitive string')); // 'The sensitive string'

DependencyInjectionUtils::getDoctrineDbalConnectionReference('default'); // doctrine.dbal.default_connection

DependencyInjectionUtils::getDoctrineOrmEntityManagerReference('default'); // doctrine.orm.default_entity_manager

# AppBundle\DependencyInjection\Configuration.php
...
use Cosmologist\Bundle\SymfonyCommonBundle\DependencyInjection\DependencyInjectionUtils;
...
->arrayNode('events')
    ->beforeNormalization()
        ->always(ConfigurationUtils::useKeyAsAttribute('server'))
    ->end()
    ->prototype('array')
...

use Cosmologist\Bundle\SymfonyCommonBundle\DependencyInjection\ContainerStatic;

ContainerStatic::getContainer();
ContainerStatic::get('serivice_id');
ContainerStatic::getParameter('parameter_id');

use Cosmologist\Bundle\SymfonyCommonBundle\BrowserKit\BrowserKitUtils;

/** @var \Symfony\Component\BrowserKit\Client $cient */

BrowserKitUtils::addHeader($client, 'header-name', 'header-value');