Extending Laravel 5.1 Mess Detection the right way - oop

Laravel 5.1 has only one built-in hardcoded Mess Detection, it searches to see if #package tag is the same with /namespace, like so:
preg_match('/namespace\ ' . self::REQUIRED_NAMESPACE . '\\\(.*);/isU', $content, $namespace);
preg_match('/\/*( )+#package\ (.*)[\r?\n]/isU', $content, $package);
if (!isset($namespace[1]) || empty($namespace[1])) {
continue;
}
I want to extend this to add multiple detection classes.
My folder structure currently looks like this:
Helpers >
Commands >
MessDetector >
Detector >
DetectorInterface.php
DetectorAbstract.php
PackageTagDetector.php
MessDetector.php
The file MessDetector is made as an Artisan Command, that means it extends \Illuminate\Console\Command
I have a config file called mess_detection.php which has the detection rules like so:
return [
'rules' => [
'\Helpers\Commands\MessDetector\PackageTagDetector' =>
[
'rule' => '/\/*( )+#package\ (.*)[\r?\n]/isU',
'path' => app_path(),
'info' => 'Checking for #package tag',
'error'=> 'Malformed or missing package tag'
]
]
];
But got stuck with figuring out how exactly to instantiate PackageTagDetector class in MessDetector class.
Any ideas design pattern wise?

Laravel has no built in mess detector.
Also checking if package tags matches the namespace is a custom convention, Laravel doesn't use package tags at all.
It seems like you are using a third party library?

I have made an abstract class and interfaces for every rule I needed.
Each rule had it's own class.
The rules extended the abstract and in the abstract most of the logic was used.
The rules themselves where stored in a config file, in the config file I also mentioned the class and class specific actions.

Related

how to using plugin with inherited auth as child of other plugin

for my application i want to write an "admin" plugin, named "db" with own auth-component. To manage acl i tried to implement ivanamat/cakephp3-aclmanager as part of my admin tool, because my app won't need user authentification, maybe later.
i load AclManager in plugins/db/Plugin.php
public function bootstrap(PluginApplicationInterface $app)
{
Configure::write('AclManager.aros', array('DbGroups', 'DbRoles', 'DbUsers'));
parent::bootstrap($app);
$app->addPlugin('AclManager', ['routes' => false]);
}
I set up Routing Rules in db/config/routes.php
Router::connect(
'db/acl',
['plugin' => 'AclManager', 'controller' => 'Acl', 'action' => 'index']
);
Router::connect(
'db/acl/:action/*',
['plugin' => 'AclManager', 'controller' => 'Acl']
);
And i set up Auth Configuration in plugins/db/Controller/AppController.php
It's loaded and aside a "Auth not found"-error it works, but this is my problem.
When i debug $this->Auth in app.local/db/ it's filled. But not when i debug in app.local/db/acl. even when i load this plugin in my plugin, it's using Base AppController, not Plugin AppController.
Is it possible to inherit auth from plugin to plugin or load plugins with parent-child-association?
You cannot change what a (controller) class inherits, that is compile time information, and once it's processed, it is what it is, the controller classes of the AclManager plugin do extend App\AppController.
There's lots of different ways to solve your problem, one would for example be to dynamically configure the authentication in your application's base App\AppController class, rather than in your Db plugin's Db\AppController class.
You can check the routing information whether it's a request for the Db plugin or the AclManager plugin, and only load the auth component in that case, something like this:
public function initialize()
{
if (in_array($this->request->getParam('plugin'), ['Db', 'AclManager'], true)) {
$this->loadComponent('Auth', [
// ...
]);
}
}
That way the Db and AclManager plugin controller classes will inherit the authenticaton setup while your main application and possible other plugins are being excluded.
ps. if you're just starting to implement authentication, then I'd strongly suggest that you try the new authentication and authorization plugins, they're much more versatile than the (deprecated) auth component!

Yii2 config params vs class properties

What is better to use:
params in config
'modules' => [
'admin' => [
'class' => 'app\modules\admin\Admin',
'defaultRoute' => 'question',
'layout' => 'main.php',
], ],
or properties in class
namespace app\modules\admin;
class Admin extends \yii\base\Module
{
public $class = 'app\modules\admin\Admin';
public $defaultRoute ='question';
public $layout = 'main.php';
...
What is the best practise?
For me the best pratice is the firts one.
Is the pratice suggested by Yii2 framework, and It allows you to centralize all major aspects of recurring costs related to the configuration, leaving the class aspects of its behavior are not relevant to the configfurazione framework. In one fell swoop in the first solution all the configuration of the framework is visible.
It is also active from the start without having to invoke the class
Configuration is only for the configuration files. The reason is that you put it in one centralized place. The configuration is used by the whole application and if you change it, you don't change the code in your application.

using multiple translation files in aurelia i18N

I have a working app using aurelia-i18n. I would like to split translation.json file into multiple files like nav.json, message.json, etc but I am not sure how to do it.
This is how it looks right now.
locale
|-en
|- translation.json
But I want to change it to this way.
locale
|-en
|- nav.json
|- message.json
Is it possible to do it? If so, how do I configure it and access values in each file?
You can have multiple resource files and these are called namespaces in the i18next library (by default you only have one namespace which is called: translation) which is used by aurelia i18N.
You just need to list your namespaces when configuring the plugin with the namespaces and defaultNs properties inside the ns option:
.plugin('aurelia-i18n', (instance) => {
// adapt options to your needs (see http://i18next.com/pages/doc_init.html)
instance.setup({
resGetPath : 'locale/__lng__/__ns__.json',
lng : 'de',
attributes : ['t','i18n'],
ns: {
namespaces: ['nav', 'message'],
defaultNs: 'message'
},
getAsync : true,
sendMissing : false,
fallbackLng : 'en',
debug : false
});
});
See also the documentation of i18next and this related github issue: Using namespaces

Prevent Yii from loading JS out of assets

Is there a way to configure Yii such that it will no longer load any Javascript out of the Assets folder?
Make your own AssetManager or extend current
protected/components/DummyAssetManager.php:
class DummyAssetManager extends CApplicationComponent {
public function publish(){}
}
add into components array in
protected/config/main.php:
'assetManager'=>array(
'class'=>'DummyAssetManager',
),
You should consult the manual for a detailed description of
the assetManager options
I think you can try following option in your config/main.php
'components' => array(
'assetManager' => array(
'linkAssets' => true,
),
),
This will make asset files symbolic links to your original js/css sources. See linkAssets for more details on it.
If your PHP<5.3, or the OS it's running on doesn't support symbolic links, you won't be able to use 'linkAssets' option, in this case you can try:
'components' => array(
'assetManager' => array(
'forceCopy' => true,
),
),
This should update asset folder on every request. These two options are usually used during development process (btw, you can't use both) and should be removed from production.
PS: if you're sure that you haven't explicitly enabled ckeditor somewhere in your code and you're confident about your assetmanager calls throughout the code, check your layout and page for widgets that require this CKeditor, as Yii can't preload 'stuff' just randomly, it can be triggered by some preloaded component/extension or yii widget.

What is the correct place to set the Default Namespace for a Multiple Module project created with Phalcon DevTools?

I just started looking about Phalcon this week, I'm trying to create a multiple module application with the dev tools.
The result of running phalcon project <name> multiple only creates one module ('frontend') and it works fine. However, when I add a second module (by copying the frontend one and changing the namespace to \Backend , I couldn't get to the Backend\IndexController class.
After reading the doc page about mutiple module applications and looking at the samples (https://github.com/phalcon/mvc/tree/master/multiple and https://github.com/phalcon/mvc/tree/master/multiple-volt) and an old question on the Google group (sorry, can't post more than 2 links since I'm new on StackOverflow), I've ended commenting this this line on the services.php file:
$router->setDefaultNamespace("MyL\Frontend\Controllers"); //project name is MyL
and adding the following on the setServices of my backend/Module.php file:
$di->set('dispatcher', function() {
$dispatcher = new Dispatcher();
$dispatcher->setDefaultNamespace("MyL\Backend\Controllers");
return $dispatcher;
});
and the something similar on the frontend/Module.php
It works with these modifications, but my question is: is this the best way to do it, or is there a better way?
Thanks in advance!
You need to register your modules in your app like so:
$app = new \Phalcon\Mvc\Application();
$app->registerModules(
[
'frontend' => [
'className' => 'MyL\Frontend\Controllers',
'path' => '../apps/frontend/Module.php'
],
'backend' => [
'className' => 'MyL\Backend\Controllers',
'path' => '../apps/backend/Module.php'
],
]
);
Make sure that you have the Module.php ready also for each module
Here is a simple way to split frontend and backend in Phalcon project without modules:
https://github.com/borzov/phalcon-templates