コード例 #1
0
ファイル: ExcelServiceTest.php プロジェクト: phppro/silex-api
 public function testImportFileNoHeaders()
 {
     $s = new ExcelService();
     $ctx = new \stdClass();
     $ctx->data = [];
     $s->importFile(__DIR__ . '/excel/basic.xlsx', function ($row, $ctx) {
         $ctx->data[] = $row;
         return [];
     }, null, null, null, $ctx, ['noHeaders' => true]);
     $this->assertEquals([['field1', 'field2', 'field3'], ['a', 'b'], ['c', 'd', 'e']], $ctx->data);
 }
コード例 #2
0
 /**
  * @param Application $app
  */
 protected function registerStaticServices(Application $app)
 {
     $app['api.services.guzzle'] = $app->share(function () use($app) {
         return new Client(['defaults' => $app['api.http.defaults']]);
     });
     $app['api.services.mandrillSdk'] = $app->share(function () use($app) {
         if (!isset($app['api.mandrill.key'])) {
             throw new \RuntimeException("No Mandrill API Key set", 500);
         }
         return new \Mandrill($app['api.mandrill.key']);
     });
     $app['api.services.ovh'] = $app->share(function () use($app) {
         $service = new Api($app['api.ovh.applicationKey'], $app['api.ovh.applicationSecret'], $app['api.ovh.endPointName'], $app['api.ovh.consumerKey']);
         return $service;
     });
     $app['api.services.ovhSms'] = $app->share(function () use($app) {
         $service = new Ovh\SmsService();
         $service->setEventDispatcher($app['dispatcher']);
         $service->setApi($app['api.services.ovh']);
         return $service;
     });
     $app['api.services.swiftMailer'] = $app->share(function () use($app) {
         $service = new SwiftMailerService();
         $service->setLoggerService($app['api.services.logger']);
         $service->setEventDispatcher($app['dispatcher']);
         $service->setSwiftMailer($app['mailer']);
         return $service;
     });
     $app['api.services.mail'] = $app->share(function () use($app) {
         $service = new MailService();
         $service->setEventDispatcher($app['dispatcher']);
         $transportKey = 'api.services.' . $app['api.email.transport'];
         if (!isset($app[$transportKey])) {
             throw new \RuntimeException(sprintf("Unknown Mail transport '%s'", $app['api.email.transport']), 500);
         }
         $service->setTransport($app[$transportKey]);
         $service->setBlackHole($app['api.notifications.blackHole']);
         $service->setTemplatingService($app['api.services.templating']);
         $service->setDefaultSenderEmail($app['api.email.sender']);
         return $service;
     });
     $app['api.services.mandrill'] = $app->share(function () use($app) {
         $service = new MandrillService();
         $service->setMandrillSdk($app['api.services.mandrillSdk']);
         $service->setEventDispatcher($app['dispatcher']);
         $service->setLoggerService($app['api.services.logger']);
         return $service;
     });
     $app['api.services.sms'] = $app->share(function () use($app) {
         $service = new SmsService();
         $service->setEventDispatcher($app['dispatcher']);
         $transportKey = 'api.services.' . $app['api.sms.transport'];
         if (!isset($app[$transportKey])) {
             throw new \RuntimeException(sprintf("Unknown SMS transport '%s'", $app['api.sms.transport']), 500);
         }
         $service->setTransport($app[$transportKey]);
         $service->setBlackHole($app['api.notifications.blackHole']);
         $service->setTemplatingService($app['api.services.templating']);
         $service->setDefaultSenderSms($app['api.sms.sender']);
         return $service;
     });
     $app['api.services.mongo'] = $app->share(function () use($app) {
         if (!isset($app['api.mongo.host'])) {
             throw new \RuntimeException(sprintf("Missing mongo host"), 500);
         }
         $dbHosts = $app['api.mongo.host'];
         if (!is_array($dbHosts)) {
             $dbHosts = [$dbHosts];
         }
         $dbHosts = join(',', $dbHosts);
         $options = ['connect' => true];
         if (isset($app['api.mongo.replicaset'])) {
             $options += ['replicaSet' => $app['api.mongo.replicaset']];
         }
         $server = sprintf('mongodb://%s%s/%s', isset($app['api.mongo.user']) && strlen($app['api.mongo.user']) ? sprintf('%s:%s@', $app['api.mongo.user'], $app['api.mongo.pass']) : '', $dbHosts, $app['api.mongo.db']);
         return new \MongoClient($server, $options);
     });
     $app['api.services.logger'] = $app->share(function () use($app) {
         $service = new LoggerService();
         $service->setEventDispatcher($app['dispatcher']);
         $service->setMonolog($app['monolog']);
         return $service;
     });
     $app['api.services.payPal'] = $app->share(function () use($app) {
         $service = new PayPalService();
         $service->setEventDispatcher($app['dispatcher']);
         $service->setLoggerService($app['api.services.logger']);
         $service->setPayPal($app['api.services.payPalSdk']);
         if (isset($app['api.paypal.currency.default'])) {
             $service->setCurrency($app['api.paypal.currency.default']);
         }
         if (isset($app['api.paypal.environment'])) {
             $service->setEnvironment($app['api.paypal.environment']);
         }
         if (isset($app['api.paypal.version'])) {
             $service->setVersion($app['api.paypal.version']);
         }
         return $service;
     });
     $app['api.services.payPalSdk'] = $app->share(function () use($app) {
         if (isset($app['api.paypal.config'])) {
             $config = $app['api.paypal.config'];
         } else {
             $config = ['mode' => $app['api.paypal.environment'], 'acct1.UserName' => $app['api.paypal.username'], 'acct1.Password' => $app['api.paypal.password'], 'acct1.Signature' => $app['api.paypal.signature']];
         }
         return new PayPalAPIInterfaceServiceService($config);
     });
     $app['api.services.mangoPay'] = $app->share(function () use($app) {
         $service = new MangoPayService();
         $service->setEventDispatcher($app['dispatcher']);
         $service->setLoggerService($app['api.services.logger']);
         if (isset($app['api.mangoPay.currency.default'])) {
             $service->setCurrency($app['api.mangoPay.currency.default']);
         }
         $env = 'live';
         if (isset($app['api.mangoPay.environment'])) {
             $env = $app['api.mangoPay.environment'];
         }
         switch ($env) {
             case 'live':
                 $service->setMangoPay($app['api.services.mangoPayApi.live']);
                 break;
             case 'sandbox':
                 $service->setMangoPay($app['api.services.mangoPayApi.sandbox']);
                 break;
             default:
                 throw new \RuntimeException(sprintf("Unsupported MangoPay environment '%s'", $env), 500);
         }
         $service->setEnvironment($env);
         return $service;
     });
     $app['api.services.mangoPayApi.sandbox'] = $app->share(function () use($app) {
         if (isset($app['api.mangoPay.config'])) {
             $config = $app['api.mangoPay.config'];
         } else {
             $config = ['ClientId' => $app['api.mangoPay.clientId'], 'ClientPassword' => $app['api.mangoPay.clientPassword'], 'TemporaryFolder' => $app['api.mangoPay.temporaryFolder'], 'BaseUrl' => 'https://api.sandbox.mangopay.com'];
         }
         $api = new MangoPayApi();
         foreach ($config as $key => $value) {
             $api->Config->{$key} = $value;
         }
         return $api;
     });
     $app['api.services.mangoPayApi.live'] = $app->share(function () use($app) {
         if (isset($app['api.mangoPay.config'])) {
             $config = $app['api.mangoPay.config'];
         } else {
             $config = ['ClientId' => $app['api.mangoPay.clientId'], 'ClientPassword' => $app['api.mangoPay.clientPassword'], 'TemporaryFolder' => $app['api.mangoPay.temporaryFolder'], 'BaseUrl' => 'https://api.mangopay.com'];
         }
         $api = new MangoPayApi();
         foreach ($config as $key => $value) {
             $api->Config->{$key} = $value;
         }
         return $api;
     });
     $app['api.services.route'] = $app->share(function () use($app) {
         $service = new RouteService();
         $service->setEventDispatcher($app['dispatcher']);
         return $service;
     });
     $app['api.services.event'] = $app->share(function () use($app) {
         $service = new EventService();
         $service->setEventDispatcher($app['dispatcher']);
         $service->setApplication($app);
         return $service;
     });
     $app['api.services.translation'] = $app->share(function () use($app) {
         $service = new TranslationService();
         $service->setEventDispatcher($app['dispatcher']);
         $service->setTranslator($app['translator']);
         return $service;
     });
     $app['api.services.notification'] = $app->share(function () use($app) {
         $service = new NotificationService();
         $service->setEventDispatcher($app['dispatcher']);
         $service->setTranslationService($app['api.services.translation']);
         $service->setMailService($app['api.services.mail']);
         $service->setSmsService($app['api.services.sms']);
         return $service;
     });
     $app['api.services.excel'] = $app->share(function () use($app) {
         $service = new ExcelService();
         $service->setEventDispatcher($app['dispatcher']);
         return $service;
     });
     $app['api.services.templating'] = $app->share(function () use($app) {
         $service = new TemplatingService();
         $service->setEventDispatcher($app['dispatcher']);
         $service->setTemplating($app['twig']);
         return $service;
     });
     $app['api.services.database'] = $app->share(function () use($app) {
         $service = new DatabaseService();
         $service->setApplication($app);
         $service->setCollectionName($app['api.mongo.collections.migration']);
         $service->setDatabaseName($app['api.mongo.db']);
         $service->setDirectory($app['api.data.dir']);
         $service->setEnvironment($app['api.env']);
         $service->setEventDispatcher($app['dispatcher']);
         $service->setLoggerService($app['api.services.logger']);
         $service->setMongoClient($app['api.services.mongo']);
         $service->setFormService($app['api.services.form']);
         return $service;
     });
     $app['api.services.request'] = $app->share(function () use($app) {
         $service = new RequestService();
         $service->setEventDispatcher($app['dispatcher']);
         $service->setLoggerService($app['api.services.logger']);
         $service->setUserProvider($app['security.user_provider.default']);
         $service->setClientHeaderKey($app['api.security.client.header']);
         $service->setUserHeaderKey($app['api.security.user.header']);
         $service->setClientTokenCreationUriPattern($app['api.security.client.token_creation.uri_pattern']);
         $service->setUserTokenCreationUriPattern($app['api.security.user.token_creation.uri_pattern']);
         $service->setClientSecret($app['api.security.client.secret']);
         $service->setUserSecret($app['api.security.user.secret']);
         return $service;
     });
     $app['api.services.http'] = $app->share(function () use($app) {
         $service = new HttpService();
         $service->setEventDispatcher($app['dispatcher']);
         $service->setLoggerService($app['api.services.logger']);
         $service->setClient($app['api.services.guzzle']);
         return $service;
     });
     $app['api.services.form'] = $app->share(function () use($app) {
         $service = new FormService();
         $service->setEventDispatcher($app['dispatcher']);
         $service->setFormFactory($app['form.factory']);
         $service->setForms([]);
         $service->setTypeNamespaces($app['api.namespaces.forms']);
         return $service;
     });
     $app['security.authentication_listener.factory.api'] = $app->protect(function ($name) use($app) {
         $app['security.authentication_provider.' . $name . '.api'] = $app->share(function () use($app) {
             $provider = new ApiAuthenticationProvider($app['security.user_provider.default']);
             $provider->setEventDispatcher($app['dispatcher']);
             $provider->setRequestService($app['api.services.request']);
             return $provider;
         });
         $app['security.authentication_listener.' . $name . '.api'] = $app->share(function () use($app) {
             $listener = new ApiListener($app['security'], $app['security.authentication_manager']);
             $listener->setEventDispatcher($app['dispatcher']);
             $listener->setRequestService($app['api.services.request']);
             return $listener;
         });
         return array('security.authentication_provider.' . $name . '.api', 'security.authentication_listener.' . $name . '.api', null, 'pre_auth');
     });
     $app['security.user_provider.default'] = $app->share(function () use($app) {
         $provider = new ApiUserProvider();
         return $provider;
     });
     $app['security.user_provider.resources'] = $app->extend('security.user_provider.default', function ($s) {
         return $s;
     });
     $app['api.services.pchart.factory'] = $app->share(function () use($app) {
         return new pChartFactory();
     });
     $app['api.services.chart'] = $app->share(function () use($app) {
         $s = new ChartService();
         $s->setPChartFactoryService($app['api.services.pchart.factory']);
         $s->setEventDispatcher($app['dispatcher']);
         return $s;
     });
 }