I'm trying to use zend expressive and looking at how to do database stuff now. I was looking at this, but it's not clear. I used composer to install zend-db and it mentioned to add a line in dependencies.global.php and then use container in the factory class to get the adapter but then didn't talk about how to access it in the actual action class so I don't know what's going on as the adapter object is out of scope from other class.
Anyone has good and clear example from start to finish to actually able to connect and query sql?
try to inject your db class via factory, follow example on skeleton app, you can do something like this:
HomePageFactory.php
public function __invoke(ContainerInterface $container)
{
$router = $container->get(RouterInterface::class);
$template = $container->has(TemplateRendererInterface::class) ? $container->get(TemplateRendererInterface::class) : null;
$adapter = $container->get( Adapter::class );
$usersTable = $container->get( Table\UsersTable::class );
return new HomePageAction($router, $template,$adapter,$usersTable);
}
HomePageAction.php
class HomePageAction implements ServerMiddlewareInterface
{
/**
* #var Router\RouterInterface
*/
private $router;
/**
* #var null|Template\TemplateRendererInterface
*/
private $template;
/**
* #var Adapter
*/
private $dbAdapter;
/**
* #var UsersTable
*/
private $usersTable;
/**
* HomePageAction constructor.
* #param Router\RouterInterface $router
* #param Template\TemplateRendererInterface|null $template
* #param Adapter $adapter
* #param UsersTable $usersTable
*/
public function __construct( Router\RouterInterface $router, Template\TemplateRendererInterface $template = null, Adapter $adapter, Table\UsersTable $usersTable )
{
$this->router = $router;
$this->template = $template;
$this->dbAdapter = $adapter;
$this->usersTable = $usersTable;
}
on your config provider where you have your tables u have to config dependencies as factory EX:
'factories' => [
Table\UsersTable::class => function($container) {
$dbAdapter = $container->get( AdapterInterface::class );
$resultSetPrototype = new ResultSet();
$resultSetPrototype->setArrayObjectPrototype( new Model\Users() );
$tableGateway = new TableGateway('users', $dbAdapter, null, $resultSetPrototype);
return new Table\UsersTable($tableGateway);
},
],
Related
I'm running a bitbucket pipeline to execute all the unit test with PHP Unit. When I execute the test on local all of them pass. But on the bitbucket pipeline it always fail. In this case, the tests are related to an external service that we are checking.
<?php
namespace Tests\Unit;
use Tests\TestCase;
use Illuminate\Foundation\Testing\RefreshDatabase;
use MyService;
class MyTest extends TestCase
{
/**
* Test the dummies in this new system
*
* #return void
*/
public function testDumies()
{
$games = DummyService::getDummies();
$this->assertTrue(count($dummies) > 0);
}
public function testDummiesOfUser()
{
$dummies = DummyService::getDummiesOfUser('someemail#mail.com');
$this->assertTrue(count($dummies) > 0);
}
}
And the following is the service to get the dummies
<?php
namespace App\Services;
class DummyService {
/**
* Get dummies
*
* #return void
*/
public function getDummies() {
$collection = [];
$games = $this->getDummiesInUrl('http://my-project/api/v1/platform/dummies');
foreach($dummies as $dummy) {
$collection[] = $dummy;
}
return $collection;
}
/**
* Retrieves the dummies in url
*
* #param string $endpoint
* #return array
*/
public function getDummiesInUrl($endpoint) {
$client = new \GuzzleHttp\Client();
$res = $client->request('GET', $endpoint);
$body = $res->getBody();
$body = json_decode($body, true);
$data = $body['data'];
$dummies = $data['dummies'];
return $dummies;
}
/**
* Returns the dummies of an user
*
* #param string $email
* #return array
*/
public function getDummiesOfUser($email) {
$collection = [];
$dummies = $this->getDummiesOfUserInUrl('http://myroute/api/v1/platform/dummies/user', $email);
foreach($dummies as $d) {
$collection[] = $d;
}
return $collection;
}
/**
* Get gameplays in url
*
* #param string $endpoint
* #param string $email
* #return array
*/
public function getDummiesOfUserInUrl($endpoint, $email) {
$client = new \GuzzleHttp\Client();
$res = $client->request('GET', $endpoint, ['query' => ['email' => $email]]);
$body = $res->getBody();
$body = json_decode($body, true);
$data = $body['data'];
$dummies = $data['dummiess'];
return $dummies;
}
}
But when that is tested on the bitbucket pipeline I got the following errors:
PDOException: SQLSTATE[HY000] [2054] The server requested authentication method unknown to the client and Caused by PDOException:PDO::__construct(): The server requested authentication method unknown to the client [caching_sha2_password]
Apparently with the new public version of mysql 8, lots of things have changed. So in order to keep working with your pipelines, I edited my bitbucket-pipelines.yml and changed the mysql image version from mysql to mysql:5.7.22
definitions:
services:
mysql:
image: mysql:5.7.22
environment:
MYSQL_DATABASE: 'homestead'
MYSQL_RANDOM_ROOT_PASSWORD: 'yes'
MYSQL_USER: 'homestead'
MYSQL_PASSWORD: 'secret'
I need to implement an API with a PUT method and I would like to use the ParamConverter in my Controller to find an existing entity object, or if the entity object doesn't exist, to create a new one.
However the standard Symfony ParamConverter returns an exception if it doesn't find the entity object in the repository.
Do you have any ideas to do that in a nice and clean way ? Thx.
Here is an example of what I would like to do (I use FOS REST Bundle to handle the PUT request):
/**
* #param Request $request
* #return View
*
* #ParamConverter("video")
*
*/
public function putVideosAction(Request $request, Video $video)
{
try {
return $this->getHandlerVideos()->put($video, $request->request->all());
} catch (InvalidFormException $e) {
return $e->getForm();
}
}
Here's a solution. Please give me your thoughts on it.
In your controller, I would do that:
/**
* #param Request $request
* #return View
*
* #Rest\Put()
* #Rest\View()
*
* #ParamConverter("video", converter="app_get_or_create_entity_converter", options={"repository_method" = "findOneById"})
*/
public function putVideosAction(Request $request, Video $video)
{
try {
$video = $this->getHandlerVideos()->put($video, $request->request->all());
return $video;
} catch (InvalidFormException $e) {
return $e->getForm();
}
}
I would write a dynamic param converter that way:
class GetOrCreateEntityConverter implements \Sensio\Bundle\FrameworkExtraBundle\Request\ParamConverter\ParamConverterInterface
{
/**
* #var EntityManagerInterface
*/
protected $entityManager;
/**
* #var ManagerRegistry $registry Manager registry
*/
private $registry;
/**
* #param ManagerRegistry $registry
* #param EntityManagerInterface $entityManager
*/
public function __construct(ManagerRegistry $registry, EntityManagerInterface $entityManager)
{
$this->registry = $registry;
$this->entityManager = $entityManager;
}
public function supports(ParamConverter $configuration)
{
if ('app_get_or_create_entity_converter' !== $configuration->getConverter()) {
return false;
}
return true;
}
/**
* {#inheritdoc}
*
* Applies converting
*
* #throws \InvalidArgumentException When route attributes are missing
* #throws NotFoundHttpException When object not found
*/
public function apply(Request $request, ParamConverter $configuration)
{
$name = $configuration->getName();
$options = $configuration->getOptions();
$class = $configuration->getClass();
$repository = $this->entityManager->getRepository($class);
$repositoryMethod = $options['repository_method'];
if (!is_callable([$repository, $repositoryMethod])) {
throw new \BadMethodCallException($repositoryMethod . ' function does not exist.', 405);
}
$entity = $repository->$repositoryMethod($id);
if (null === $entity) {
$entity = new $class;
}
$request->attributes->set($name, $entity);
}
}
If you ask why I return a form in the catch, please go and see https://github.com/liuggio/symfony2-rest-api-the-best-2013-way/blob/master/src/Acme/BlogBundle/Controller/PageController.php
You'll have to create your own custom paramConverter.
First, here is what you want to write in your controller:
/**
* #ParamConverter("video", class = "MyBundle:Video", converter = "my_param_converter")
* #param Request $request
* #param Video $video
* #return \Symfony\Component\HttpFoundation\Response
*/
public function putVideosAction(Request $request, Video $video)
{
// your code..
}
Now let's write the my_param_converter!
use Doctrine\Common\Persistence\ManagerRegistry;
use Sensio\Bundle\FrameworkExtraBundle\Request\ParamConverter\ParamConverterInterface;
// ...
class MyParamConverter implements ParamConverterInterface
{
private $registry;
/**
* #param ManagerRegistry $registry
*/
public function __construct(ManagerRegistry $registry = null)
{
$this->registry = $registry;
}
/**
* Check if object supported by our paramConverter
*
* #param ParamConverter $configuration
*/
public function supports(ParamConverter $configuration)
{
// In this case we can do nothing and just return
if (null === $this->registry || !count($this->registry->getManagers())) {
return false;
}
// Check if the class is set in configuration
if(null === $configuration->getClass()) {
return false;
}
// Get actual entity manager for class
$em = $this->registry->getManagerForClass($configuration->getClass());
// Check what you need to check...
return true;
}
public function apply(Request $request, ParamConverter $configuration)
{
$videoId = $request->attributes->get('video');
if(null === videoId) {
throw new \InvalidArgumentException('Route attribute is missing');
}
// Get actual entity manager for class
$em = $this->registry->getManagerForClass($configuration->getClass());
$repository = $em->getRepository($configuration->getClass());
// Try to find the video
$video = $$repository->findOneById($videoId);
if($video === null || !($video instanceof Video)) {
// Here you can create your new video object
}
// Map video to the route's parameter
$request->attributes->set($configuration->getName(), $video);
}
}
Once your new paramConverter wrote, declare it as a service:
services:
app.param_converter.my_param_converter:
class: YourBundle\Path\To\MyParamConverter
tags:
- { name: request.param_converter, converter: my_param_converter }
arguments:
- #?doctrine
Here you're done!
My answer is largely inspired by this article and hope is helpful.
I'm using Laravel to develop my website, and I integrated the linkedin login to it, it works fine for like a few months. then suddenly, last week i received an error. I didn't change any code that has something to do with Linkedin API. I'm suspecting whether it has something to do with Linkedin itself.
here's what the error looks like:
If you are using this library to integrate Linkedin to your laravel project, there might be an issue right now with the update. you can simply fix the error in OAuth.php by replacing it with this code:
<?php namespace Artdarek\OAuth;
/**
* #author Dariusz Prząda <artdarek#gmail.com>
* #copyright Copyright (c) 2013
* #license http://www.opensource.org/licenses/mit-license.html MIT License
*/
use \Config;
use \URL;
use \OAuth\ServiceFactory;
use \OAuth\Common\Consumer\Credentials;
class OAuth {
/**
* #var ServiceFactory
*/
private $_serviceFactory;
/**
* Storege name from config
*
* #var string
*/
private $_storage_name = 'Session';
/**
* Client ID from config
*
* #var string
*/
private $_client_id;
/**
* Client secret from config
*
* #var string
*/
private $_client_secret;
/**
* Scope from config
*
* #var array
*/
private $_scope = [];
/**
* Constructor
*
* #param ServiceFactory $serviceFactory - (Dependency injection) If not provided, a ServiceFactory instance will be constructed.
*/
public function __construct(ServiceFactory $serviceFactory = null)
{
if (null === $serviceFactory)
{
// Create the service factory
$serviceFactory = new ServiceFactory();
}
$this->_serviceFactory = $serviceFactory;
}
/**
* Detect config and set data from it
*
* #param string $service
*/
public function setConfig($service)
{
// if config/oauth-4-laravel.php exists use this one
if (Config::get('oauth-5-laravel.consumers') != null)
{
$this->_storage_name = Config::get('oauth-5-laravel.storage', 'Session');
$this->_client_id = Config::get("oauth-5-laravel.consumers.$service.client_id");
$this->_client_secret = Config::get("oauth-5-laravel.consumers.$service.client_secret");
$this->_scope = Config::get("oauth-5-laravel.consumers.$service.scope", []);
// esle try to find config in packages configs
}
else
{
$this->_storage_name = Config::get('oauth-5-laravel::storage', 'Session');
$this->_client_id = Config::get("oauth-5-laravel::consumers.$service.client_id");
$this->_client_secret = Config::get("oauth-5-laravel::consumers.$service.client_secret");
$this->_scope = Config::get("oauth-5-laravel::consumers.$service.scope", []);
}
}
/**
* Create storage instance
*
* #param string $storageName
*
* #return OAuth\Common\\Storage
*/
public function createStorageInstance($storageName)
{
$storageClass = "\\OAuth\\Common\\Storage\\$storageName";
$storage = new $storageClass();
return $storage;
}
/**
* Set the http client object
*
* #param string $httpClientName
*
* #return void
*/
public function setHttpClient($httpClientName)
{
$httpClientClass = "\\OAuth\\Common\\Http\\Client\\$httpClientName";
$this->_serviceFactory->setHttpClient(new $httpClientClass());
}
/**
* #param string $service
* #param string $url
* #param array $scope
*
* #return \OAuth\Common\Service\AbstractService
*/
public function consumer($service, $url = null, $scope = null)
{
// get config
$this->setConfig($service);
// get storage object
$storage = $this->createStorageInstance($this->_storage_name);
// create credentials object
$credentials = new Credentials(
$this->_client_id,
$this->_client_secret,
$url ? : URL::current()
);
// check if scopes were provided
if (is_null($scope))
{
// get scope from config (default to empty array)
$scope = $this->_scope;
}
// return the service consumer object
return $this->_serviceFactory->createService($service, $credentials, $storage, $scope);
}
}
FYI : I'm very new to Laravel and doing my best to learn it properly.
Working on an auth driver that uses a soap service to authenticate.
Error I get when trying to test with Auth::attempt()
Symfony \ Component \ Debug \ Exception \ FatalErrorException (E_COMPILE_ERROR)
Declaration of Project\Providers\AuthUserProvider::retrieveByToken() must be compatible with Illuminate\Auth\UserProviderInterface::retrieveByToken($identifier, $token)
Here is the driver...
<?php namespace Project\Providers;
use Illuminate\Auth\UserProviderInterface;
use Illuminate\Auth\GenericUser;
use Illuminate\Auth\UserInterface;
class AuthUserProvider implements UserProviderInterface {
/**
* External webservice for authentication
*/
private $webservice;
/**
* The user object.
*/
private $user;
/**
* Constructor
*
* #return void
*/
public function __construct(\Project\Webservice\AuthCheckApi $webservice)
{
$this->webservice = $webservice;
$this->user = null;
}
/**
* Retrieves a user by id
*
* #param int $identifier
* #return mixed null|array
*/
public function retrieveByID($identifier)
{
$this->user = is_null($this->user) ? $this->webservice->find($identifier) : $this->user;
return $this->user;
}
/**
* Tries to find a user based on the credentials passed.
*
* #param array $crendtials username|password
* #return mixed bool|UserInterface
*/
public function retrieveByCredentials(array $credentials)
{
if(!$user = $this->webservice->byusername($credentials['username'],$credentials['password'])) return false;
return new GenericUser($user);
}
/**
* Validates the credentials passed to the ones in webservice.
*
* #param UserInterface $user
* #param array $credentials
* #return bool
*/
public function validateCredentials(\Illuminate\Auth\UserInterface $user, array $credentials)
{
$validated = $this->webservice->validateCredentials($user,$credentials['username']);
return true;
}
/**
* Needed by Laravel 4.1.26 and above
*/
public function retrieveByToken()
{
return true;
}
/**
* Needed by Laravel 4.1.26 and above
*/
public function updateRememberToken()
{
return false;
}
}
Thanks for any help.
You are implementing the UserProviderInterface so you need to add the complete definition of all functions of the interface, here you are forgetting the arguments for the last two function
public function retrieveByToken($identifier, $token)
{
}
public function updateRememberToken($user, $token)
{
}
I have entity and proper mapping with them. PFB
/**
* User
*
* #ORM\Entity
* #ORM\Table(name="user")
*/
class User{
/**
* #ORM\OneToOne(targetEntity="Language", mappedBy="languageCode2")
**/
private $languageObj;
function __construct(){
$this->languageObj = new ArrayCollection();
}
}
/**
* Language
*
* #ORM\Entity
* #ORM\Table(name="language")
*/
class Language
{
/**
* #ORM\OneToOne(targetEntity="User", inversedBy="languageObj")
* #ORM\JoinColumn(name="language_id", referencedColumnName="language_id")
*/
private $languageCode2;
public function __construct()
{
$this->languageCode2 = new ArrayCollection();
}
}
when I print it It gives below
[languageObj:User:private] => Common\User\Entity\Language Object
(
[languageId:Language:private] => 1
[languageCode:Language:private] => en
[languageName:Language:private] => English
[languageCode2:Language:private] => User Object
what issue I am facing is, i am not able to fetch languageCode from Language object
To do that I have written below in __construct() of User class
$this->lc = $this->languageObj->first();
AND
$this->lc = $this->languageObj->getCode();
getCode() is method in Language class, which return $this->languageCode variable