I'm starting to work with phpspec and I'm struggling with this problem. I'm having a spec code like this :
class OrderItemSpec extends ObjectBehavior
{
function it_is_initializable()
{
$this->shouldHaveType('Ts\Project\Model\OrderItem');
}
function it_is_a_model()
{
$this->shouldImplement('Ts\Generic\Model');
}
}
Model interface class:
namespace Ts\Generic;
interface Model
{
}
When running phpspec it always ask me:
Do you want me to create `Ts\Generic\Model` for you?
File "Model.php" already exists. Overwrite?
When overwriting, he changes interface to a regular class.
In composer I have autoload configured like this :
"autoload": {
"psr-0": {
"": "src"
}
}
Is it a phpspec error, limitation or am I doing it in a wrong way ?
Related
I'm facing a problem with my slim(http://www.slimframework.com/) application. When I'm trying to load my Database class using namespacing I get this error:
Message: Class 'Craft\Code\CraftDB\Database' not found
I have Database.php in folder app/config/Database.php
My Database class start like this
namespace Craft\Code\CraftDB;
class Database {
I'm trying to use it like this in another file:
use Craft\Code\CraftDB as DB;
class MyOtherClass {
protected $connectDb;
protected $db;
public function __construct() {
$this->connectDb = new DB\Database;
$this->db = $connectDb->connect();
}
My Composer file is :
"autoload": {
"psr-4": {
"Craft\\Code\\": "app/"
}
}
I'm trying to find the problem but I'm lost now. Please help. Thanks
Where you say your file is: app/config/Database.php
Where you tell Composer your file is: app/CraftDB/Database.php
Things simply don't seem to match here. On file system side you have that config level unaccounted for, on namespace side CraftDB level in namespace.
From your description I imagine you need something like:
"autoload": {
"psr-4": {
"Craft\\Code\\CraftDB\\": "app/config/"
}
}
I'm setting up a factory to load my classes and their methods for use across my application. Probably continuing on with the strategy method. I feel as though I'm missing a step with my factory, as it throws an exception to finding the class through class_exists(), meanwhile it loads correctly if I omit the check. So, the question is - how do I load my custom factory and classes in laravel appropriately?
Interestingly enough, it doesn't throw an error inside my tests right now.
Only inside my application itself.
I'm newish to the implementation process so I suspect I'm not loading my custom classes appropriately or have a namespacing problem, or maybe should be loading this up in a service provider? Ok that's two questions really. Here's the code:
Factory
namespace app\Support\Humans;
class PeopleFactory
{
public static function build($person)
{
//adding this made it work for my test
$person = __NAMESPACE__ . "\\" . $person;
if (class_exists($person)) {
return new $person();
} else {
//person doesn't exist
throw new Exception;
}
}
}
Person Type
namespace app\Support\Humans;
class PersonType extends BasePerson
{
public function joinHumanity()
{
//etc
}
}
As I'm suspecting a namespacing issue, a segment of my composer.json pointing to the directory of my custom classes.
"autoload": {
"classmap": [
"app/Humans/"
],
"psr-4": {
"Humanity\\": "app/"
}
},
Implementation both in testing or elsewhere
use app\Support\Humans\PeopleFactory;
....
$new_human = PeopleFactory::build($this->user->personType);
$new_human->joinHumanity();
Just started working through laracasts and trying to move on from direct eloquent use in the controllers.
I have implemented everything that I need to but hitting this error:
Class tva\Repositories\VehicleRepositoryInterface does not exist
My folder structure is:
app/
tva/
repositories/
VehiclesController:
use tva\Repositories\VehicleRepositoryInterface;
class VehiclesController extends \BaseController {
protected $vehicle;
public function __construct(VehicleRepositoryInterface $vehicle)
{
$this->vehicle = $vehicle;
}
}
In the repositories folder:
VehicleRepository:
namespace tva\Repositories;
class VehicleRepository implements VehicleRepositoryInterface {
}
VehicleRepositoryInterface:
namespace tva\Repositories;
interface VehicleRepositoryInterface {
}
And also updated my composer.json:
"psr-0": {
"tva": "app/"
},
To me, this should work?
Issue solved, instead of using psr-0 I added the directory to the classmap and all issues solved.
I've been comparing my code to this question and many other guides online but with little success. Everything works fine until I try to inject the the interface in my controller. When I do inject it, I get the Target Interface is not instantiable error.
app\models\Interfaces\InterviewInterface.php
<?php namespace app\models\Interfaces;
interface InterviewInterface {
public function fetch($id);
}
app\models\Interview.php
use app\models\Interfaces\InterviewInterface;
class Interview extends Eloquent implements InterviewInterface {
public function fetch($id)
{
return Interview::find($id);
}
}
routes.php
App::bind('app\models\Interfaces\InterviewInterface');
composer.json
"psr-4": {
"Blog\\Articles\\" : "app/lib/Blog/Articles",
"app\\models\\Interfaces\\" : "app/models/Interfaces/InterviewInterface.php"
}
AdminInterviewController.php
use app\models\Interfaces\InterviewInterface as InterviewInterface;
class AdminInterviewController extends BaseController {
public function __construct(InterviewInterface $interview)
{
$this->interview = $interview;
$this->beforeFilter('auth');
}
}
As soon as I add the
use app\models\Interfaces\InterviewInterface as InterviewInterface;
and
__construct(InterviewInterface $interview)
$this->interview = $interview;
lines, it gives me the error. I pull them out, no error.
I have run php composer.phar dump-autoload and php artisan dump-autoload commands multiple times and they succeed.
Any ideas? I really appreciate it.
You need to bind the interface to a class in order to resolve it from the IOC:
In routes.php, assuming it is not namespaced:
App::bind('app\modesl\Interfaces\InterviewInterface', 'Interview');
In my folder "/Vendor/User/Admin" I created a new custom class (Adminuser.php)
namespace \User\Admin;
class Adminuser {
public $username;
public $password;
}
Now Im trying to use it in a controller:
namespace Section\AdminBundle\Controller;
use \User\Admin;
class DefaultController extends Controller
{
public function indexAction()
{
$AdminUser = new \User\Admin\Adminuser(); // CLASS NOT FOUND!!
.......
Why is this happening?, the namespace is wrong? (I tried a few options..)
Im very begginer with Symfony, sorry.
You have 2 main issues.
The First
When declaring a namespace you should not start with a \
namespace \User\Admin;
Should just be:
namespace User\Admin;
The Second
If you want those classes to live in your Vendors Dir then you need to make sure the class is being autoloaded by symfony correctly. To do this we will use composer.
In your composer.json you will want to change this section from:
"autoload": {
"psr-0": { "": "src/" }
},
TO:
"autoload": {
"psr-0": { "": "src/" },
"psr-0": { "": "vendor/User/Admin" }
},
Then composer will add classes under that folder to the available namespaces and you will be able to access it as expected.
just remove the first "\" in the namespace, as the comments said. So the first file is:
namespace User\Admin;
class Adminuser {
public $username;
public $password;
}
if the problem persist check your autoloading configuration, maybe the right way would be using src dir to develop your code, not vendor :S