예제 #1
0
use Propilex\Hateoas\TransExpressionFunction;
use Propilex\Hateoas\VndErrorRepresentation;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Validator\Mapping\ClassMetadataFactory;
use Symfony\Component\Validator\Mapping\Loader\YamlFileLoader;
$app = new Silex\Application();
// Providers
$app->register(new Silex\Provider\ValidatorServiceProvider());
$app->register(new Silex\Provider\UrlGeneratorServiceProvider());
$app->register(new Propel\Silex\PropelServiceProvider(), array('propel.config_file' => __DIR__ . '/config/propel/propilex.php', 'propel.model_path' => __DIR__ . '/../src/Propilex/Model'));
// Configure the validator service
$app['validator.mapping.class_metadata_factory'] = new ClassMetadataFactory(new YamlFileLoader(__DIR__ . '/config/validation.yml'));
// Configure Hateoas serializer
$app['serializer'] = $app->share(function () use($app) {
    $jmsSerializerBuilder = JMS\Serializer\SerializerBuilder::create()->setMetadataDirs(array('' => __DIR__ . '/config/serializer', 'Propilex' => __DIR__ . '/config/serializer'))->setDebug($app['debug'])->setCacheDir(__DIR__ . '/cache/serializer');
    return Hateoas\HateoasBuilder::create($jmsSerializerBuilder)->setMetadataDirs(array('' => __DIR__ . '/config/serializer', 'Propilex' => __DIR__ . '/config/serializer'))->setDebug($app['debug'])->setCacheDir(__DIR__ . '/cache/hateoas')->setUrlGenerator(null, new SymfonyUrlGenerator($app['url_generator']))->setUrlGenerator('templated', new SymfonyUrlGenerator($app['templated_uri_generator']))->setXmlSerializer(new XmlHalSerializer())->addConfigurationExtension(new CuriesConfigurationExtension($app['curies_route_name'], 'templated'))->setExpressionContextVariable('curies_prefix', $app['curies_prefix'])->registerExpressionFunction(new TransExpressionFunction($app['translator']))->build();
});
$app['hateoas.pagerfanta_factory'] = $app->share(function () use($app) {
    return new PagerfantaFactory();
});
// Translation
$app->register(new Silex\Provider\TranslationServiceProvider());
$app->before(function (Request $request) use($app) {
    $validatorFile = __DIR__ . '/../vendor/symfony/validator/Symfony/Component/Validator/Resources/translations/validators.%s.xlf';
    $locale = $request->attributes->get('_language', 'en');
    $app['translator']->setLocale($locale);
    $app['translator']->addLoader('xlf', new Symfony\Component\Translation\Loader\XliffFileLoader());
    $app['translator']->addResource('xlf', sprintf($validatorFile, $locale), $locale, 'validators');
    $messagesLocale = $locale;
    if (!is_file($messagesFile = __DIR__ . '/config/messages.' . $messagesLocale . '.yml')) {
예제 #2
0
파일: test_rest.php 프로젝트: itkg/consumer
<?php

include_once __DIR__ . '/../vendor/autoload.php';
ini_set('display_errors', 1);
$adapter = new Itkg\Core\Cache\Adapter\Bridge\Doctrine(new \Doctrine\Common\Cache\FilesystemCache('/tmp'));
$registry = new \Itkg\Core\Cache\Adapter\Registry();
$eventDispatcher = new \Symfony\Component\EventDispatcher\EventDispatcher();
$eventDispatcher->addSubscriber(new \Itkg\Consumer\Listener\CacheListener($eventDispatcher));
$eventDispatcher->addSubscriber(new \Itkg\Consumer\Listener\LoggerListener());
$eventDispatcher->addSubscriber(new \Itkg\Consumer\Listener\DeserializerListener(JMS\Serializer\SerializerBuilder::create()->build()));
$eventDispatcher->addSubscriber(new \Itkg\Consumer\Listener\CacheControlListener(new \Itkg\Consumer\Cache\ServiceCacheQueueWriter($adapter)));
$service = new \Itkg\Consumer\Service\Service($eventDispatcher, new Itkg\Consumer\Client\RestClient(array('timeout' => 10)), array('identifier' => 'my test', 'cache_adapter' => $adapter, 'cache_ttl' => 3600, 'cache_warmup' => true, 'cache_fresh_ttl' => 10));
$service->sendRequest(\Symfony\Component\HttpFoundation\Request::create('XXXX'))->getResponse();
$service = new \Itkg\Consumer\Service\Service($eventDispatcher, new Itkg\Consumer\Client\RestClient(array('timeout' => 10)), array('cache_ttl' => 20, 'cache_adapter' => $registry, 'identifier' => 'my test'));
$service->sendRequest(\Symfony\Component\HttpFoundation\Request::create('XXXX'))->getResponse();
$service = new \Itkg\Consumer\Service\Service($eventDispatcher, new Itkg\Consumer\Client\RestClient(array('timeout' => 10)), array('identifier' => 'my test', 'logger' => new \Monolog\Logger('my_logger', array(new \Monolog\Handler\StreamHandler('/tmp/test')))));
$response = $service->sendRequest(\Symfony\Component\HttpFoundation\Request::create('XXXX'))->getResponse();
<?php

require_once __DIR__ . "/init.inc.php";
$serializer = JMS\Serializer\SerializerBuilder::create()->addMetadataDir(__DIR__ . "/src")->build();
$data = ['title' => 'Test Product 2', 'description' => '...', 'category' => ['id' => '1']];
$jsonData = json_encode($data);
$product = $serializer->deserialize($jsonData, 'Product', 'json');
// in this case category object should be loaded, that is the problem
// it works if we set
// $product->setCategory($category);
// Solution:
$product = $entityManager->merge($product);
$entityManager->persist($product);
$entityManager->flush();