Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue Extend Core Class #1653

Closed
Iamscalla opened this issue Jan 6, 2019 · 5 comments
Closed

Issue Extend Core Class #1653

Iamscalla opened this issue Jan 6, 2019 · 5 comments

Comments

@Iamscalla
Copy link
Contributor

There's an issue with getsharedinstance while extend the core class.
prior to the last major change on Autoload and Baseservice, it was all Good.

for instance i extended the view class and used the extended service as follows,
$renderer = Services::renderer($path, null, true);

i still get a call to the core class lib instead of the extended lib.
screenshot_2019-01-06 error

<?php

namespace App\Libraries\View;

use Config\Services;
use CodeIgniter\View\View as BaseView;

class View extends BaseView
{

	/**
	* Check if the view exist
	*
	* @param  string $view
	* @return boolean
	*/
	public function exists(string $view): bool
	{
		$fileExt  = pathinfo($view, PATHINFO_EXTENSION);
		$realPath = empty($fileExt) ? $view . '.php' : $view;
		$file = $this->viewPath . $realPath;
		if (! file_exists($file))
		{
			$file = $this->loader->locateFile($view, 'Views');
		}
		if (empty($file))
		{
			//log_message('error', "{$view} view is not found");
			return false;
		}
		return true;
	}
}

then i extended the render service as shown below

public static function renderer($viewPath = null, $config = null, bool $getShared = false)
	{
		if ($getShared)
		{
			return static::getSharedInstance('renderer', $viewPath, $config);
		}

		if (is_null($config))
		{
			$config = new \Config\View();
		}

		if (is_null($viewPath))
		{
			$paths = config(Paths::class);

			$viewPath = $paths->viewDirectory;
		}
		return new \App\Libraries\View\View(
			$config,
			$viewPath,
			static::locator(true), CI_DEBUG,
			static::logger(true)
		);
	}
@natanfelles
Copy link
Contributor

natanfelles commented Jan 6, 2019

The library namespace is App\Libraries\View.

I think the class is not in the correct filepath: APPPATH . 'Libraries/View/View.php'.

If is the case, move and test, please:

<?php namespace App\Controllers;

use CodeIgniter\Controller;
use Config\Services;

class Issue1653 extends Controller
{
	public function index()
	{
		$r = Services::renderer();
		\var_dump($r->exists('errors/html/unknown'));
		\var_dump($r->exists('errors/html/error_404'));
	}
}

I tested, and it worked.

@Iamscalla
Copy link
Contributor Author

@natanfelles did a check, the class path is correct. Mind u, this error occurs when the sharedinstance is true.

@natanfelles
Copy link
Contributor

Good to know that you found the bug when you saw that using the same instance in $r it worked.

Thanks for mind me (and saying why) even though I've seen it but thought it was part of your customizations! Next time you do not need to open an issue because of this, and that's very good.

@Iamscalla
Copy link
Contributor Author

@natanfelles please re-phrase "Good to know that you found the bug when you saw that using the same instance in $r it worked." I seem lost.

@natanfelles
Copy link
Contributor

No. You are not lost. You solved. And that's it.

Thanks, for say that if the $getShared param is set to true, this error occurs.

Have a great week, @chistel !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants