how to parse Laravel route file? - php

In roures.php file has contain
Route::get('roles/create',['as'=>'roles.create','uses'=>'RoleController#create','middleware' => ['permission:role-create']]);
Route::post('roles/create',['as'=>'roles.store','uses'=>'RoleController#store','middleware' => ['permission:role-create']]);
Route::get('roles/{id}',['as'=>'roles.show','uses'=>'RoleController#show']);
Route::get('roles/{id}/edit',['as'=>'roles.edit','uses'=>'RoleController#edit','middleware' => ['permission:role-edit']]);
Route::patch('roles/{id}',['as'=>'roles.update','uses'=>'RoleController#update','middleware' => ['permission:role-edit']]);
Route::delete('roles/{id}',['as'=>'roles.destroy','uses'=>'RoleController#destroy','middleware' => ['permission:role-delete']]);
Route::get('itemCRUD2',['as'=>'itemCRUD2.index','uses'=>'ItemCRUD2Controller#index','middleware' => ['permission:item-list|item-create|item-edit|item-delete']]);
Route::get('itemCRUD2/create',['as'=>'itemCRUD2.create','uses'=>'ItemCRUD2Controller#create','middleware' => ['permission:item-create']]);
Route::post('itemCRUD2/create',['as'=>'itemCRUD2.store','uses'=>'ItemCRUD2Controller#store','middleware' => ['permission:item-create']]);
Route::get('itemCRUD2/{id}',['as'=>'itemCRUD2.show','uses'=>'ItemCRUD2Controller#show']);
Route::get('itemCRUD2/{id}/edit',['as'=>'itemCRUD2.edit','uses'=>'ItemCRUD2Controller#edit','middleware' => ['permission:item-edit']]);
Route::patch('itemCRUD2/{id}',['as'=>'itemCRUD2.update','uses'=>'ItemCRUD2Controller#update','middleware' => ['permission:item-edit']]);
Route::delete('itemCRUD2/{id}',['as'=>'itemCRUD2.destroy','uses'=>'ItemCRUD2Controller#destroy','middleware' => ['permission:item-delete']]);
Expected output:
[
[
`url` => 'roles/create',
'as' => 'roles.create',
'uses'=>'RoleController#create'
],
[
`url` => 'roles/create',
'as' => 'roles.store',
'uses'=>'RoleController#store'
],
]
as so on

Here you can get the file content using file_get_content, we are using regex for parsing file content using regex.
Regex: /get\s*\(\s*'\K[^']+|as'\s*=>\s*'\K[^']+|uses'\s*=>\s*'\K[^']+/
get\s*\(\s*'\K[^']+ Here this will match Example: get(' match till '
as'\s*=>\s*'\K[^']+ Here this will match Example: as'=>' match till '
uses'\s*=>\s*'\K[^']+ Here this will match Example: uses'=>' match till '
Try this code snippet here
<?php
ini_set('display_errors', 1);
$string="Route::get('roles/create',['as'=>'roles.create','uses'=>'RoleController#create','middleware' => ['permission:role-create']]);
Route::post('roles/create',['as'=>'roles.store','uses'=>'RoleController#store','middleware' => ['permission:role-create']]);
Route::get('roles/{id}',['as'=>'roles.show','uses'=>'RoleController#show']);
Route::get('roles/{id}/edit',['as'=>'roles.edit','uses'=>'RoleController#edit','middleware' => ['permission:role-edit']]);
Route::patch('roles/{id}',['as'=>'roles.update','uses'=>'RoleController#update','middleware' => ['permission:role-edit']]);
Route::delete('roles/{id}',['as'=>'roles.destroy','uses'=>'RoleController#destroy','middleware' => ['permission:role-delete']]);
Route::get('itemCRUD2',['as'=>'itemCRUD2.index','uses'=>'ItemCRUD2Controller#index','middleware' => ['permission:item-list|item-create|item-edit|item-delete']]);
Route::get('itemCRUD2/create',['as'=>'itemCRUD2.create','uses'=>'ItemCRUD2Controller#create','middleware' => ['permission:item-create']]);
Route::post('itemCRUD2/create',['as'=>'itemCRUD2.store','uses'=>'ItemCRUD2Controller#store','middleware' => ['permission:item-create']]);
Route::get('itemCRUD2/{id}',['as'=>'itemCRUD2.show','uses'=>'ItemCRUD2Controller#show']);
Route::get('itemCRUD2/{id}/edit',['as'=>'itemCRUD2.edit','uses'=>'ItemCRUD2Controller#edit','middleware' => ['permission:item-edit']]);
Route::patch('itemCRUD2/{id}',['as'=>'itemCRUD2.update','uses'=>'ItemCRUD2Controller#update','middleware' => ['permission:item-edit']]);
Route::delete('itemCRUD2/{id}',['as'=>'itemCRUD2.destroy','uses'=>'ItemCRUD2Controller#destroy','middleware' => ['permission:item-delete']]);";
preg_match_all("/get\s*\(\s*'\K[^']+|as'\s*=>\s*'\K[^']+|uses'\s*=>\s*'\K[^']+/", $string,$matches);
$result=array();
for($x=0;$x<count($matches[0]);$x+=3)
{
$result[]=array(
'url'=>$matches[0][$x],
'as'=>$matches[0][$x+1],
'uses'=>$matches[0][$x+2]
);
}
print_r($result);

Use the following regex match and replace:
Match: .*\('([a-z\/{}0-9]+)',\['as'=>(['a-z\.0-9]+),'uses'=>(['a-zA-Z#]+).*
Replace: [url=> $1, 'as' => $2, 'uses' => $3]
PS. wherever you come by a space to match, use \s* to counter it

Related

Regexp to search for strings like 'cpt' => on Notepad++

I'm trying to build a list of array of mime types for PHP. I got a long list of mime types but I need to remove all the 'xxx' => upfront. How to detect them using regexp cos I tried '[\u][a-z0-9]' => and it didn't work.
'cpt' => 'application/mac-compactpro',
'cpt' => 'application/x-compactpro',
'cpt' => 'application/x-cpt',
'crl' => 'application/pkcs-crl',
'crl' => 'application/pkix-crl',
'crt' => 'application/pkix-cert',
'crt' => 'application/x-x509-ca-cert',
'crt' => 'application/x-x509-user-cert',
'csh' => 'application/x-csh',
'csh' => 'text/x-script.csh',
'css' => 'application/x-pointplus',
Try this ....
Find:^\s*'\w+' =>
Replace with:empty
You may try the following find and replace, in regex mode:
Find: ^\s*'.*?'\s*=>\s*('.*?',?)$
Replace: $1
Demo
Let's keep it simple. You need to search for ^\s*'\w\w\w' =>
Don't forget to set the Search Mode to Regular Expression in the search dialog.

Laravel - phpunit array assertion

I am trying to write a phpunit test so I can test that I'm getting the correct assertion, this test i currently have which is passing and works as intended.
/**
#test
*/
$filmInfo = $this->postRequest();
$this->assertFilm($this->requestData, $filmInfo['id']);
$film = Film::findOrFail($filmInfo['id']);
$this->assertEquals('LOTR', $filmInfo['name']);
$this->assertEquals('Film about a ring', $filmInfo['description']);
$this->assertEquals('Fantasy', $filmInfo['genre']['main']);
$this->assertEquals('Adventure', $filmInfo['genre']['sub']);
}
This is the request data array it is referring to:
private function requestData(): array
{
return [
'name' => 'LOTR',
'description' => 'Film about a ring',
'main' => 'Fantasy',
'sub' => 'Adventure',
];
}
This test works fine and it passing but I want to test it within one assertion like so:
$this->assertEquals([
'name' => 'LOTR',
'description' => 'Film about a ring',
'genre' => [
'main' => 'Fantasy',
'sub' => 'Adventure'
]
,
], $filmInfo);
But I keep getting an error that the 2 arrays I'm asserting are not matching, do you guys have an idea on what could be causing this?
Just like what the PHPUnit said, your array aren't matching. Your array should match with all values in $filmInfo array.
From your code, we can guessing that you aren't comparing the id. Maybe you can try this code:
$this->assertEquals(array_merge($filmInfo, [
'name' => 'LOTR',
'description' => 'Film about a ring',
'genre' => [
'main' => 'Fantasy',
'sub' => 'Adventure'
],
]), $filmInfo);

Youtube embed start time convert to time in seconds directly in array from regex group

I want to extend one php package which returns embed code from YouTube. I want to extend it and make embed code with start time querystring.
Piece of Code:
'website' => 'http://youtube.com',
'ssl' => true,
'url' => [
'^(https?://)?(?:www\.)?youtu\.be/([0-9a-zA-Z-_]{11})',
'^(https?://)?(?:www\.)?(?:youtu\.be/|youtube\.com/(?:embed/|v\/|watch\?v=|watch\?.+&v=))((\w|-){11})(?:.+&t=)(\S+)',
],
'info' => [
'id' => '{1}',
'url' => '{protocol}://youtu.be/{1}',
'dataUrl' => '{protocol}://gdata.youtube.com/feeds/api/videos/{1}?v=2&alt=jsonc',
'imageRoot' => '{protocol}://img.youtube.com/vi/{1}/',
],
'render' => [
// iframe attributes
'sizeRatio' => 1.77,
'iframe' => [
'src' => '{protocol}://www.youtube.com/embed/{1}?rel=0&wmode=transparent&start={3}',
'width' => 560,
'height' => 315,
'allowfullscreen' => null,
'frameborder' => 0,
],
{3} is a start time from YouTube in format like 1h22m59s or 22m59s or 59s. I am getting {3} from regex from the last group : (?:.+&t=)(\S+)',
Is it possible to run function directly in array value to convert {3} time in seconds? For embed videos I need &start=123 (not ?start=1h22m59s )
I solved it by editing package source. It was the best solution.

create a .php file through the launch of a .bat

I need to launch a .bat file that creates a php file, containing this text:
<?php
$_mail_mailSpooler = array (
'classname' => 'mail_mailSpooler',
'from' => 'test#test.com',
'array_indirizzi_mail' =>
array (
0 => ‘lol#hotmail.com’,
'testo_mail' => ‘prova’,
'filename_html' => NULL,
'oggetto_mail' => ‘prova’,
'elenco_allegati' => '',
'forza_mail_txt' => 0,
'asl_logger' => '',
'log_identifier' => NULL,
'log_in_web' => 0,
'log_str' => '',
'nome_file_dump' => '../mailSpooler/mail_incoming/maildda2.php',
);
I have tried echo " (test)" >gnagna.php
but it doen't work.
any suggestion please?
thanks
use type instead of echo - echo's only supposed to give user feedback, file reading and writing is properly done by type. Have a look at http://www.robvanderwoude.com/allhelpw2ksp4_en.php#TYPE for more
sorry, but i'm not already able to launch my php with right code.
i've tried type <?php
$_mail_mailSpooler = array (
'classname' => 'mail_mailSpooler',
'from' => 'test#test.com',
'array_indirizzi_mail' =>
array (
0 => ‘lol#hotmail.com’,
'testo_mail' => ‘prova’,
'filename_html' => NULL,
'oggetto_mail' => ‘prova’,
'elenco_allegati' => '',
'forza_mail_txt' => 0,
'asl_logger' => '',
'log_identifier' => NULL,
'log_in_web' => 0,
'log_str' => '',
'nome_file_dump' => '../mailSpooler/mail_incoming/maildda2.php',
); C:\>test.php
but it doesn't work.
moreover your link says that this command displays the content of an existing file and i need to create a new one!

CakePHP Router::connect and regex

I'm having some trouble with redirection in config/routes.php file
I can't find the right regex
$_skill = '[A-Za-z\-]+'
Router::connect('/-:skill/:city-:zipcode:shit', array('controller' => 'redirects', 'action' => 'district'),
array('city' => '[A-Za-z-0-9\]+.(er|eme)-arrondissement',
'skill' => $_skill,
'zipcode' => $_zipcode,
'shit' => '(.*)',
'pass' => array('zipcode'),
)
);
I would like to match any url where city ends with 'arrondissement' but i'm a total noob in regex
thank you.
With most regex you can use the dollar symbol to match the end of a string.
simply 'arrondissement$'
And drop all the stuff at the beginning.

Categories