Registry Pattern location of variable declared - magento

As Magento uses
Mage::register('somevar',$object);
Then use
Mage::registery('somevar') to get the data anywhere in the system.
Now,
Is there any quick way to find out the location of the declaration of the 'somevar'?
Can we locate that file and line quickly?
Or we have to go through all the class and find it all manually.

grep,awk, or your IDE should suffice for finding strings. grep example:
grep -srn0 "Mage::register('current" ./
Output:
./app/code/core/Mage/Catalog/Helper/Product.php:324: Mage::register('current_category', $category);
./app/code/core/Mage/Catalog/Helper/Product.php:328: Mage::register('current_product', $product);
./app/code/core/Mage/Catalog/Model/Convert/Parser/Product.php:384: Mage::register('current_imported_inventory', $inventoryFields);
./app/code/core/Mage/Catalog/Model/Layer/Filter/Category.php:94: Mage::register('current_category_filter', $this->getCategory(), true);
./app/code/core/Mage/Catalog/controllers/CategoryController.php:57: Mage::register('current_category', $category);

Related

Codeigniter set_output function not working with image

I am trying to get the image as output, but it seems like set_output() function is not working properly with content type jpeg
My Code is given below...
$image = file_get_contents('assets/images/ThinkstockPhotos-145054512_small.jpg');
$this->output->set_content_type('jpeg')->set_output($image);
When I replace the image with a plain text file, in that case, it shows me the correct output
$file = file_get_contents('assets/images/test.txt');
$this->output->set_content_type('text')->set_output($file);
I have change content type from set_content_type('jpeg') to set_content_type('jpg') and set_content_type('gif') but stil it does not work and not show me on output.
What output I am getting now is shown in screenshot given below.
I was able to replicate the issue on my localhost setup only when I provided file_get_contents either a (1) bad path or (2) a non-existent image. I think you need to provide a full path to the image as with any directory/file related operation.
Try using FCPATH that is where you index.php lies and I assume your assets/ folder as well.
public function index() {
$file = FCPATH . 'assets/images/ThinkstockPhotos-145054512_small.jpg';
if (!is_file($file)) {
exit('File not found!');
}
$image = file_get_contents($file);
$this->output->set_content_type('jpeg')->set_output($image);
}
Note: if you get File not found! then assure that the file exists in /htdocs/assets/... where /htdocs/index.php is your main CI file

Get segment from url CodeIgniter but not first

I know i can get all segments from url like this
Lets say i have this example link
www.example.com/de/products.html
Using url_helper like this:
$data['url'] = $this->uri->uri_string();
I will get value like this
de/products
But i dont need first segment de, only products, the problem is that
i dont know how many segments it will be, i only need to remove the first
Is there possible to forget first segment with url helper in CI?
Try like this...
Use the php's explode() function to make the url string as array.Then apply array's array_shift() function which always removes the first element from array.
Code is looks like as below
$data= $this->uri->uri_string();
$arr=explode('/', $data);
array_shift($arr);
//print_r($arr);
Then use the php's implode() method to get the URI without first segment.Hope it will works...
$uri=implode('/',$arr);
echo $uri;
There is no URL helper in the CI to forget the first segment. However you can easily make a custom one and put #Hikmat's answer below it in the application/helpers/MY_url_helper.php in the Core folder.
e.g.
function my_forget_first_segment() {
$data= $this->uri->uri_string();
$arr=explode('/', $data);
array_shift($arr);
$uri=implode('/',$arr);
return $uri;
}
Before Edit answer.
You need to try this
$second_segment = $this->uri->segment(2);
From Codeigniter documentation -
$this->uri->segment(n);
Permits you to retrieve a specific segment. Where n is the segment number you wish to retrieve. Segments are numbered from left to right. For example, if your full URL is this:
http://example.com/index.php/news/local/metro/crime_is_up
The segment numbers would be this:
1. news
2. local
3. metro
4. crime_is_up
The optional second parameter defaults to NULL and allows you to set the return value of this method when the requested URI segment is missing. For example, this would tell the method to return the number zero in the event of failure:
$product_id = $this->uri->segment(3, 0);
example:
<?php
$data=$this->uri->segment(2);
$val=explode('.', $data);
echo $val[0];
?>

How to clear compiled view for specific blade template

PHP artisan view:clear command clears whole compiled views in an application.
How to clear compiled output for specific view.
Simple answer: Write your own command.
How do i start?
First of all, You must know that compiled views have different names than the original blade views.
What names do they have?
Laravel calls sha1() in the full file path. So for example. The compiled file name of layouts/app.blade.php (comes with default installation).
in versions less than 5.2 md5() is used instead of sha1(),
5.2, 5.3 => sha1()
5.1, 5.0, 4.2, 4.1, 4.0 => md5()
Assuming your version is >= 5.2
sha1('C:\xampp\htdocs\myapp\resources\views/layouts/app.blade.php');
So file name will be 9407584f16494299da8c41f4ed65dcb99af82ae2.php
How do i do that then?
Create new command that takes filename as an argument.
Add views path for filename in fire() function. As i showed you before C:\xampp\htdocs\myapp\resources\views (view full path) + /layouts/app.blade.php (filename)
$path = 'C:\xampp\htdocs\myapp\resources\views' . '/layouts/app.blade.php';
$path = sha1($path) . '.php'; To get the compiled filename.
Check if filename exists in compiled views dir
Delete file if exists
The command you'll have something like,
Note: If you have different view paths (Changed defaults), You must
make changes on my code below.
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
use RuntimeException;
class RemoveCompiled extends Command
{
protected $signature = 'view:clearOne {file}';
protected $description = 'Remove one compiled view!';
public function handle()
{
$path = sha1($this->laravel['config']['view.paths'][0] . '/' . $this->argument('file'));
$f = $this->laravel['config']['view.compiled'] . '\\'. $path . '.php';
if(!file_exists($f))
return; //do whatever you want
if(unlink($f))
echo "File deleted!";
}
}
Calling: php artisan view:clearOne layouts/app.blade.php

Laravel move uploaded file

It is working good for full path like this
$file=$request->file('file');
$file->move('C:\xampp\htdocs\modo\images',$file->getClientOriginalName());
But i cant understand why it doesnt for root folder path :
$file->move('\modo\images',$file->getClientOriginalName());
You need to use base_path() method. This method returns the fully qualified path to the project root:
So in your case try the below code:
$file = $request->file('file');
$file->move(base_path('\modo\images'), $file->getClientOriginalName());
and if you want to return the public directory then use:
$path = public_path();
For more info read Laravel helper functions
You need to do it this way:
$file->move(base_path('\modo\images'),$file->getClientOriginalName());

How to get the details of a file on Windows

If you open the properties of a file in Windows, there usually is a Details tab. I want to access the information on this tab, but I don't know how.
Is there a module for it? Does someone has a code sniplet?
I tried to work with Win32::File's GetAttributes, but these are not the attributes I was looking for.
use Win32::OLE;
my $objShell = Win32::OLE->new("Shell.Application") or die;
my $objFolder = $objShell->NameSpace($myDir) or die;
my $objFile = $objFolder->ParseName($fileName) or die;
while ( $i <= 34 )
{
my $propertyName = $objFolder->GetDetailsOf($fileName,$i);
my $propertyValue = $objFolder->GetDetailsOf($objFile,$i);
print "$i -- $propertyName -- $propertyValue\n";
$i++;
}
You can instantiate a COM "Shell.Application" object. It exposes a .NameSpace(folder) method that returns a reference to the name space of the indicated folder, which holds the information you need. The retrieved instance holds a Items collection with references to each of the files in the folder, and a .GetDetailsOf(file,property) to retrieve each of the values seen in the details tab and explorer columns.
Sorry i have no idea of perl, so i can not include any working code.

Resources