psr-4 changes to working examples do not work - psr-4

I have to achieve that all sources are inclueded like this
$instance = new \d1\d2\d3\app\MyClass();
Where d1\d2\d3\ points to the root directory.
I have read the basics on
https://getcomposer.org/doc/04-schema.md#psr-4
and on
http://www.php-fig.org/psr/psr-4/.
The example on https://laracasts.com/lessons/psr-4-autoloading also works for me.
My problem is: a soon as I change the code below a little bit towards to my needs the class is not found any more. (Yes I issue the command composer update after the changes in composer.json. And yes I updated the composer using self-update).
So this works:
composer.json
"autoload": {
"psr-4": {
"Laracasts\\": "app/Laracasts"
}
}
index.php
require_once 'vendor/autoload.php';
// According to https://laracasts.com/lessons/psr-4-autoloading
$test = new \Laracasts\Repositories\BlogRepository();
$test->hello();
But this does'nt:
composer.json
"autoload": {
"psr-4": {
"d1\\": "app/Laracasts"
}
}
index.php
require_once 'vendor/autoload.php';
// According to https://laracasts.com/lessons/psr-4-autoloading
$test = new \d1\Repositories\BlogRepository();
$test->hello();
What am I doing wrong?
I am using php 5.3.28 with IIS 8.

Solved!
The problem was that Prefix used in composer.json ('d1' in this case) must also be used as the Namespace-Prefix in the class to be included. And this was not the case while I posted the previous post.
For the non-working example above to work the files must look like this in case of 'd1':
app\Laracasts\Repositories\BlogRepository.php
namespace d1\Repositories;
class BlogRepository
{
public function hello(){
echo 'hello from a non-root dir!';
}
}
index.php
require_once 'vendor/autoload.php';
$test = new \d1\Repositories\BlogRepository();
$test->hello();
composer.json
{
"autoload": {
"psr-4": {
"d1\\": "app/Laracasts"
}
}
}
The achieve that d1\d2\d3\ points to the root directory, we have to adjust the prefix in all 3 files mentioned above and move the file with the class to the root directory. Then we have adjust the composer.json like this
"d1\\d2\\d3\\": ""

Related

wordpress live site : include them class in a cron job

import a class from theme directory outside of wordpress.
The CRON
I am cronning a daily job via Cpanel for my site that will feed a stats table. I created a file in the root Home/myDirectory (same level as public_html ) beloo is the code
<?php
include dirname(__FILE__) . '/public_html/wp-load.php'; // Load WP Functions
include dirname(__FILE__) . '/public_html/wp-content/themes/cooking/config/Data/ViewsData.php'; // require a class
function test(){
if(class_exists('ViewsData')){
$viewsData = new ViewsData;
$views= $viewsData::getViews(394);
update_post_meta(394 , 'views_test', $views);
}
die();
}
test();
THE CLASS
as shown in the code I am trying to include a class from the theme folder. this class include different functionaries to set , get and update the views data> bellow is an idea of how the class is structured ;
namespace GS\Data;
use GS\DisplayFunc;
class ViewsData {
static function getViews(){}
}
however class_exists('ViewsData') always return false.
any suggestions on what is wrong. or even on how to reorganize the whole cron solution. the most important is that I need to use many classes from my theme folder.
I was able to find the problem. it has to do with namespaces the code that works bellow :
<?php
include dirname(__FILE__) . '/public_html/wp-load.php'; // Load WP Functions
include dirname(__FILE__) . '/public_html/wp-content/themes/cooking/config/Data/ViewsData.php'; // require a class
function test(){
if(class_exists('GS\Data\ViewsData')){
$viewsData = new ViewsData;
$views= $viewsData::getViews(394);
update_post_meta(394 , 'views_test', $views);
}
die();
}
test();

Silverstripe 4: Include Template from Filepath

I'm working on a Silverstripe 4 project where we need to include a SS template file from a path.
Here's a simple example giving the gist of what I'm trying to achieve.
class ExampleController extends ContentController
{
public function IncludeTemplateFromFilePath() {
var $FilePath = '/path/to/file';
???
return $output
}
}
Template syntax:
<div>$IncludeTemplateFromFilePath</div>
I've had a look through the SSViewer documentation and looked at the Silverstripe source code but can't work out the correct syntax to make this work.
There are many examples of:
return SSViewer::get_templates_by_class(static::class, $suffix, self::class);
But what is the syntax to get a template from it's filepath?
I believe you can do the following:
public function IncludeTemplateFromFilePath()
{
return SSViewer::execute_string(
file_get_contents('/path/to/Template.ss'),
[
'Content' => 'Value that will be in $Content when used in /path/to/Template.ss'
]
);
}
Reference: http://api.silverstripe.org/4/SilverStripe/View/SSViewer.html#method_execute_string

Problems including inline JS in wordpress 4.2.2, wp_add_inline_script not working

i want to put inline js in worpress and according to the wordpress docs the wp_add_line_script is for this, BUT it doesn't exists in the functions.wp-scripts.php file . The other way i tried was using WP_Scripts::add_inline_script but this method doesn't exits as well.
It is funny that wp_add_inline_style works so i can doit with CSS but no with JS..
So i need to include inline JS but cannot use the docs function and i don't want to do
echo "<script>
//code
</script>
So what can i do?, and why this functions are not in wordpress 4.2.2?
Thanks
finally i end up implementing a static method in a helper class that prints the JS before body get closed, i think this can be impruved but for me worked...
With this class i can include an array of js/css files and inline javascript like:
Assets::registerCSS(['jquery-ui.min.css', 'jquery.tagsinput-revisited.css']);
Assets::registerJS(['jquery-ui.min.js', 'jquery.tagsinput-revisited.js'], ['jquery']);
and the inline JS like:
Assets::registerInlineJS($script);
Where $script is the JS code without the tags <script> </script>
This class is a piece of a plugin i'm working and it should work too for other plugins..in my case in the root folder of my plugin i have two folders named css and js thats why i call plugin_dir_url(DIR) . "js/$file" to look for the .js files
Hope this helps somebody
class Assets {
public static function registerJS(Array $files = [], Array $deps = []) {
foreach ($files as $file) {
wp_enqueue_script(basename($file, '.js'), plugin_dir_url(__DIR__) . "js/$file", $deps, false, false);
}
}
public static function registerCSS(Array $files = [], Array $deps = []) {
foreach ($files as $file) {
wp_enqueue_style(basename($file, '.css'), plugin_dir_url(__DIR__) . "css/$file", $deps, false);
}
}
public static function registerInlineJS($js) {
add_action('wp_print_footer_scripts', function() use ($js) {
echo '<script>';
echo $js;
echo '</script>';
});
}
}

Wordpress: wp_set_object_terms() not work from included php

I use wp_set_object_terms to set terms for user custom taxonomy, this function only works from the plugin main php file, but not from included php file.
for example this works wp-content/plugins/my_lugin/my_plugin.php:
function set_user_term_a()
{
$user_professions = Array('aaa','bbb');
wp_set_object_terms(46, $user_professions, 'user_profession', false);
clean_object_term_cache(46, 'user_profession');
}
add_action('init', 'set_user_term_a');
but if function included from another php it not work
wp-content/plugins/my_lugin/included_file.php:
echo "the file was successfully included"; // ok
function set_user_term_b()
{
$user_professions = Array('aaa','bbb');
wp_set_object_terms(46, $user_professions, 'user_profession', false);
clean_object_term_cache(46, 'user_profession');
}
add_action('init', 'set_user_term_b');
wp-content/plugins/my_lugin/my_plugin.php:
include_once("included_file.php");
Any ideas what wrong?

Cant access wp-load.php dynamically

How to have access to wp-load.php ?
I can access it using the following
require_once("../../../../wp-load.php");
But I need to find it dynamically so I am using the following but none of them works.
require_once( dirname(__FILE__).'/../../../wp-load.php');
require_once( dirname(__FILE__)."/../../../wp-load.php");
require_once( ABSPATH.'wp-load.php');
require_once( ABSPATH."wp-load.php");
How to have access to localhost:8888/wordpress/wp-load.php?
Add below code snippets at beginning of file where you require to load wp-load.php
/* FindWPConfig - searching for a root of wp */
function FindWPConfig($dirrectory){
global $confroot;
foreach(glob($dirrectory."/*") as $f){
if (basename($f) == 'wp-config.php' ){
$confroot = str_replace("\\", "/", dirname($f));
return true;
}
if (is_dir($f)){
$newdir = dirname(dirname($f));
}
}
if (isset($newdir) && $newdir != $dirrectory){
if (FindWPConfig($newdir)){
return false;
}
}
return false;
}
if (!isset($table_prefix)){
global $confroot;
FindWPConfig(dirname(dirname(__FILE__)));
include_once $confroot."/wp-config.php";
include_once $confroot."/wp-load.php";
}
The same question is here.
Denzel Chia answer was :
Put the following in the beginning of your file that requires
wp-load.php
//include wp-config or wp-load.php
$root = dirname(dirname(dirname(dirname(__FILE__))));
if (file_exists($root.'/wp-load.php')) {
// WP 2.6
require_once($root.'/wp-load.php');
} else {
// Before 2.6
require_once($root.'/wp-config.php');
}
Hope it helps

Resources