Drupal 8 class not found - drupal

I know this is no doubt a simple answer, but I just cannot seem to find the answer. I'm following a course online and this is the custom Controller:
<?php
namespace Drupal\dino_roar\Controller;
use Drupal\dino_roar\Jurrasic\RoarGenerator;
use Symfony\Component\HttpFoundation\Response;
class RoarController
{
public function roar($count)
{
$test = new RoarGenerator();
return new Response('hello');
}
}
This is the object:
<?php
namespace Drupal\dino_roar\Jurrasic;
class RoarGenerator
{
public function getRoar($length)
{
return 'R' . str_repeat('O', $length) . 'AAARRR';
}
}
But, I keep getting this:
Fatal error: Class 'Drupal\dino_roar\Jurrasic\RoarGenerator' not found
Does anybody have any ideas?
Also, I know Jurassic is spelt wrong :S

Related

Symfony6 EasyAdmin4 406 (Not Acceptable)

I recently put my project into production. And since then I can no longer create, edit and delete. I am automatically redirected to my dashboard and in my console log I have a 406 error (Not Acceptable)
Especially since I had no problem when I was local.
Thank you in advance for your help
I am using EasyAdmin version 4.4 and Symfony 6.0
<?php
namespace App\Controller\Admin;
use App\Entity\User;
use App\Entity\Skills;
use App\Entity\Project;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use EasyCorp\Bundle\EasyAdminBundle\Config\MenuItem;
use EasyCorp\Bundle\EasyAdminBundle\Config\Dashboard;
use EasyCorp\Bundle\EasyAdminBundle\Router\AdminUrlGenerator;
use EasyCorp\Bundle\EasyAdminBundle\Provider\AdminContextProvider;
use EasyCorp\Bundle\EasyAdminBundle\Controller\AbstractDashboardController;
class DashboardController extends AbstractDashboardController
{
private $adminContextProvider;
public function __construct(AdminContextProvider $adminContextProvider)
{
$this->adminContextProvider = $adminContextProvider;
}
#[Route('/admin', name: 'admin')]
public function index(): Response
{
return $this->render('admin/dashboard.html.twig');
}
public function configureDashboard(): Dashboard
{
return Dashboard::new()
->setTitle('My Project');
}
public function configureMenuItems(): iterable
{
yield MenuItem::linktoRoute('Back to the website', 'fa-solid fa-rotate-left', 'app_home');
yield MenuItem::linkToDashboard('Dashboard', 'fa fa-home');
yield MenuItem::linkToCrud('Profil', 'fas fa-user', User::class);
yield MenuItem::linkToCrud('Projects', 'fas fa-list', Project::class);
yield MenuItem::linkToCrud('Skills', 'fa-solid fa-gears', Skills::class);
}
}

PhpUnit does not identify TypeError with string numbers and Abstract Class

With the following class
declare(strict_types=1);
abstract class IntValueObject
{
public function __construct(protected int $value)
{
}
}
and the test
declare(strict_types=1);
final class IntValueObjectTest extends TestCase
{
public function testWithNotValidValue(): void
{
$value = '1';
$this->expectException(\TypeError::class);
$this->getMockForAbstractClass(IntValueObject::class, [$value]);
}
}
return
Api\Tests\Shared\Domain\ValueObject\IntValueObjectTest::testWithNotValidValue
Failed asserting that exception of type "TypeError" is thrown.
If I change $value from '1' to 'foo' if it passes the test.
We use PHP 8, and in production, if the value '1' is passed it would give TypeError, why doesn't this happen in the test?
Thanks in advance.
ORIGIN OF THE "PROBLEM"
https://bugs.php.net/bug.php?id=81258
POSSIBLE SOLUTION
declare(strict_types=1);
final class IntValueObjectTest extends TestCase
{
public function testWithNotValidValue(): void
{
$value = '1';
$this->expectException(\TypeError::class);
new class($value) extends IntValueObject {};
}
}
One explanation I can imagine is that during the test, IntValueObject::__construct('1') is called from code that is not using declare(strict_types=1); and therefore the string '1' is being coerced to integer 1. No TypeError is thrown in that case (but it would for string 'foo' - as you describe the behaviour in your question).
The Cause
The cause is using a generated mock:
<?php declare (strict_types = 1);
...
$this->expectException(\TypeError::class);
$this->getMockForAbstractClass(IntValueObject::class, [$value]);
...
To not have a TypeError in this situation is likely unexpected to you as you have scalar strict types but still see the type-coercion of the string '1' to integer 1 for the constructors' first parameter.
The Mismatch
However the TypeError is only thrown when the code calling IntValueObject::__construct(int $value) has declare(strict_types=1).
While the test-code has declare(strict_types=1) it must not be that code where the constructor method is called - as no TypeError is thrown.
For Real
Behind the scenes $this->getMockForAbstractClass(...); uses an Instantiator from the Doctrine project which is making use of PHP reflection (meta-programming). As those methods are all internal code, declare(strict_types=1) is not effective and there is no TypeError anymore.
Compare with the following code-example:
<?php declare(strict_types=1);
class Foo {
public function __construct(int $integer) {
$this->integer = $integer;
}
}
try {
$foo = new Foo('1');
} catch (TypeError $e) {
} finally {
assert(isset($e), 'TypeError was thrown');
assert(!isset($foo), '$foo is unset');
}
$foo = (new ReflectionClass(Foo::class))->newInstance('1');
var_dump($foo);
When executed with assertions enabled, the output is the following:
object(Foo)#3 (1) {
["integer"]=>
int(1)
}
Within the try-block, the TypeError is thrown with new as you expect it.
But afterwards when instantiating with PHP reflection it is not.
(see as well https://3v4l.org/aZTJl)
The Remedy
Add a class to your test-suite that is really mocking the abstract base class and place it next to the test of it so you can easily use it:
<?hpp declare(strict_types=1);
class IntValueObjectMock extends IntValueObject
{...}
Then use IntValueObjectMock in your test:
$value = '1';
$this->expectException(\TypeError::class);
new IntValueObjectMock($value);
Alternatively use anonymous class when extending it is straight forward:
$value = '1';
$this->expectException(\TypeError::class);
new class($value) extends IntValueObject {};
Or apply type-checks on the constructor method your own, either with PHP reflection or run static code-analysis which has the benefit that it can detect such issues already without instantiating - so no additional test-code is involved.

Symfony 2.4.1 ClassNotFoundException

Hello I dont know how to fix this bug , I searched in google but I got no solution for this bug, i am just trying to follow this tutorial http://tutorial.symblog.co.uk/docs/validators-and-forms.html, any help will be appreciated
ClassNotFoundException: Attempted to load class "Enquiry" from namespace
"Blogger\BlogBundle\Entity" in
C:\wamp\www\symblog.dev\src\Blogger\BlogBundle\Controller\PageController.php line 22.
Do you need to "use" it from another namespace?
this is the file code:
<?php
// src/Blogger/BlogBundle/Controller/PageController.php
namespace Blogger\BlogBundle\Controller;
// Import new namespaces
use Blogger\BlogBundle\Entity\Enquiry;
use Blogger\BlogBundle\Form\EnquiryType;
class PageController extends Controller
{
public function indexAction()
{
return $this->render('BloggerBlogBundle:Page:index.html.twig');
}
public function aboutAction()
{
return $this->render('BloggerBlogBundle:Page:about.html.twig');
}
public function contactAction()
{
$enquiry = new Enquiry();
$form = $this->createForm(new EnquiryType(), $enquiry);
$request = $this->getRequest();
if ($request->getMethod() == 'POST') {
$form->bindRequest($request);
if ($form->isValid()) {
// Perform some action, such as sending an email
// Redirect - This is important to prevent users re-posting
// the form if they refresh the page
return $this->redirect($this->generateUrl('BloggerBlogBundle_contact'));
}
}
return $this->render('BloggerBlogBundle:Page:contact.html.twig', array('form' => $form->createView()
));
}
}
Make sure:
1 the name of your file is Enquiry.php
2 it is in the folder Blogger/BlogBundle/Entity
3 it is called class Enquiry
4 the namespace is namespace Blogger\BlogBundle\Entity;
Note: The reason is that this is very often due to a typo in either the name of the file, the name of the class, the use statement or the namespace.
I solved it starting PageController.php like this:
<?php
// src/Blogger/BlogBundle/Controller/PageController.php
namespace Blogger\BlogBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Blogger\BlogBundle\Entity\Enquiry;
use Blogger\BlogBundle\Form\EnquiryType;
If you find a better solution, please let me know
If you are sure that your namespaces are all right, check if your PHP file really has .php extension.

How to include Facebook-SDK

I'm using an example of FOSUserBundle with FOSFacebookBundle. Hereon i have build my application.
The relevant Project Structure is like following:
src\ABC\MainBundle\
src\ABC\UserBundle\
src\ABC\MainBundle\Controller\DefaultController.php
src\ABC\UserBundle\Security\User\Provider\FacebookProvider.php
vendor\facebook\php-sdk\src\base_facebook.php
Part of the FacebookProvider:
use \BaseFacebook;
use \FacebookApiException;
class FacebookProvider implements UserProviderInterface
{
protected $facebook;
public function __construct(BaseFacebook $facebook, $userManager, $validator)
{
$this->facebook = $facebook;
}
public function loadUserByUsername($username)
{
try {
$fbdata = $this->facebook->api('/me');
...
As you can see there is the Facebook-Object already available.
What i want to do now is nearly the same, but in my DefaultController:
use \BaseFacebook;
use \FacebookApiException;
class DefaultController extends BaseController
{
public function indexAction(){
$facebook = new Facebook('key', 'secret');
$fbfriends_obj = $facebook->api('/'.$fbid.'/friends');
...
But there i get the message
Fatal error: Class 'ABC\MainBundle\Controller\Facebook' not found in C:\xampp\htdocs\...\src\ABC\MainBundle\Controller\DefaultController.php on line x
Why is that? How can i access the facebook-class from inside my defaultcontroller? If its already possible for the facebookprovider, why it aint possible for my controller?
any hints will be really appreciated!
The solution to that problem is, that the facebook-class has no namespace and you have to do something like
$facebook = new \Facebook(...)
Problem is here:
use \BaseFacebook;
use \FacebookApiException;
You are importing BaseFacebook class from namespace you should use \Facebook (in Controller and FacebookProvider classes)

Self referencing in a WordPress plugin class

Following the advice in this posting: php class as a plugin in wordpress
I've created a helper class for use with other plugins. In the class file, I have a declaration for activating the class, like:
function test_init() {
$test = new Test();
} // End of test_init()
I'm able to access the functions in this class by doing something like:
Test::my_function();
However, I'm having issues referring to functions within this class from each other. For example:
function my_function() {
Test::other_func();
}
In a case like this, I get the error message: "Function name must be a string"
I've tried $this->other_func, which returns the error: "there is not function "other_func" in the Class_Using_The_Test_Class.
I've tried self::other_func, which return the error: "Function name must be a string"
I tried using call_user_func() and I get: "call_user_func() expects parameter 1 to be a valid callback"
How do I call another function within this class?
You don't actually need to activate the class. I'll give an example.
Let's say this code lives in helper-class.php:
<?php
class Helper_Class {
// Note: those are double underscores before the word 'construct'.
function __construct() {
// initialize/call things here.
$this->init(); // this is how you call class functions.
}
function init() {
// do some monkey-business
return;
}
// we'll call this function from our other class.
function some_function() {
// do the fancy whizbang.
}
}
?>
Now, over in your other class file you could have something like this:
<?php
// give ourselves access to the helper class.
require_once 'helper-class.php';
class Main_Class {
// Note: those are double underscores before the word 'construct'.
function __construct() {
$this->init();
}
function init() {
// classes can't be used until an object of that class is created.
$helper_class_object = new Helper_Class;
// now I can call functions in my helper class.
$helper_class_object->some_function();
return;
}
}
?>
I hope this sheds a bit of light on your situation. Just ask if you'd like further clarification. :)

Resources