Overriding ControllerTask using plugin - php

I would like to produce an extra view file, inside the model's template folder when I bake a controller. I've copied (unchanged yet) ControllerTask.php into plugins/WetKit/src/Shell/Task in my plugin.
For instance, for the model Entries, I would like a new file to appear during baking: src/Templates/Entries/my_custom_file.ctp.
However, when I run cake bake controller entries -t WetKit, I get:
$ bin/cake bake controller entries -t WetKit
PHP Fatal error: Cannot declare class Bake\Shell\Task\ControllerTask, because the name is already in use in C:\Users\me\Downloads\xampp3\xampp\htdocs\twofk\plugins\WetKit\src\Shell\Task\ControllerTask.php on line 28
What is the best way to accomplish this?
Edit 1: Would this be a namespace issue?
Edit 2:
Edits made to the WetKitControllerTask.php, kept as-is, but added "Yo!" in the output of the sprintf function:
namespace Wetkit\Bake\Shell\Task;
use Cake\Console\Shell;
use Cake\Core\Configure;
use Cake\ORM\TableRegistry;
use Bake\Shell\Task\ControllerTask;
/**
* Task class for creating and updating controller files.
*
* #property \Bake\Shell\Task\ModelTask $Model
* #property \Bake\Shell\Task\BakeTemplateTask $BakeTemplate
* #property \Bake\Shell\Task\TestTask $Test
*/
class WetKitControllerTask extends ControllerTask
...
public function bake($controllerName)
{
$this->out("\n" . sprintf('Yo! Baking controller class for %s...', $controllerName), 1, Shell::QUIET);
When I run the bake command, I don't seem to be getting "Yo!" to appear:
$ bin/cake bake controller entries -t WetKit
Baking controller class for Entries...
Edit 3
I get an error:
file: plugins/WetKit/src/Shell/Task/WetKitControllerTask.php
class name: ControllerTask
result: Cannot declare class Wetkit\Bake\Shell\Task\ControllerTask because the name is already in use
I get an error:
file: plugins/WetKit/src/Shell/Task/ControllerTask.php
class name: ControllerTask
result: Cannot declare class Wetkit\Bake\Shell\Task\ControllerTask because the name is already in use
No error, but doesn't output desired line:
file: plugins/WetKit/src/Shell/Task/ControllerTask.php
class name: WetKitControllerTask
result: no issues, but also not displaying added output command
Command
public function bake($controllerName)
{
$this->out("\n" . 'Override damn you!');
Under the Include tab in DebugKit, I see that my WetKit plugin is loaded.
To confirm, the override should happen in plugins/WetKit/src/Shell/Task/ of the plugin?
Edit 4
The namespace issue was my fault. I introduced this while trying to override the task.
In terms of autoloader, in composer.json:
"autoload": {
"psr-4": {
"WetKit\\": "src/"
}
},
With:
Namespace: WetKit\Shell\Task
Filename: ControllerTask.php
Class name: WetKitControllerTask
I decided to refresh the autoload files:
$ composer dump-autoload -o
Generating optimized autoload files
Class WetKit\Shell\Task\WetKitControllerTask located in C:/Users/me/Downloads/xampp3/xampp/htdocs/twofk/plugins/WetKit/src\Shell\Task\ControllerTask.php does not comply with psr-4 autoloading standard. Skipping.
Generated optimized autoload files containing 3261 classes
If I change the filename to match the classname, composer will load the class and not output the PSR-4 issue. The baking still doesn't work, I don't see the class changes.
I tried to bake with the filename being ControllerTask.php and class name WetKitControllerTask. Composer would have skipped the file, but baking still doesn't show my class modifications. Strange.
Edit 5
Noting a change to targeting TemplateTask.php instead of ControllerTask.php. What I need to do fits better in TemplateTask.php.
With the class and filename matching, I cannot bake as I get the error shown below (which also happened with ControllerTask.php):
Edit 6
Solution:
...
use Bake\Shell\Task\TemplateTask as WetKitTemplateTask;
...
class TemplateTask extends WetKitTemplateTask

I just had to change the namespace and class definition as such and place the file under plugin/WetKit/src/Shell/Task.
...
use Bake\Shell\Task\TemplateTask as WetKitTemplateTask;
...
class TemplateTask extends WetKitTemplateTask

Related

Trait not found error magento2

I am installing a module, where I am getting the error when
running
php bin/magento setup:di:compile.
The error is like:
Fatal error: Trait 'Niks\LayeredNavigation\Model\Layer\Filter\SliderTrait'
not found in /var/www/clients/client2/web202/web/app/code/NIKS
/LayeredNavigation/Model/Layer/Filter/Decimal.php on line 10
When I check the Decimal.php the code is like
<?php
namespace Niks\LayeredNavigation\Model\Layer\Filter;
use Magento\CatalogSearch\Model\Layer\Filter\Decimal as CoreDecimal;
/**
* Layer attribute filter
*/
class Decimal extends CoreDecimal
{
use SliderTrait; //This is relevant line 10 referred in code
...
and in the file SliderTrait.php it is like:
<?php
namespace Niks\LayeredNavigation\Model\Layer\Filter;
use Magento\Framework\App\ObjectManager;
use Magento\Store\Model\StoreManagerInterface;
trait SliderTrait
{
/** #var \Niks\LayeredNavigation\Model\ResourceModel\Fulltext\Collection|null */
protected $_skipFilterCollection;
...
so to me the code looks okay and am not able to find any issue, why this error is generated.
I can see that your vendor folder name is "NIKS", compared to the vendor namespace "Niks". It seems like Magento 2 folder structure is case sensitive inside /app/code for autoloading classes. Either change the namespace to "NIKS", or the folder name to "Niks". That should solve it!

Custom class not found in a Laravel app

I've had to try and work with an older package meant for php into my Laravel project.
I've added two custom classes, both are in the same folder "Classes" under the main "app" folder in my Laravel project.
One of these classes is recognized from a generated controller for my Laravel project. My paymentsController has use App\Classes\Quickbooks_Payments; in the top.
However, going to that Classes' file, I hit the following error through a route leading to my controller:
Class 'App\Classes\Quickbooks_Loader' not found
Now this is where this above is referenced in my paymentsController file:
<?php
namespace App\Classes\Quickbooks_Payments;
use App\Classes\Quickbooks_Loader;
/**
* QuickBooks Payments class
*
*/
/**
* Utilities class (for masking and some other misc things)
*/
QuickBooks_Loader::load('/QuickBooks/Utilities.php');
This last line is where the above error is referenced. And I do have both the Quickbooks_Loader.php and the Quickbooks_Payments.php in the same folder. My Quickbooks_Loader.php file starts off as such:
<?php
namespace App\Classes\QuickBooks_Loader;
So I know this is likely because of my inexperience with custom/imported classes. What am I doing wrong and how should I properly "import" these custom classes and have them recognized without any issues?
Change namespace in both classes you've shown to:
namespace App\Classes;
And run composer du
Also, make sure the class looks like this and not like you've shown (I'm not sure about did you cut something or not):
<?php
namespace App\Classes;
use App\Classes\Quickbooks_Loader;
class Quickbooks_Payments
{
public function __construct()
{
dd('It\'s working!');
}
}

Attempted to load class "ClassName" from namespace (...). Even though namespace is imported

I have a Symfony project to which I added some non-symfony php files containing various classes. But for some reason the classes are not loaded when loading the website, even though the IDE sees them properly.
So, I have a class that needs other classes:
namespace rootspace\FrontBundle\Controller;
use rootspace\FrontBundle\Networks\TwitterOAuth;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
class TwitterController extends Controller
{
public function connectAction(){
// The TwitterOAuth instance
$connection = new TwitterOAuth('abc', '123');
}
}
And then the class which fails to load (that needs yet another file)
namespace rootspace\FrontBundle\Networks;
/* Load OAuth lib. You can find it at http://oauth.net */
//require_once('OAuth.php'); -- should this be commented out?
/**
* Twitter OAuth class
*/
class TwitterOAuth {
/* Contains the last HTTP status code returned. */
}
Lastly, the third file
namespace rootspace\FrontBundle\Networks;
use Symfony\Component\Config\Definition\Exception\Exception;
class OAuthConsumer
{
public $key;
public $secret;
}
(...)
I assume the actual filenames don't matter, right? Nor their structure? PhpStorm sees all the classes properly, I can right-click through them, but it fails when deployed.
Thanks for help
Edit - the whole error message says
Attempted to load class "TwitterOAuth" from namespace "rootspace\FrontBundle\Networks" in D:\Dropbox\project\src\rootspace\FrontBundle\Controller\TwitterController.php line 15. Do you need to "use" it from another namespace?
This is because Symfony's autoloader follows PSR standards (PSR-0, PSR-4) which says that fully qualified (with namespace) class name translates to file location and name. So in fact file names does matter.
So in your case rootspace\FrontBundle\Networks\TwitterOAuth class should be located in rootspace/FrontBundle/Networks directory in file called TwitterOAuth.php
If classes you are using does not follow PSR standards you can also register them manually in app/autoloader.php file
Check these for more info:
How can I add a namespace to Symfony 2.1?:
How to autoload class
And check this answer
I forgot to add a .php extension to my filename

PHP laravel class not found. How can I use namespaces to fix this?

I'm using the Laravel framework and have a directory of the following structure:
models
Statistic.php
presenters
StatisticPresenter.php
In my models/Statistic class, I'm trying to access a present method which calls a protected property which then references my presenter:
protected $presenter = 'presenters\StatisticPresenter';
public function present() {
return new $this->presenter($this);
}
Yet, this returns the error: Class 'presenters\StatisticPresenter' not found. I realize this is a namespacing error of some sort, and I've tried watching a few videos on how it works, but I simply can't wrap my head around it. I have already added my presenter folder to my composer.json file. For example, adding this to the top of my Statistic model does not work:
use presenters\StatisticPresenter;
How can I fix this?
Do the followings;
Mark your namespace in StatisticPresenter.php ? (at the top of file "namespace presenters;")
Add PSR-4 class map to your composer
{
"autoload": {
"psr-4": {
"presenters\\": "app/presenters/"
}
}
}
run "composer dump-autoload" once and you wont need to run this command again for the "presenters" namespace if you add new classes into "app/presenters/ folder"
Test your class with "use presenters/StatisticPresenter;"
If you can access your class you dont need to change your code your present() function will be valid

PHP Zend Framework: How do I make ZF Tool work with extended Controller_Actions?

I'm working with a Zend Framework project and using the ZF tool from the command line. After setting up some initial structure, I extended the Zend_Controller_Action class with something like MySite_Controller_Action and made the existing controllers point to that. So now I have something like:
class IndexController extends MySite_Controller_Action
{ ... }
and
abstract class MySite_Controller_Action extends Zend_Controller_Action
{ ... }
The problem is that now when I attempt to run a command like
Bash$ zf create action edit Index
I get an error like this:
Creating an action named edit inside controller at /Library/WebServer/Documents/MySite/application/controllers/IndexController.php
PHP Fatal error: Class 'MySite_Controller_Action' not found in /Library/WebServer/Documents/MySite/application/controllers/IndexController.php on line 3
Fatal error: Class 'MySite_Controller_Action' not found in /Library/WebServer/Documents/OurMods/application/controllers/IndexController.php on line 3
Can anyone offer up any ideas? I've done a little searching, but I don't even know where to start on this one.
NOTE: I HAVE loaded a 'MySite' namespace via the application.ini file as follows:
autoloadernamespaces.MySite = "MySite_"

Categories