Fully qualified class names in Laravel 5.2 - namespaces

So as I'm working through various tutorials (laracast & YouTube), learning Laravel 5.2 I keep coming across this situation wherethe tutorial uses a namespace like
App\Models\CarModel.php
but it only works for me if I use
carfreak\Models\CarModel.php
It appears to be that for some reason my app demands a fully qualified class name, and the tutorials avoid it. How could I do the same?
My folder structure is as follows
myprojects
-project1
-project2
-carfreak
--app
---Console
---Events
---Exceptions
---Http
---Jobs
---Listners
---Models
----CarModel.php
---Policies
---Providers
--bootstrap
--config
--database
EDIT: My composer.json
"autoload": {
"classmap": [
"database",
"app/Models"
],
"psr-4": {
"carfreak\\": "app/"
}
},

I am sure this will solve your issue, You just need to autoload your models directory. to do this you have to write this line "app/Models" in your composer.json file under autoload -> class map array. see below.
....
"autoload": {
"classmap": [
"database",
"app/Models"
]
...
},
When you add this, then Run this command in your terminal
composer dump-autoload
After that you can call your models like this
use App\Models\Problem;

Related

JetBrains WebStorm Jest encountered an unexpected token

I have a problem with running a simple project in WebStorm IDE. This is what I get when I hit run:
Test suite failed to run
Jest encountered an unexpected token
This usually means that you are trying to import a file which Jest cannot parse, e.g. it's not plain JavaScript.
By default, if Jest sees a Babel config, it will use that to transform your files, ignoring "node_modules".
Here's what you can do:
• To have some of your "node_modules" files transformed, you can specify a custom "transformIgnorePatterns" in your config.
• If you need a custom transformation specify a "transform" option in your config.
• If you simply want to mock your non-JS modules (e.g. binary assets) you can stub them out with the "moduleNameMapper" config option.
You'll find more details and examples of these config options in the docs:
https://jestjs.io/docs/en/configuration.html
Details:
/home/patryk/WebstormProjects/Case Converter/node_modules/hs-test-web/hstest/stage/stageTest.js:12
runner = new PureJsApplicationRunner();
^
SyntaxError: Unexpected token =
at ScriptTransformer._transformAndBuildScript (node_modules/jest/node_modules/jest-runtime/build/script_transformer.js:403:17)
at Object.<anonymous> (node_modules/hs-test-web/hstest/index.js:1:110)
At this moment, my project contains just 1 html file. I tried reinstalling nodejs and npm, but that didn't work
Good time of day,
I've encoutered the same problem with one Edu project in WebStorm IDE - when i pressed the "check" button for task - i've got issue:
"test suite failed to run
Jest encountered an unexpected token ..."
How it was solved:
Please check if nodejs and npm installed - At IDE: File\Settings, section: Languages & frameworks > nodejs and npm section > at field: "Node interpreter" you'll find something like : "node /usr/bin/node" and at field: "Package manager" the value "npm /usr/share/npm"
In IDE - you need to expand "Project pane" and to choose view mode "Project Files" - then you need open the file "package.json",
initially this file contains the code:
{
"devDependencies": {
"#types/jest": "^23.3.12",
"hs-test-web": "https://github.com/hyperskill/hs-test-web/archive/release.tar.gz",
"jest": "^27.3.1",
"puppeteer": ">=8.0.0"
},
"scripts": {
"test": "jest"
}
}
You need to add the section for "jest" - please found the complete package.json below
{
"devDependencies": {
"#types/jest": "^23.3.12",
"hs-test-web": "https://github.com/hyperskill/hs-test-web/archive/release.tar.gz",
"jest": "^27.3.1",
"puppeteer": ">=8.0.0"
},
"scripts": {
"test": "jest"
},
"jest": {
"verbose": true,
"moduleFileExtensions": [
"ts",
"tsx",
"js",
"jsx",
"json"
],
"transform": {
"^.+\\.jsx?$": "babel-jest",
"^.+\\.tsx?$": "<rootDir>/node_modules/ts-jest/preprocessor.js"
},
"transformIgnorePatterns": ["/node_modules/(?!lodash-es)"],
"testRegex": "test/.*\\.spec\\.ts$"
}
}
So,i hope it help you.

How do I run a Postman collection subfolder with Newman

Postman: 5.5.0
Newman: 3.9.0
OS: Windows 10
How am I supposed to use the --folder command in Newman?
I have a Postman Collection Export (v2.1) which I am loading into Newman using:
- newman run "AME.postman_collection.json"
When I run this command, all tests execute as expected (including all sub folders.
However, I would like to run a specific subfolder called "Media". My understanding is that I should run the commandline:
- newman run "AME.postman_collection.json" --folder "Media"
Unfortunately this does not work. Newman simply reports no failure, but also 0 tests. I have also tried numerous other variations of the --folder parameter such as:
- "AME.Media"
- "AME/Media"
- "AME\Media"
- "\Media"
- "/Media"
- "Media/"
- "Media\"
- ".Media"
- And many, many more
Sample data from the Export below:
{
"info": {
"name": "AME",
"_postman_id": "023f6868-4c80-8c0f-01b6-78943caa38a6",
"description": "",
"schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json"
},
"item": [
{
"name": "Media",
"description": "",
"item": [
{
"name": "showRenderer",
"event": [
Try updgrading to v3.9.1 (December 8, 2017).
The changelog states
Fixed folder option handling
that there was in error in folder handling which is now fixed. I tried v3.9.1 with sub folder and it worked for me.

Babel transpiling es7 class decorators Unexpected token error

im working on an Aurelia app that used es6 and es7 code, im trying to transpile the code using babel. I have the following in my packages.json file
"scripts": {
"babel": "babel --stage 1 -d AureliaWeb/ ../Test/Aurelia/ --extends babelrc",
I have the following packages installed:
"devDependencies": {
"babel-core": "^6.10.4",
"babel-plugin-syntax-decorators": "^6.8.0",
"babel-plugin-syntax-flow": "^6.8.0",
"babel-plugin-transform-es2015-modules-amd": "^6.8.0",
"babel-plugin-transform-es2015-modules-commonjs": "^6.10.3",
"babel-plugin-transform-es2015-modules-systemjs": "^6.9.0",
"babel-plugin-transform-flow-strip-types": "^6.8.0",
"babel-preset-es2015": "^6.9.0",
"babel-preset-es2015-loose": "^7.0.0",
"babel-preset-react": "^6.11.1",
"babel-preset-stage-0": "^6.5.0",
"babel-preset-stage-1": "^6.5.0",
"babel-preset-stage-2": "^6.11.0",
"babel-preset-stage-3": "^6.11.0",
"babel-register": "^6.9.0",
"chai": "^3.5.0"
},
"dependencies": {
"babel-plugin-transform-class-properties": "^6.10.2",
"babel-plugin-transform-decorators-legacy": "^1.3.4",
"babel-preset-es2015-loose": "^7.0.0",
"core-js": "^2.4.0",
"lodash": "^4.13.1"
}
Im trying to transpile es7 code looking like:
import {inject, noView} from 'aurelia-framework';
import {HttpClient} from 'aurelia-http-client';
#noView() <-- THIS IS CAUSING THE ERROR!!
#inject(HttpClient)
export class CompanyDataManager {
Im getting the error Syntax error Unexpected token arount the class decorator #noView. I have checked the stage 1 preset and i know that class decorators have been removed http://babeljs.io/docs/plugins/preset-stage-1/
Due to this I have added in the legacy decorator 'babel-plugin-transform-decorators-legacy found here 'https://github.com/loganfsmyth/babel-plugin-transform-decorators-legacy
so my .babelrc (which is located at the root of my project) looks like so:
{
"presets": [ "es2015-loose", "stage-1" ],
"plugins": [
"syntax-decorators",
"syntax-flow",
"babel-plugin-transform-decorators-legacy",
"transform-class-properties",
"transform-flow-strip-types"
]
}
However im still finding no joy! Question is
How can i confirm that my babelrc file is being picked up when im executing the babel script in npm? I tried adding --extends babelrc in an attempt to force it, but im not sure if this is correct.
I have also tried specifying a plugin in the babel command like so:
"babel --plugins babel-plugin-transform-decorators-legacy --stage 1 -d AureliaWeb/ ../Test/Aurelia/"
Still no luck, any help would be greatly appreciated.
Update 1
After messing around with the .bablerc file, i think i might have placed it in the wrong location. My structure is as follows (its an mvc app).
Test.UnitTest (Project) <-- i am running the babel script from here
AureliaWeb <-- end trandpiled location
Test (Project)
Aurelia <-- contains actual Aurelia code
After having moved the .bablerc file into Test > Aurelia I started getting the following error
Unknown plugin "transform-decorators-legacy" specified in "C:\Code\src\Test\Aurelia\.babelrc" at 0, attempted to resolve relative to "C:\Code\src\Test\Aurelia"
I have tried installing the package for transform-decorators-legacy in both projects to see if it makes any difference
Update 2
I can now confirm that this was in fact the issue, it appears that the babel file needs to be placed in the source folder, not destination or the location from which babel has been executed.
I believe you just need to remove the parentheses:
#noView
#inject(HttpClient)
export class CompanyDataManager {
See, for example, point 2 under "Best Effort":
class Example {
#dec
static prop = i++;
}
______
Note: when you declare a babel plugin in your .babelrc (or package.json) you can drop the prefix babel-plugin-:
[
"syntax-decorators",
"syntax-flow",
"transform-decorators-legacy",
"transform-class-properties",
"transform-flow-strip-types"
]
______
How can i confirm that my babelrc file is being picked up when im executing the babel script
Babel looks in the directory in which it was invoked for either a .babelrc or package.json with "babel" property. So long as you have either of these and are executing Babel in the same directory, Babel will find it.
I have answered my own question, details are in the main post.
The babel file needs to be placed in the source folder, not destination or the location from which babel has been executed.

Sublime Text 2 Project file build system for js files prevents JSHint from running on save

I have setup Sublime Text 2 to run JSHint when JavaScript files are saved. The easiest way to achieve this seemed to be to use the plugin SublimeOnSaveBuild together with the JSHint plugin. And it works great.
The problem is that I now need to run an additional shell command when JavaScript files are saved. This is project specific, so I added a build system to the .sublime-project file (echo Test is just a placeholder for the real command).
"build_systems": [
{
"name": "RunOnJavaScriptSave",
"selector": "source.js",
"cmd": ["echo Test"],
"windows": {
"shell": true
}
}
]
This is now triggered when JavaScript files are saved, but it stops JSHint from running. It seems the "source.js" selector conflicts with the JSHint plugin.
I located Packages\JSHint\JSHint.sublime-build and indeed it also contains a "source.js" selector:
{
"selector": "source.js",
"cmd": ["jshint", "$file", "--reporter", "$packages/JSHint/reporter.js"],
...
}
While not very elegant, my "solution" is to run jshint in my build system as well:
"build_systems": [
{
"name": "RunOnJavaScriptSave",
"selector": "source.js",
"cmd": "jshint $file --reporter \"$packages/JSHint/reporter.js\" && echo Test",
...
}
]
This works, but surely there must be a more elegant way to have several build systems that use the same selector (source.js in my case) and have them all run on save?
Any help would be much appreciated.

Load namespaces with composer as psr-0 libraries

I am using Silex (and so, composer) for any web application project.
I would like to autoload 4 entities and forms:
FSB\MyProject\Entity\Booking --> /src/FSB/MyProject/Entity/Booking.php
FSB\MyProject\Form\BookingType --> /src/FSB/MyProject/Form/BookingType.php
FSB\MyProject\Entity\Contact --> /src/FSB/MyProject/Entity/Contact.php
FSB\MyProject\Form\ContactType --> /src/FSB/MyProject/Form/ContactType.php
The composer.json is defined with a psr-0 array:
{
"require": {
"silex/silex": "~1.0",
[...]
},
"autoload": {
"psr-0": {
"FSB": "src/"
}
}
}
Then in my index.php file I just call :
use FSB\MyProject\Entity\Contact;
use FSB\MyProject\Form\ContactType;
use FSB\MyProject\Entity\Booking;
use FSB\MyProject\Form\BookingType;
$contact = new Contact();
Everything works fine in my dev-environment, MAMP, php 5.4
But it breaks on my production server, Apache2 on Ubuntu with PHP 5.4 too
"PHP Fatal error: Class 'FSB\MyProject\Entity\Contact' not found
in /[...]/index.php"
I've tried many ways to load my entities like :
{
"require": {
"silex/silex": "~1.0",
[...]
},
"autoload": {
"psr-0": {
"FSB/Palmeraie": "src/",
}
}
}
Or even:
{
"require": {
"silex/silex": "~1.0",
[...]
},
"autoload": {
"psr-0": {
"FSB\\Palmeraie\\Entity": "src/",
"FSB\\Palmeraie\\Form": "src/"
}
}
}
--> Several ways are working in my dev-environment ; none on my production environment...
Did I miss something ?
Check your file/directory names for correct capitalization, file names on OS X are case insensitive opposed to those on Linux/Unix file systems.
Execute "composer update" and it will work again.