laravel proper way to extend(expand)/override specific vendor package file - laravel

the project I involved has composer require fengqi/hanzi into the vendor folder, which is used to convert simplified Chinese and traditional Chinese
For people who want to take a look at the package https://github.com/fengqi/hanzi
Inside the vendor folder, the package has the following example directory structure:
vendor/
--hanzi/
----src/
------Hanzi.php
------HanziDict.php
------Pinyin.php
The specific file HanziDict.php I want to modify has a really simple code structure:
<?php namespace fengqi\Hanzi;
return array (
'å•Š' => 'å•Š',
...
...
'sample char A'=>'sample char B'
);
The github repository of the package suggested that I can insert any "char C => char D" inside the php file if I found the certain chars are missing from the dictionary.
But I believe I should not directly put the code inside vendor folder since it will be override after update.
So, my problem is how I can properly override/extend this file in Laravel (such as insert "char C => char D" into the array).
I already read and know how to properly extend class outside of vendor folder but did not find any useful information about other types of php files. I wonder if there is certain ways or rules to do with this kinds of files.
Ideally I want to achieve something like:
outside of the vendor folder, I have an expanded ExtraHanziDict.php. So it can always build upon the vendor dict.php.
The following links is the vendor class code (only fewer simple functions to read the dict and convert character)
https://github.com/fengqi/hanzi/blob/master/src/Hanzi.php (apology for throwing the code)

Unfortunately, in this case, there's no easy way to change the dictionary. As you correctly guessed you should never change the content of the vendor folder as it's not committed to version control.
In your specific case what you can do is:
Ask the repository mantainer to add an api that allows you to add new dictionaries at runtime.
Fork the repository, change what you need, and use that instead (see loading a custom git repository with composer). If you think your changes can be useful to others open a Pull request on the original repository.
There's no much logic on the repository you linked. Each package you add to your composer.json file is a burden, code that doesn't belongs to you and you MUST blindly thrust (what if at some point the repository is hacked and some malicious code hidden into it?). Just create your own service to do such a simple task.

Related

Is there a Go implementation of fs.ReadDir for Google Cloud Storage?

I am creating a web application in Go.
I have modified my working code so that it can read and write files on both a local filesystem and a bucket of Google Cloud Storage based on a flag.
Basically I included a small package in the middle, and I implemented my-own-pkg.readFile or my-own-pkg.WriteFile and so on...
I have replaced all calls in my code where I read or save files from the local filesystem with calls to my methods.
Finally these methods include a simple switch case that runs the standard code to read/write locally or the code to read/wrote from/to a gcp bucket.
My current problem
In some parts I need to perform a ReadDir to get the list of DirEntries and then cycle though them. I do not want to change my code except for replacing os.readDir with my-own-pkg.ReadDir.
So far I understand that there is not a native function in the gcp module. So I suppose (but here I need your help because I am just guessing) that I would need an implementation of fs.FS for the gcp. It being a new feature of go 1.6 I guess it's too early to find one.
So I am trying to create simply a my-own-pkg.ReadDir(folderpath) function that does the following:
case "local": { }
case "gcp": {
<Use gcp code sample to list objects in my bucket with Query.Prefix = folderpath and
Query.Delimiter="/"
Then create a slice of my-own-pkg.DirEntry (because fs.DkrEntry is just an interface and so it needs to be implemented... :-( ) and return them.
In order to do so I need to implement also the interface fs.DirEntry (which requires the implementation of interface for FileInfo and maybe something else...)
Question 1) is this the right path to follow to solve my issue or is there a better way?
Question 2) (only) if so, does the gcp method that lists object with a prefix and a delimiter return just files? I can't see a method that returns also the list of prefixes found
(If I have prefix/file1.txt and prefix/a/file2.txt I would like to get both "file1.txt" and "a" as files and prefixes...)
I hope I was enough clear... This time I can't include code because it's incomplete... But in case it helps I can paste what I can.
NOTE: by the way go 1.6 allowed me to solve elegantly a similar issue when dealing with assets either embedded or on the filesystem thanks to the existing implementation of fs.FS and the related ReadDirFS. So good if I could follow the same route 🙂
By the way I am going on studying and experimenting so in case I am successful I will contribute as well :-)
I think your abstraction layer is good but you need to know something on Cloud Storage: The directory doesn't exist.
In fact, all the object are put at the root of the bucket / and the fully qualified name of the object is /path/to/object.file. You can filter on a prefix, that return all the object (i.e. file because directory doesn't exist) with the same path prefix.
It's not a full answer to your question but I'm sure that you can think and redesign the rest of your code with this particularity in mind.

What NOREPLACE means at netflix's nebula plugin?

I was using netflix's nebula. Looking here, I saw this line:
fileType [org.freecompany.redline.payload.Directive] - Default for types, e.g. CONFIG, DOC, NOREPLACE, LICENSE
I didn't find any doc about the actual meaning of this enum, but I've found the original code.
Now I want an actual description of this enum. I thought NOREPLACE is releated to being not allowed to replace the file. But I want to be sure and don't rely on assumptions.
I have only seen noreplace as an additional attribute on a config file, e.g. %config(noreplace). It means that if the user has edited the file, the installer should put its new version as filename.rpmnew; by default %config files are replaced with the user one put as filename.rpmold .

How to serve static profile images in node

This is really more of a "using what method" than a "how-to" question. I am creating a site in NodeJS with Express. So, each user has the ability to upload a profile picture, and my concern is how to route requests for these images. A couple of questions I have are:
Can I use express.static() to serve a default file if a valid one isn't specified? If not, am I going to have to specify a GET route for /img/profileand handle the FS querying there?
How can I find the correct image if multiple file extensions are allowed? (I originally just removed the file extension and they appeared in img tags anyway, is that okay?)
I am currently naming all pictures after their user's name. Looking ahead into the future (for apps I may have to scale), what are normal naming conventions for static user content? Are most stored with a UUID referencing the content in the database?
What else should I take into consideration that I may not have accounted for yet?
First question:
At present, I'd recommend storing your images in a predictable location that can be inferred from some combination of columns or fields in your database entries. One of those fields or columns would be a filename (accounts for different extensions). Then, if they haven't uploaded an image, you just lay down in your view code a reference to the generic "has not set an image" file.
express.static obviously can server static files, but it currently doesn't have a way to serve some other file if the one you wanted isn't there. Because this sounded like fun, I made some modifications to static to support what you request and submitted a couple of pull requests (the feature touched 2 different modules):
In send: https://github.com/visionmedia/send/pull/33
In connect: https://github.com/senchalabs/connect/pull/999
I don't know if those will get included in the project, but if you want to see my forks that have these changes, they are here:
https://github.com/bigohstudios/send
https://github.com/bigohstudios/connect
If this went through, you would mount a static at the root of your profile image files:
app.use(static('<profile image files root>', { fallback: 'anon.jpg'}))
Second question
I generally store the extension in my DB, then when I load the image, I have the correct extension to put into the src attribute on the img tag.
Third question
If you can guarantee unique names, then using those would work. I prefer using the DB id or a UUID as you suggest. It's less intuitive when scanning all the image uploads, but I rarely find myself doing that. I'm usually hunting for a specific image, and I can look up the identifier for that as needed.
Fourth question
You may end up storing the static content outside your app server (e.g. on S3). If that happens, then of course static won't help you.

Silverstripe: Adding to the translation tables

I need to add additional translations to the Silverstripe 3 translation tables in sapphire/lang/[lang].yml
I am pulling in 'sapphire' as an SVN external so cannot just add the new values to the existing yml files.
Am I able to create additional language yml files for languages to add to these translations in mysite directory?
Yes, just create a new module with the following structure:
z_translations/
_config.php
lang/
en.yml
The z_ prefix ensures it comes alphabetically last, currently there's no way to influence loading order. the translations part can be anything. _config.php can be empty, but needs to be present for the folder to be detected as a module.
Note that in order to override translations in framework/admin/lang/, you have to create an admin subfolder in your module as well ... don't ask ;)

Magento - Make a copy of history.phtml and use it in my own way

I’m looking to find a way to copy the page history that we can access from My account/My orders which is available on this directory template/sales/order/history.phtml and use its content on my own way without affecting the original one. I’ve been trying many ways, as copying the whole directory and editing the Xml files related to it in order to setup up the right path and make it work, unfortunately it was a failure. I would like to know if you could give me a solution for this.
thx.
To use the functions of a block inside another .phtml I'm quite sure you can use getBlock
$blockFunctions = $this->getLayout()->getBlock('sales/order_history');
$order = $blockFunctions->getOrderHistory();
And to add a block in your custom module you'll need to create a .xml file for your block and add it to your template, you'll also have to add the actual .phtml file. Take a look at the moduleCreator (http://www.magentocommerce.com/magento-connect/danieln/extension/1108/modulecreator) this handles most of this quite well.
This is by no means througher its just a rough guide.

Resources