Laravel psr-0 auto-loading - laravel-4

I'm using the standard laravel directory structure that comes out the box. I've added this to the composer.json
"autoload": {
"classmap": [
"app/commands",
....
],
**"psr-0": {
"CARS": "app/"
}**
},
I've got a View
<?php namespace Views;
class MyView {
public function Build()
{
return "Great !";
}
}
?>
and a controller
class MyController extends \BaseController {
public function loadHome() {
$view = new \Views\MyView;
return $view->Build();
}
}
It says Class 'Views\XView' not found.
I've ran composer auto-load once already with the PSR-0 there but now I want to add Controllers and View without re-running the composer and I believe PSR-0 allows me to do that (If I run the composer, it works btw).

You're basically not using PSR-0, while trying to use it...
If you tell Composer you have:
"psr-0": {
"CARS": "app/"
}
It will look desperately for files in
app/CARS/*.php
And also will understand that all files on it are under the CARS namespace:
So, your class must be in:
/whatever/application/app/CARS/Views/MyView.php
And must be declared as
<?php namespace CARS\Views;
class MyView {
public function Build()
{
return "Great !";
}
}

You will need to wrap it up into CARS namespace mate

As I reviewed composer version 1.8.6 and I had the same issue I referred to Docs
Under the psr-0 key you define a mapping from namespaces to paths, relative to the package root. Note that this also supports the PEAR-style non-namespaced convention.
Note namespace declarations should end in \ to make sure the autoloader responds exactly
{
"autoload": {
"psr-0": {
"Monolog\\": "src/",
"Vendor\\Namespace\\": "src/",
"Vendor_Namespace_": "src/"
}
}
}
and here is where I was interested with, The PSR-0 style is not limited to namespace declarations only but may be specified right down to the class level. This can be useful for libraries with only one class in the global namespace. If the php source file is also located in the root of the package, for example, it may be declared like this:
{
"autoload": {
"psr-0": { "UniqueGlobalClass": "" }
}
}
Note
You need to declare a full path to it for example
the file structure is
/myProject
-/app
--/Acme
---/Repositories
----/MyClass
So my composer psr-0 will look like
"psr-0": {
"Acme": "app/Acme/"
},
Hope this help someone

Related

Laravel Pint json config not wokring

I've been playing around with Laravel Pint and I can't seem to make it work the way I want.
My goal is to have the curly brackets inline with class or function
so instead
function test()
{
}
I want to have a format like this
function test() {
}
I have this on pint.json but doesn't change anything.
{
"preset": "laravel",
"braces": {
"position_after_functions_and_oop_constructs": "same",
"position_after_anonymous_constructs": "same"
}
}
I event tried using psr12 preset and still does not change anything
{
"preset": "psr12"
}
Additionally, I'd like to know how I can allow this format
if ( !$num )
return;
it changes to this after running pint, (it removes the space between if condition and added a space after ! and wrap the state with brackets)
if (! $num) {
return;
}
The rule in a pint.json will be
{
"preset": "laravel",
"rules": {
"not_operator_with_successor_space": false,
"curly_braces_position": {
"functions_opening_brace": "same_line",
"classes_opening_brace": "same_line"
}
}
}
As per PHP-CS-Fixer Rule, use curly_braces_position
Ref: How with pint can I remove space after negative “!” symbol?

Spring #ConfigurationProperties configuration processor not generating correct metadata for Maps with enum keys and values

I need to do some lightweight attribute-based access control in a Spring Boot application.
(The background is not related to my question, but here's a brief summary: There are different types of users, different types of resources (each with attributes), and different levels of access. I need to evaluate access to resources very dynamically - hence using enums to separate permission evaluation from other logic.)
I want to store the configuration for the permissions in application.yml so I can configure it easily, and change it without making code changes.
I wanted to set this up using #ConfigurationProperties, so that the config in application.yml had autocompletion and could be softly validated. This works very nicely for other properties, but not when I start using Maps.
I can change the keys to strings, and map them in the config class (even though I lose type-completion), but the documentation in a few places indicates that that Maps should use enum, and metadata will be generated correctly - so I am confused.
I have correctly set up spring-boot-configuration-processor (it works for other properties) and it is generating the metadata JSON (see below).
MyConfig.kt
package example.myapp
import org.springframework.boot.context.properties.ConfigurationProperties
import org.springframework.boot.context.properties.ConstructorBinding
#ConstructorBinding
#ConfigurationProperties("my-app")
data class MyConfig(
val permissions: Map<Role, Map<ResourceId, Set<Permission>>>,
val owner: Map<ResourceId, Set<Permission>>
) {
enum class Role {
OWNER,
VIEWER,
}
enum class Permission {
VIEW,
EDIT,
}
enum class ResourceId {
ARTICLE_TITLE,
ARTICLE_BODY,
}
}
spring-configuration-metadata.json
{
"groups": [
{
"name": "my-app",
"type": "example.myapp.MyConfig",
"sourceType": "example.myapp.MyConfig"
}
],
"properties": [
{
"name": "my-app.owner",
"type": "java.util.Map<example.myapp.MyConfig$ResourceId,? extends java.util.Set<? extends example.myapp.MyConfig.Permission>>",
"sourceType": "example.myapp.MyConfig"
},
{
"name": "my-app.permissions",
"type": "java.util.Map<example.myapp.MyConfig$Role,? extends java.util.Map<example.myapp.MyConfig.ResourceId,? extends java.util.Set<? extends example.myapp.MyConfig.Permission>>>",
"sourceType": "example.myapp.MyConfig"
}
],
"hints": []
}
Versions
Spring Boot 2.7.1
Kotlin 1.7.10
Update
Changing Map<> to HashMap<> seems to help avoid the errors in the keys, but doesn't affect the values - I get a misleading warning: Cannot resolve configuration property
#ConstructorBinding
#ConfigurationProperties("my-app")
data class MyConfig(
val permissions: HashMap<Role, HashMap<ResourceId, HashSet<Permission>>>,
val owner: HashMap<ResourceId, HashSet<Permission>>
)

Error in mutation nuwave/lighthouse:^5.0 and input

I am using nuwave/lighthouse:^5.0, and I am trying to create a mutation for an entity which have a belongsTo relationship. The thing is that in my input I am using a sanitizer directive to transform from string to id, but after that when Laravel gets the properties, it shows errors with the validation of the class. In addition, I debug the directive code and it works correctly.
Error
"errors": [
{
"message": "The given data was invalid.",
"extensions": {
"validation": {
"content_type_id": [
"The content type id field is required."
]
},
"category": "validation"
},
Input
input CreateContentInput {
content_type: CreateContentTypeBelongsTo!
.....
input CreateContentTypeBelongsTo {
connect: ID! #typeuuid(model: "App\\ContentType")
create: CreateContentTypeInput
update: UpdateContentTypeInput
}
Model
class Content extends Model
{
protected $rules = [
'content_type_id' => 'required|integer|is_main_content_type',
];
/**
* #return BelongsTo
*/
public function contentType(): BelongsTo
{
return $this->belongsTo(ContentType::class);
}
Any idea will be appreciated
Finally, after some days, I found the issue.
The error come from the main Input definition:
input CreateContentInput {
content_type: CreateContentTypeBelongsTo!
}
I was following a company standard that says that we need to use the properties always in ** snake case ** although they are relationships. So looks like Lighthouse uses always ** camel case ** for relationships.
The solution was add the ** rename ** property to the input. So the right input should be:
input CreateContentInput {
content_type: CreateContentTypeBelongsTo! #rename (attribute: "contentType")
}
I hope this could help someone else.

How to add custom route for user

I have created a books content type containg books. Each book in the collection belongs to a user (user content type provided by Strapi).
I want to return list of books owned by authenticated user at /users/me/books endpoint. Where can I add this route and handler as there is /api/books directory containing books related route, controllers, etc. but not /api/users directory.
You can extend or override using the extensions system.
extensions/users-permissions/controllers
Just add the controller you want to extend or override as a .js file like so:
So to override the me endpoint under User.js you only need to define the method again:
'use strict';
module.exports = {
//Override me
async me(ctx) {
//do your thing
}
};
To extend, not override, means to add another endpoint, therefor you need to define it, add a route and set permissions for it. The routes.js files should be created at:
extensions/users-permissions/config/routes.json
Like so:
{
"routes": [
{
"method": "GET",
"path": "/users/me/books",
"handler": "User.getUserBooks",
"config": {
"policies": [],
"prefix": "",
"description": "description",
"tag": {
"plugin": "users-permissions",
"name": "User",
"actionType": "find"
}
}
}
}
The controller this time (same location as in beginning):
module.exports = {
async getUserBooks(ctx) {
//add logic
}
}
OP correctly added:
After adding custom route and controller, one has to go to Admin Panel(log in as admin)>Roles and Permission> Users-Permission. There you can find the newly added route and have to enable it by checking it.
The originals(if you need examples) are located at:
/node_modules/strapi-plugin-users-permissions/config/routes.json
/node_modules/strapi-plugin-users-permissions/controllers/User.js
I don't think you should extend the User controller as it isn't logically correct. You are trying to GET books - you should extend the book api in the same way.
From what I can tell a ContentType doesn't contain information about its creator(you're welcome to educate me if it's not true).
So to tackle that you can add to your ContentType "books" a relation to User.
Then I think you should extend the books api with a endpoint that returns books "belonging" to that user using the ctx received.
Also - check this question out
Comment if you need more info.

Tslint force use to access modifiers and space between function

Hello guys I want to use a mandatory space between each function declaration and also to force the developer to use access modifiers (Public, Private, Protected) each time I declare a method or variable.
**For instance:**
private my_var;
private myFunction() {}
// one space between function.
private myFunction() {}
I have this rules in my tslint.json file but it is not working.
{
"rulesDirectory": [
"node_modules/codelyzer"
],
"rules": {
"member-access": true,
"typedef-whitespace": [true,
{ "call-signature": "onespace" }
],
}
Thank you very much.

Resources